src/EventListener/GtmRouteListener.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Repository\Blog\ArticleCategoryRepository;
  5. use App\Repository\Blog\ArticleRepository;
  6. use App\Repository\Cms\PageRepository;
  7. use App\Repository\Product\ProductRepository;
  8. use App\Repository\Taxon\TaxonRepository;
  9. use Sylius\Component\Channel\Context\ChannelContextInterface;
  10. use Sylius\Component\Locale\Context\LocaleContextInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Xynnn\GoogleTagManagerBundle\Service\GoogleTagManagerInterface;
  16. final class GtmRouteListener
  17. {
  18.     private GoogleTagManagerInterface $googleTagManager;
  19.     private PageRepository $pageRepo;
  20.     private ChannelContextInterface $channelContext;
  21.     private LocaleContextInterface $localeContext;
  22.     private TaxonRepository $taxonRepo;
  23.     private FlashBag $flashBag;
  24.     private ProductRepository $productRepo;
  25.     private AuthorizationCheckerInterface $authorizationCheckerInterface;
  26.     private RequestStack $requestStack;
  27.     public function __construct(ChannelContextInterface $channelContextLocaleContextInterface $localeContextGoogleTagManagerInterface $googleTagManagerPageRepository $pageRepoTaxonRepository $taxonRepoProductRepository $productRepo/* ArticleRepository $articleBlogRepo, ArticleCategoryRepository $articleCatBlogRepo, */ FlashBag $flashBagAuthorizationCheckerInterface $authorizationCheckerInterfaceRequestStack $requestStack)
  28.     {
  29.         $this->googleTagManager $googleTagManager;
  30.         $this->pageRepo $pageRepo;
  31.         $this->channelContext $channelContext;
  32.         $this->localeContext $localeContext;
  33.         $this->taxonRepo $taxonRepo;
  34.         $this->flashBag $flashBag;
  35.         $this->productRepo $productRepo;
  36.         $this->authorizationCheckerInterface $authorizationCheckerInterface;
  37.         $this->requestStack $requestStack;
  38.     }
  39.     public function onKernelRequest(RequestEvent $event): void
  40.     {
  41.         if (!$event->isMasterRequest())
  42.         {
  43.             return;
  44.         }
  45.         // Pas de suite si le code de GoogleTagManager n'est pas ajouté en BO
  46.         if ($this->channelContext->getChannel()->getGoogleTagManagerId() === null)
  47.         {
  48.             return;
  49.         }
  50.         // Par défaut
  51.         $sPageCategory1 '';
  52.         $sPageCategory2 '';
  53.         $sPageCategory3 '';
  54.         $sPageName $event->getRequest()->getPathInfo();
  55.         $sLoginStatus = ($this->authorizationCheckerInterface->isGranted('IS_AUTHENTICATED_FULLY')) ? 'true' 'false';
  56.         switch ($event->getRequest()->get('_route'))
  57.         {
  58.             case 'sylius_shop_homepage':
  59.                 // Succès de la page contact
  60.                 $flashSuccess $this->flashBag->peekAll();
  61.                 if (!empty($flashSuccess))
  62.                 {
  63.                     foreach ($flashSuccess as $key => $val)
  64.                     {
  65.                         if ($key == 'success')
  66.                         {
  67.                             foreach ($val as $flash)
  68.                             {
  69.                                 if ($flash == 'sylius.contact.request_success')
  70.                                 {
  71.                                     $sPageCategory1 'contact';
  72.                                     $sPageCategory2 'form';
  73.                                     $sPageCategory3 'form completed';
  74.                                     $sPageName '/contact/form/completed';
  75.                                     break;
  76.                                 }
  77.                             }
  78.                         }
  79.                     }
  80.                 }
  81.                 if ($sPageCategory1 == '')
  82.                 {
  83.                     // Homepage
  84.                     $sPageCategory1 'homepage';
  85.                     $sPageCategory2 '';
  86.                     $sPageCategory3 '';
  87.                     $sPageName '/homepage';
  88.                 }
  89.                 break;
  90.             case 'monsieurbiz_sylius_search_taxon':
  91.                 // Catégorie
  92.                 $sSlug str_replace('/categorie/'''$sPageName);
  93.                 $sSlug str_replace('/taxons/'''$sPageName);
  94.                 $taxon $this->taxonRepo->findOneBySlug($sSlug$this->localeContext->getLocaleCode());
  95.                 if (!empty($taxon))
  96.                 {
  97.                     $sPageCategory1 = ($taxon->getParent()) ? $taxon->getParent()->getName() : '';
  98.                     $sPageCategory2 'categorie';
  99.                     $sPageCategory3 $taxon->getName();
  100.                     if ($sPageCategory1 == 'Categories')
  101.                     {
  102.                         $sPageCategory1 $sPageCategory3;
  103.                         $sPageCategory3 '';
  104.                     }
  105.                 }
  106.                 break;
  107.             case 'sylius_shop_product_show':
  108.                 // Produit
  109.                 $sSlug str_replace('/produit/'''$sPageName);
  110.                 $sSlug str_replace('/products/'''$sPageName);
  111.                 $product $this->productRepo->findOneBySlug($this->localeContext->getLocaleCode(), $this->channelContext->getChannel(), $sSlug);
  112.                 if (!empty($product))
  113.                 {
  114.                     $taxon $product->getMainTaxon();
  115.                     if ($taxon) {
  116.                         $sPageCategory1 = ($product && $taxon->getParent()) ? $taxon->getParent()->getName() : '';
  117.                         $sPageCategory2 'produit';
  118.                         $sPageCategory3 $taxon->getName();
  119.                     }
  120.                 }
  121.                 break;
  122.             case 'bitbag_sylius_cms_plugin_shop_page_show':
  123.                 // Pages
  124.                 $sSlug str_replace('/page/'''$sPageName);
  125.                 $page $this->pageRepo->findOneBySectionCodeAndChannelCodeAndSlug(''$this->localeContext->getLocaleCode(), $this->channelContext->getChannel()->getCode(), $sSlug);
  126.                 if (!empty($page))
  127.                 {
  128.                     $sections reset($page)->getSections()->toArray();
  129.                     if (!empty($sections))
  130.                     {
  131.                         $sPageCategory1 reset($sections)->getName();
  132.                     }
  133.                     else
  134.                     {
  135.                         $sPageCategory1 reset($page)->getName();
  136.                     }
  137.                     $sPageCategory2 'content page';
  138.                     $sPageCategory3 reset($page)->getName();
  139.                 }
  140.                 break;
  141.             case 'sylius_shop_contact_request':
  142.                 // Page contact
  143.                 $sPageCategory1 'contact';
  144.                 $sPageCategory2 'form';
  145.                 $sPageCategory3 'form started';
  146.                 $sPageName '/contact/form/started';
  147.                 break;
  148.             case 'sylius_shop_login':
  149.                 // Page login
  150.                 $sPageCategory1 'login';
  151.                 $sPageCategory2 '';
  152.                 $sPageCategory3 '';
  153.                 $sPageName '/contact/form/started';
  154.                 break;
  155.             case 'sylius_shop_register':
  156.                 // Page inscription
  157.                 $sPageCategory1 'register';
  158.                 $sPageCategory2 'account-creation';
  159.                 $sPageCategory3 'step0';
  160.                 break;
  161.             case 'app_shop_register_from_cart':
  162.                 // Page inscription depuis le panier
  163.                 $sPageCategory1 'register';
  164.                 $sPageCategory2 'account-creation';
  165.                 $sPageCategory3 'step1';
  166.                 $sPageName '/register/account-creation/step1';
  167.                 break;
  168.             case 'app_shop_choice_login':
  169.                 // Page inscription succès depuis le panier
  170.                 $flashSuccess $this->flashBag->peekAll();
  171.                 if (!empty($flashSuccess))
  172.                 {
  173.                     foreach ($flashSuccess as $key => $val)
  174.                     {
  175.                         if ($key == 'success')
  176.                         {
  177.                             foreach ($val as $flash)
  178.                             {
  179.                                 if ($flash == 'sylius.customer.register')
  180.                                 {
  181.                                     $sPageCategory1 'register';
  182.                                     $sPageCategory2 'account-creation';
  183.                                     $sPageCategory3 'step2';
  184.                                     $sPageName '/register/account-creation/step2';
  185.                                 }
  186.                             }
  187.                         }
  188.                     }
  189.                 }
  190.                 break;
  191.             case 'monsieurbiz_sylius_search_search':
  192.             case 'monsieurbiz_sylius_search_post':
  193.                 // Page recherche
  194.                 $sRecherche htmlspecialchars(urldecode(str_replace('/search/'''$sPageName)));
  195.                 $sPageCategory1 'search';
  196.                 $sPageCategory2 $sRecherche;
  197.                 $sPageCategory3 '';
  198.                 break;
  199.                 /*
  200.                 case 'odiseo_sylius_blog_plugin_shop_article_index':
  201.                     // Blog index
  202.                     $sPageCategory1 = 'blog';
  203.                     $sPageCategory2 = 'index';
  204.                     break;
  205.                 case 'odiseo_sylius_blog_plugin_shop_article_index_by_category':
  206.                     // Blog categ
  207.                     $sPageCategory1 = 'blog';
  208.                     $sPageCategory2 = 'categorie';
  209.                     $sSlug = str_replace('/blog/articles/category/', '', $sPageName);
  210.                     $category = $this->articleCatBlogRepo->findOneByCategorySlug($this->localeContext->getLocaleCode(), $sSlug);
  211.                     if (!empty($category))
  212.                     {
  213.                         $sPageCategory3 = $category->getTitle();
  214.                     }
  215.                     break;
  216.                 case 'odiseo_sylius_blog_plugin_shop_article_show':
  217.                     // Blog article
  218.                     $sPageCategory1 = 'blog';
  219.                     $sPageCategory2 = 'article';
  220.                     $sSlug = str_replace('/blog/articles/', '', $sPageName);
  221.                     $article = $this->articleBlogRepo->findOneBySlugAndChannel($sSlug, $this->localeContext->getLocaleCode(), $this->channelContext->getChannel()->getCode());
  222.                     if (!empty($article))
  223.                     {
  224.                         $sPageCategory3 = $article->getTitle();
  225.                     }
  226.                     break;
  227.                 */
  228.             default:
  229.                 // dump($event->getRequest()->get('_route'));
  230.                 // exit;
  231.         }
  232.         $this->googleTagManager->setData('pageCategory1'$this->replace_accents($sPageCategory1));
  233.         $this->googleTagManager->setData('pageCategory2'$this->replace_accents($sPageCategory2));
  234.         $this->googleTagManager->setData('pageCategory3'$this->replace_accents($sPageCategory3));
  235.         $this->googleTagManager->setData('pageName'$sPageName);
  236.         // Utilise session pour être affiché qu'une fois
  237.         if ($sLoginStatus == 'true')
  238.         {
  239.             $session $this->requestStack->getSession();
  240.             if ($session->get('loginStatus') === null)
  241.             {
  242.                 $this->googleTagManager->setData('login_status'$sLoginStatus);
  243.                 $session->set('loginStatus'true);
  244.             }
  245.         }
  246.     }
  247.     private function replace_accents($str)
  248.     {
  249.         $str htmlentities($strENT_COMPAT'UTF-8');
  250.         $str preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde|ring);/''$1'$str);
  251.         return html_entity_decode($str);
  252.     }
  253. }