<?php
namespace Elements\Bundle\TrackingBundle\EventSubscriber;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class SessionBagSubscriber implements EventSubscriberInterface
{
private $pimcoreVersion;
public function __construct($pimcoreVersion)
{
$this->pimcoreVersion = $pimcoreVersion;
}
public static function getSubscribedEvents(): array
{
return [
//run after Symfony\Component\HttpKernel\EventListener\SessionListener
KernelEvents::REQUEST => ['onKernelRequest', 127],
];
}
/**
* @param KernelEvent $event
*/
public function onKernelRequest(KernelEvent $event)
{
//this will only work in pimcore X -> see SessionGa4Configurator.php for pim 6
if ($this->pimcoreVersion >= 10) {
//for deprecation issues
if (method_exists($event, 'isMainRequest')) {
$isMainRequest = $event->isMainRequest();
} else {
$isMainRequest = $event->isMasterRequest();
}
if (!$isMainRequest) {return;}
$session = $event->getRequest()->getSession();
$bag = new AttributeBag('_session_ga4');
$bag->setName('session_ga4');
$session->registerBag($bag);
}
}
}