custom/plugins/WbmTagManagerEcomm/src/Subscriber/StorefrontRenderSubscriber.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wbm\TagManagerEcomm\Subscriber;
  3. use Shopware\Storefront\Event\StorefrontRenderEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Wbm\TagManagerEcomm\Cookie\CustomCookieProvider;
  7. use Wbm\TagManagerEcomm\Services\DataLayerModules;
  8. use Wbm\TagManagerEcomm\Services\DataLayerRenderer;
  9. use Wbm\TagManagerEcomm\Utility\SessionUtility;
  10. class StorefrontRenderSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var DataLayerModules
  14.      */
  15.     private $modules;
  16.     /**
  17.      * @var DataLayerRenderer
  18.      */
  19.     private $dataLayerRenderer;
  20.     /**
  21.      * @var SessionInterface
  22.      */
  23.     private $session;
  24.     public function __construct(
  25.         DataLayerModules $modules,
  26.         DataLayerRenderer $dataLayerRenderer,
  27.         SessionInterface $session
  28.     ) {
  29.         $this->modules $modules;
  30.         $this->dataLayerRenderer $dataLayerRenderer;
  31.         $this->session $session;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             StorefrontRenderEvent::class => ['onRender', -1],
  37.         ];
  38.     }
  39.     public function onRender(StorefrontRenderEvent $event): void
  40.     {
  41.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  42.         $containerId $this->modules->getContainerId($salesChannelId);
  43.         $isActive = !empty($containerId) && $this->modules->isActive($salesChannelId);
  44.         $route $event->getRequest()->attributes->get('_route');
  45.         if (!$isActive) {
  46.             return;
  47.         }
  48.         if ($this->session->has(SessionUtility::ATTRIBUTE_NAME)) {
  49.             if (in_array($route$this->modules->getResponseRoutes(), true)) {
  50.                 $dataLayer json_decode($this->session->get(SessionUtility::ATTRIBUTE_NAME), true);
  51.             }
  52.         } else {
  53.             $parameters $event->getParameters();
  54.             $modules $this->modules->getModules();
  55.             if (array_key_exists($route$modules)) {
  56.                 $dataLayer $this->dataLayerRenderer->setVariables($route$parameters)->renderDataLayer($route);
  57.                 $dataLayer $dataLayer->getDataLayer($route);
  58.             }
  59.         }
  60.         if (!$event->getRequest()->isXmlHttpRequest()) {
  61.             $event->setParameter(
  62.                 'wbmTagManagerConfig',
  63.                 [
  64.                     'gtmContainerId' => $containerId,
  65.                     'isTrackingProductClicks' => $this->modules->isTrackingProductClicks($salesChannelId),
  66.                     'wbmCookieEnabledName' => CustomCookieProvider::WBM_GTM_ENABLED_COOKIE_NAME,
  67.                     'hasSWConsentSupport' => $this->modules->hasSWConsentSupport($salesChannelId),
  68.                     'scriptTagAttributes' => $this->modules->getScriptTagAttributes($salesChannelId),
  69.                     'dataLayerScriptTagAttributes' => $this->modules->getDataLayerScriptTagAttributes($salesChannelId),
  70.                     'extendedUrlParameter' => $this->modules->getExtendedURLParameter($salesChannelId),
  71.                     'gtmFunctionOverwrite' => $this->modules->getGtmFunctionOverwrite($salesChannelId)
  72.                 ]
  73.             );
  74.             if (!empty($dataLayer)) {
  75.                 $event->setParameter(
  76.                     'dataLayer',
  77.                     $dataLayer['default']
  78.                 );
  79.                 if (array_key_exists('onEvent'$dataLayer)) {
  80.                     $event->setParameter(
  81.                         'onEvent',
  82.                         $dataLayer['onEvent']
  83.                     );
  84.                 }
  85.             }
  86.         }
  87.     }
  88. }