custom/plugins/WbmTagManagerAnalytics/src/Subscriber/StorefrontRenderSubscriber.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wbm\TagManagerAnalytics\Subscriber;
  3. use Shopware\Storefront\Event\StorefrontRenderEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Wbm\TagManagerAnalytics\Cookie\CustomCookieProvider;
  7. use Wbm\TagManagerAnalytics\Services\DataLayerModules;
  8. use Wbm\TagManagerAnalytics\Services\DataLayerRenderer;
  9. use Wbm\TagManagerAnalytics\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 => [
  37.                 ['onRender', -1],
  38.                 ['addUserDataLayer', -1],
  39.             ],
  40.         ];
  41.     }
  42.     public function onRender(StorefrontRenderEvent $event): void
  43.     {
  44.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  45.         $containerId $this->modules->getContainerId($salesChannelId);
  46.         $isActive = !empty($containerId) && $this->modules->isActive($salesChannelId);
  47.         $route $event->getRequest()->attributes->get('_route');
  48.         if (!$isActive) {
  49.             return;
  50.         }
  51.         if ($this->session->has(SessionUtility::ATTRIBUTE_NAME)) {
  52.             if (in_array($route$this->modules->getResponseRoutes(), true)) {
  53.                 $dataLayer json_decode($this->session->get(SessionUtility::ATTRIBUTE_NAME), true);
  54.             }
  55.         } else {
  56.             $parameters $event->getParameters();
  57.             $modules $this->modules->getModules();
  58.             if (array_key_exists($route$modules)) {
  59.                 $dataLayer $this->dataLayerRenderer->setVariables($route$parameters)->renderDataLayer($route);
  60.                 $dataLayer $dataLayer->getDataLayer($route);
  61.             }
  62.         }
  63.         if (!$event->getRequest()->isXmlHttpRequest()) {
  64.             $event->setParameter(
  65.                 'wbmTagManagerConfig',
  66.                 [
  67.                     'gtmContainerId' => $containerId,
  68.                     'isTrackingProductClicks' => $this->modules->isTrackingProductClicks($salesChannelId),
  69.                     'isTrackingAddToWishlistClicks' => $this->modules->isTrackingAddToWishlistClicks($salesChannelId),
  70.                     'wbmCookieEnabledName' => CustomCookieProvider::WBM_GTM_ENABLED_COOKIE_NAME,
  71.                     'hasSWConsentSupport' => $this->modules->hasSWConsentSupport($salesChannelId),
  72.                     'scriptTagAttributes' => $this->modules->getScriptTagAttributes($salesChannelId),
  73.                     'dataLayerScriptTagAttributes' => $this->modules->getDataLayerScriptTagAttributes($salesChannelId),
  74.                     'extendedUrlParameter' => $this->modules->getExtendedURLParameter($salesChannelId),
  75.                     'gtmFunctionOverwrite' => $this->modules->getGtmFunctionOverwrite($salesChannelId)
  76.                 ]
  77.             );
  78.             if (!empty($dataLayer)) {
  79.                 $event->setParameter(
  80.                     'dataLayer',
  81.                     $dataLayer['default']
  82.                 );
  83.                 if (array_key_exists('onEvent'$dataLayer)) {
  84.                     $event->setParameter(
  85.                         'onEvent',
  86.                         $dataLayer['onEvent']
  87.                     );
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     public function addUserDataLayer(StorefrontRenderEvent $event): void
  93.     {
  94.         $customer $event->getSalesChannelContext()->getCustomer();
  95.         $event->setParameter(
  96.             'dataLayerUser',
  97.             json_encode([
  98.                 'event' => 'user',
  99.                 'id' => $customer $customer->getId() : null,
  100.             ])
  101.         );
  102.     }
  103. }