From cf97045dec5f5e548434ed89f6e6ccefa1e2d59e Mon Sep 17 00:00:00 2001
From: Pierre Gauthier <pigau@smile.fr>
Date: Thu, 2 May 2024 14:44:42 +0200
Subject: [PATCH] [Tracker] Add page type in add to cart event

---
 .../Plugin/QuotePlugin.php                    | 44 +++++++++++++++----
 .../etc/frontend/page_types.xml               |  4 ++
 2 files changed, 40 insertions(+), 8 deletions(-)
 create mode 100644 src/module-elasticsuite-tracker/etc/frontend/page_types.xml

diff --git a/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php b/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php
index 5ff720911..ba5358be8 100644
--- a/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php
+++ b/src/module-elasticsuite-tracker/Plugin/QuotePlugin.php
@@ -14,9 +14,8 @@
 
 namespace Smile\ElasticsuiteTracker\Plugin;
 
-use \Magento\Quote\Model\Quote;
-use \Magento\Catalog\Model\Product;
-use \Magento\Catalog\Model\Product\Type\AbstractType;
+use Magento\Framework\View\Layout\PageType\Config as PageTypeConfig;
+use Magento\Quote\Model\Quote;
 
 /**
  * Log add to cart events into the event queue.
@@ -42,6 +41,11 @@ class QuotePlugin
      */
     private $trackerHelper;
 
+    /**
+     * @var PageTypeConfig
+     */
+    private $pageTypeConfig;
+
     /**
      * @var \Psr\Log\LoggerInterface
      */
@@ -50,20 +54,23 @@ class QuotePlugin
     /**
      * Constructor.
      *
-     * @param \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $service       Tracker service.
-     * @param \Magento\Framework\Stdlib\CookieManagerInterface                $cookieManager Cookie manager.
-     * @param \Smile\ElasticsuiteTracker\Helper\Data                          $trackerHelper Tracker helper.
-     * @param \Psr\Log\LoggerInterface                                        $logger        Logger.
+     * @param \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $service        Tracker service.
+     * @param \Magento\Framework\Stdlib\CookieManagerInterface                $cookieManager  Cookie manager.
+     * @param \Smile\ElasticsuiteTracker\Helper\Data                          $trackerHelper  Tracker helper.
+     * @param \Magento\Framework\View\Layout\PageType\Config                  $pageTypeConfig The Page Type Configuration
+     * @param \Psr\Log\LoggerInterface                                        $logger         Logger.
      */
     public function __construct(
         \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $service,
         \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
         \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper,
+        \Magento\Framework\View\Layout\PageType\Config $pageTypeConfig,
         \Psr\Log\LoggerInterface $logger
     ) {
         $this->service       = $service;
         $this->cookieManager = $cookieManager;
         $this->trackerHelper = $trackerHelper;
+        $this->pageTypeConfig = $pageTypeConfig;
         $this->logger        = $logger;
     }
 
@@ -107,7 +114,10 @@ public function afterAddProduct(
      */
     private function logEvent(int $productId, int $storeId): void
     {
-        $pageData = [];
+        $pageData = [
+            'identifier' => 'checkout_cart_add',
+            'label'      => stripslashes($this->getPageTypeLabel('checkout_cart_add')),
+        ];
         $pageData['store_id']           = $storeId;
         $pageData['cart']['product_id'] = $productId;
 
@@ -144,4 +154,22 @@ private function readCookieValue($cookieName)
     {
         return $this->cookieManager->getCookie($cookieName);
     }
+
+    /**
+     * Human readable version of the page type identifier.
+     *
+     * @param string $pageTypeIdentifier Page type identifier.
+     *
+     * @return string
+     */
+    private function getPageTypeLabel($pageTypeIdentifier)
+    {
+        foreach ($this->pageTypeConfig->getPageTypes() as $identifier => $pageType) {
+            if ($pageTypeIdentifier === $identifier) {
+                return $pageType['label'];
+            }
+        }
+
+        return '';
+    }
 }
diff --git a/src/module-elasticsuite-tracker/etc/frontend/page_types.xml b/src/module-elasticsuite-tracker/etc/frontend/page_types.xml
new file mode 100644
index 000000000..3393bfe13
--- /dev/null
+++ b/src/module-elasticsuite-tracker/etc/frontend/page_types.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<page_types xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_types.xsd">
+    <type id="checkout_cart_add" label="Add Item to Cart"/>
+</page_types>