src/EventListener/GtmEventListener.php line 192

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\DatalayerGTM\AddEvent;
  5. use App\Entity\Product\Product;
  6. use App\Events\PostContactEvent;
  7. use App\Events\ProductViewItemListEvent;
  8. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  9. use Sylius\Component\Channel\Context\ChannelContextInterface;
  10. use Sylius\Component\Core\Model\OrderInterface;
  11. use Sylius\Component\Core\Repository\OrderRepositoryInterface;
  12. use Sylius\Component\Locale\Context\LocaleContextInterface;
  13. use Sylius\Component\Product\Model\ProductInterface;
  14. use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  20. use Symfony\Contracts\EventDispatcher\Event;
  21. final class GtmEventListener
  22. {
  23.     public const POST_CONTACT_SENDING 'post_contact_sending';
  24.     public const POST_CUSTOMER_REGISTER 'post_customer_register';
  25.     public const POST_CUSTOMER_LOGIN 'post_customer_login';
  26.     public const POST_CART_REMOVE 'post_cart_remove';
  27.     public const POST_CART_SAVE 'post_cart_save';
  28.     private OrderRepositoryInterface $orderRepository;
  29.     private AddEvent $addEvent;
  30.     private ChannelContextInterface $channelContext;
  31.     private SessionInterface $session;
  32.     private RequestStack $requestStack;
  33.     private RouterInterface $router;
  34.     private TaxonRepositoryInterface $taxonRepository;
  35.     private LocaleContextInterface $localeContext;
  36.     public function __construct(OrderRepositoryInterface $orderRepositoryAddEvent $addEventChannelContextInterface $channelContextSessionInterface $sessionRequestStack $requestStackRouterInterface $routerTaxonRepositoryInterface $taxonRepositoryLocaleContextInterface $localeContext)
  37.     {
  38.         $this->orderRepository $orderRepository;
  39.         $this->addEvent $addEvent;
  40.         $this->channelContext $channelContext;
  41.         $this->session $session;
  42.         $this->requestStack $requestStack;
  43.         $this->router $router;
  44.         $this->taxonRepository $taxonRepository;
  45.         $this->localeContext $localeContext;
  46.     }
  47.     public function onKernelRequest(RequestEvent $event)
  48.     {
  49.         if (!$event->isMainRequest()) {
  50.             return;
  51.         }
  52.         if ($this->isAdminRoute($event)) {
  53.             return;
  54.         }
  55.         if (!$this->hasGtmId()) {
  56.             return;
  57.         }
  58.         $this->addEvent->addEventReady($event);
  59.         //use session
  60.         if ($this->session->has(self::POST_CONTACT_SENDING)) {
  61.             $subject $this->session->get(self::POST_CONTACT_SENDING);
  62.             $this->session->remove(self::POST_CONTACT_SENDING);
  63.             $this->addEvent->addEventContact($subject);
  64.         }
  65.         if ($this->session->has(self::POST_CUSTOMER_REGISTER)) {
  66.             $subject $this->session->get(self::POST_CUSTOMER_REGISTER);
  67.             $this->session->remove(self::POST_CUSTOMER_REGISTER);
  68.             $this->addEvent->addEventRegister($subject);
  69.         }
  70.         if ($this->session->has(self::POST_CUSTOMER_LOGIN)) {
  71.             $subject $this->session->get(self::POST_CUSTOMER_LOGIN);
  72.             $this->session->remove(self::POST_CUSTOMER_LOGIN);
  73.             $this->addEvent->addEventLogin($subject);
  74.         }
  75.         if ($this->session->has(self::POST_CART_REMOVE)) {
  76.             $subject $this->session->get(self::POST_CART_REMOVE);
  77.             $this->session->remove(self::POST_CART_REMOVE);
  78.             $this->addEvent->addEventRemoveFromCart($subject);
  79.         }
  80.         if ($this->session->has(self::POST_CART_SAVE)) {
  81.             $this->session->remove(self::POST_CART_SAVE);
  82.             $this->addEvent->addEventSaveCart();
  83.         }
  84.         if ($event->getRequest()->get('_route') === 'sylius_shop_cart_summary') {
  85.             $this->addEvent->addEventViewCart();
  86.         }
  87.         if ($event->getRequest()->get('_route') === 'app_cart_management.cart.save') {
  88.             $this->session->set(self::POST_CART_SAVE'cart');
  89.         }
  90.         if ($event->getRequest()->get('_route') === 'sylius_shop_checkout_address') {
  91.             $this->addEvent->addEventBeginCheckout();
  92.         }
  93.         if ($event->getRequest()->get('_route') === 'sylius_shop_checkout_select_payment') {
  94.             $this->addEvent->addEventShippingInfo();
  95.             $this->addEvent->prepareAddPaymentInfo(); //un peu spécifique, ici on va juste préparer un cart qu'on cachera dans les données afin de pouvoir par la suite ajouter l'event addPaymentInfo lors du clique sur "payer"
  96.         }
  97.         if ($event->getRequest()->get('_route') === 'sylius_shop_order_thank_you') {
  98.             $orderId $event->getRequest()->getSession()->get('sylius_order_id');
  99.             if ($orderId === null) {
  100.                 return;
  101.             }
  102.             $order $this->orderRepository->find($orderId);
  103.             if (!$order instanceof OrderInterface) {
  104.                 return;
  105.             }
  106.             $this->addEvent->addEventPurchase($order);
  107.         }
  108.     }
  109.     public function productShow(ResourceControllerEvent $event): void
  110.     {
  111.         if (!$this->hasGtmId()) {
  112.             return;
  113.         }
  114.         $referer $this->requestStack->getCurrentRequest()->headers->get('referer');
  115.         $product $event->getSubject();
  116.         if (!$product instanceof ProductInterface) {
  117.             return;
  118.         }
  119.         if ($referer) {
  120.             $refererPath parse_url($referer\PHP_URL_PATH);
  121.             $routeInfo $this->router->match($refererPath);
  122.             if (isset($routeInfo['_route'])) {
  123.                 $routeName $routeInfo['_route'];
  124.                 if ($routeName === 'monsieurbiz_sylius_search_taxon' && array_key_exists('slug'$routeInfo)) {
  125.                     $taxon $this->taxonRepository->findOneBySlug($routeInfo['slug'], $this->localeContext->getLocaleCode());
  126.                     if ($taxon) {
  127.                         $this->addEvent->addEventSelectItem($product$taxon);
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.         $this->addEvent->addEventViewItem($product);
  133.     }
  134.     public function postCartRemove(ResourceControllerEvent $event): void
  135.     {
  136.         //if($this->isAdminRoute($event)){
  137.         //    return;
  138.         //}
  139.         if (!$this->hasGtmId()) {
  140.             return;
  141.         }
  142.         $this->session->set(self::POST_CART_REMOVE$event->getSubject()->getProduct()->getId()); //on est obligé de passer l'id et non le product, sinon bug bizarre lié à la locale
  143.     }
  144.     public function addToWishlist(Product $product): ?array //un peu spécifique, il n'est pas appelé via un event, mais directement dans le controller AddProductToWishlistAction
  145.     {
  146.         if (!$this->hasGtmId()) {
  147.             return null;
  148.         }
  149.         return $this->addEvent->addEventAddToWishlist($product);
  150.     }
  151.     public function addToCart(Product $product): ?array //un peu spécifique, il n'est pas appelé via un event, mais directement dans le controller OrderItemController
  152.     {
  153.         if (!$this->hasGtmId()) {
  154.             return null;
  155.         }
  156.         return $this->addEvent->addEventAddToCart($product);
  157.     }
  158.     public function contactPostSending(PostContactEvent $event): void
  159.     {
  160.         if (!$this->hasGtmId()) {
  161.             return;
  162.         }
  163.         $this->session->set(self::POST_CONTACT_SENDING$event->getSubject());
  164.     }
  165.     public function productViewItemList(ProductViewItemListEvent $event)
  166.     {
  167.         if (!$this->hasGtmId()) {
  168.             return;
  169.         }
  170.         $allProducts $event->getProducts();
  171.         if (array_key_exists('result'$allProducts) || array_key_exists('productsPalinal'$allProducts) || array_key_exists('productsSubCategory'$allProducts)) {
  172.             $this->addEvent->addEventViewItemList($allProducts);
  173.         }
  174.     }
  175.     public function customerPostRegister(ResourceControllerEvent $event): void
  176.     {
  177.         //if($this->isAdminRoute($event)){
  178.         //    return;
  179.         //}
  180.         if (!$this->hasGtmId()) {
  181.             return;
  182.         }
  183.         $this->session->set(self::POST_CUSTOMER_REGISTER$event->getSubject());
  184.     }
  185.     public function customerPostLogin(InteractiveLoginEvent $event): void
  186.     {
  187.         if ($this->isAdminRoute($event)) {
  188.             return;
  189.         }
  190.         if (!$this->hasGtmId()) {
  191.             return;
  192.         }
  193.         $this->session->set(self::POST_CUSTOMER_LOGIN$event->getAuthenticationToken()->getUser()->getId());
  194.     }
  195.     private function hasGtmId(): bool
  196.     {
  197.         if ($this->channelContext->getChannel()->getGoogleTagManagerId()) {
  198.             return true;
  199.         }
  200.         return false;
  201.     }
  202.     private function isAdminRoute(Event $event): bool
  203.     {
  204.         $routeName $event->getRequest()->get('_route');
  205.         if (null === $routeName) {
  206.             return false;
  207.         }
  208.         if (strpos($routeName'admin') !== false) {
  209.             return true;
  210.         }
  211.         return false;
  212.     }
  213. }