src/EventListener/GtmEventListener.php line 137

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.     /**
  37.      * @param OrderRepositoryInterface $orderRepository
  38.      * @param AddEvent $addEvent
  39.      * @param ChannelContextInterface $channelContext
  40.      * @param SessionInterface $session
  41.      * @param RequestStack $requestStack
  42.      * @param RouterInterface $router
  43.      * @param TaxonRepositoryInterface $taxonRepository
  44.      * @param LocaleContextInterface $localeContext
  45.      */
  46.     public function __construct(OrderRepositoryInterface $orderRepositoryAddEvent $addEventChannelContextInterface $channelContextSessionInterface $sessionRequestStack $requestStackRouterInterface $routerTaxonRepositoryInterface $taxonRepositoryLocaleContextInterface $localeContext)
  47.     {
  48.         $this->orderRepository $orderRepository;
  49.         $this->addEvent $addEvent;
  50.         $this->channelContext $channelContext;
  51.         $this->session $session;
  52.         $this->requestStack $requestStack;
  53.         $this->router $router;
  54.         $this->taxonRepository $taxonRepository;
  55.         $this->localeContext $localeContext;
  56.     }
  57.     public function onKernelRequest(RequestEvent $event)
  58.     {
  59.         if (!$event->isMainRequest()) {
  60.             return;
  61.         }
  62.         if($this->isAdminRoute($event)){
  63.             return;
  64.         }
  65.         if(!$this->hasGtmId()){
  66.             return;
  67.         }
  68.         $this->addEvent->addEventReady($event);
  69.         //use session
  70.         if ($this->session->has(self::POST_CONTACT_SENDING)) {
  71.             $subject $this->session->get(self::POST_CONTACT_SENDING);
  72.             $this->session->remove(self::POST_CONTACT_SENDING);
  73.             $this->addEvent->addEventContact($subject);
  74.         }
  75.         if ($this->session->has(self::POST_CUSTOMER_REGISTER)) {
  76.             $subject $this->session->get(self::POST_CUSTOMER_REGISTER);
  77.             $this->session->remove(self::POST_CUSTOMER_REGISTER);
  78.             $this->addEvent->addEventRegister($subject);
  79.         }
  80.         if ($this->session->has(self::POST_CUSTOMER_LOGIN)) {
  81.             $subject $this->session->get(self::POST_CUSTOMER_LOGIN);
  82.             $this->session->remove(self::POST_CUSTOMER_LOGIN);
  83.             $this->addEvent->addEventLogin($subject);
  84.         }
  85.         if ($this->session->has(self::POST_CART_REMOVE)) {
  86.             $subject $this->session->get(self::POST_CART_REMOVE);
  87.             $this->session->remove(self::POST_CART_REMOVE);
  88.             $this->addEvent->addEventRemoveFromCart($subject);
  89.         }
  90.         if ($this->session->has(self::POST_CART_SAVE)) {
  91.             $this->session->remove(self::POST_CART_SAVE);
  92.             $this->addEvent->addEventSaveCart();
  93.         }
  94.         if($event->getRequest()->get('_route') === "sylius_shop_cart_summary"){
  95.             $this->addEvent->addEventViewCart();
  96.         }
  97.         if($event->getRequest()->get('_route') === "app_cart_management.cart.save"){
  98.             $this->session->set(self::POST_CART_SAVE'cart');
  99.         }
  100.         if($event->getRequest()->get('_route') === "sylius_shop_checkout_address"){
  101.             $this->addEvent->addEventBeginCheckout();
  102.         }
  103.         if($event->getRequest()->get('_route') === "sylius_shop_checkout_select_payment"){
  104.             $this->addEvent->addEventShippingInfo();
  105.             $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"
  106.         }
  107.         if($event->getRequest()->get('_route') === "sylius_shop_order_thank_you"){
  108.             $orderId $event->getRequest()->getSession()->get('sylius_order_id');
  109.             if ($orderId === null) {
  110.                 return;
  111.             }
  112.             $order $this->orderRepository->find($orderId);
  113.             if (!$order instanceof OrderInterface) {
  114.                 return;
  115.             }
  116.             $this->addEvent->addEventPurchase($order);
  117.         }
  118.     }
  119.     public function productShow(ResourceControllerEvent $event): void
  120.     {
  121.        //exit;
  122.         //if($this->isAdminRoute($event)){
  123.         //    return;
  124.         //}
  125.         if(!$this->hasGtmId()){
  126.             return;
  127.         }
  128.         $referer $this->requestStack->getCurrentRequest()->headers->get('referer');
  129.         $product $event->getSubject();
  130.         if(!$product instanceof ProductInterface){
  131.             return;
  132.         }
  133.         if($referer){
  134.             $refererPath parse_url($refererPHP_URL_PATH);
  135.             $routeInfo $this->router->match($refererPath);
  136.             if (isset($routeInfo['_route'])) {
  137.                 $routeName $routeInfo['_route'];
  138.                 if($routeName === "monsieurbiz_sylius_search_taxon" && array_key_exists('slug'$routeInfo)){
  139.                     $taxon $this->taxonRepository->findOneBySlug($routeInfo['slug'], $this->localeContext->getLocaleCode());
  140.                     if($taxon){
  141.                         $this->addEvent->addEventSelectItem($product$taxon);
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.         $this->addEvent->addEventViewItem($product);
  147.     }
  148.     public function postCartRemove(ResourceControllerEvent $event): void
  149.     {
  150.         //if($this->isAdminRoute($event)){
  151.         //    return;
  152.         //}
  153.         if(!$this->hasGtmId()){
  154.             return;
  155.         }
  156.         $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
  157.     }
  158.     public function addToWishlist(Product $product): ?array //un peu spécifique, il n'est pas appelé via un event, mais directement dans le controller AddProductToWishlistAction
  159.     {
  160.         if(!$this->hasGtmId()){
  161.             return null;
  162.         }
  163.         return $this->addEvent->addEventAddToWishlist($product);
  164.     }
  165.     public function addToCart(Product $product): ?array //un peu spécifique, il n'est pas appelé via un event, mais directement dans le controller OrderItemController
  166.     {
  167.         if(!$this->hasGtmId()){
  168.             return null;
  169.         }
  170.         return $this->addEvent->addEventAddToCart($product);
  171.     }
  172.     public function contactPostSending(PostContactEvent $event): void
  173.     {
  174.         //if($this->isAdminRoute($event)){
  175.         //    return;
  176.         //}
  177.         if(!$this->hasGtmId()){
  178.             return;
  179.         }
  180.         $this->session->set(self::POST_CONTACT_SENDING$event->getSubject()->getSubject());
  181.     }
  182.     public function productViewItemList(ProductViewItemListEvent $event)
  183.     {
  184.         //if($this->isAdminRoute($event)){
  185.         //    return;
  186.         //}
  187.         if(!$this->hasGtmId()){
  188.             return;
  189.         }
  190.         $allProducts $event->getProducts();
  191.         if(array_key_exists('result'$allProducts) || array_key_exists('productsPalinal'$allProducts) ||  array_key_exists('productsSubCategory'$allProducts) ){
  192.             $this->addEvent->addEventViewItemList($allProducts);
  193.         }
  194.     }
  195.     public function customerPostRegister(ResourceControllerEvent $event): void
  196.     {
  197.         //if($this->isAdminRoute($event)){
  198.         //    return;
  199.         //}
  200.         if(!$this->hasGtmId()){
  201.             return;
  202.         }
  203.         $this->session->set(self::POST_CUSTOMER_REGISTER$event->getSubject());
  204.     }
  205.     public function customerPostLogin(InteractiveLoginEvent $event): void
  206.     {
  207.         if($this->isAdminRoute($event)){
  208.             return;
  209.         }
  210.         if(!$this->hasGtmId()){
  211.             return;
  212.         }
  213.         $this->session->set(self::POST_CUSTOMER_LOGIN$event->getAuthenticationToken()->getUser()->getId());
  214.     }
  215.     protected function hasGtmId() :bool
  216.     {
  217.         if($this->channelContext->getChannel()->getGoogleTagManagerId()){
  218.             return true;
  219.         }
  220.         return false;
  221.     }
  222.     protected function isAdminRoute(Event $event): bool
  223.     {
  224.         $routeName $event->getRequest()->get('_route');
  225.         if (null === $routeName) {
  226.             return false;
  227.         }
  228.         if (strpos($routeName'admin') !== false) {
  229.             return true;
  230.         }
  231.         return false;
  232.     }
  233. }