-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathEventSubscriber.php
More file actions
39 lines (33 loc) · 1.24 KB
/
EventSubscriber.php
File metadata and controls
39 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<php>
use Ibexa\Contracts\ConnectorRaptor\Tracking\EventMapperInterface;
use Ibexa\Contracts\ConnectorRaptor\Tracking\ServerSideTrackingDispatcherInterface;
use Ibexa\Contracts\ConnectorRaptor\Tracking\EventType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ProductViewTrackingSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly EventMapperInterface $eventMapper,
private ServerSideTrackingDispatcherInterface $trackingDispatcher,
) {}
public static function getSubscribedEvents(): array
{
return [KernelEvents::RESPONSE => ['onResponse', -10]];
}
public function onResponse(ResponseEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
// Example: track only if request has specific attribute
$product = $request->attributes->get('product');
if (!$product) {
return;
}
$eventData = $this->eventMapper->map(EventType::VISIT, $product);
$this->trackingDispatcher->dispatch($eventData);
}
}
</php>