From 767636e262ac9364e788da64b01a69697216abdb Mon Sep 17 00:00:00 2001 From: Romain Ruaud Date: Thu, 17 Oct 2024 12:20:56 +0200 Subject: [PATCH 1/2] Prevent tracker to open sessions when registering events. --- .../Helper/Data.php | 2 +- .../Model/Customer/TrackingService.php | 9 +-- .../Model/CustomerDataTrackingManager.php | 20 +++--- .../Plugin/SessionStartCheckerPlugin.php | 70 +++++++++++++++++++ src/module-elasticsuite-tracker/etc/di.xml | 5 ++ .../etc/elasticsuite_indices.xml | 3 + .../view/frontend/web/js/tracking.js | 3 +- 7 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 src/module-elasticsuite-tracker/Plugin/SessionStartCheckerPlugin.php diff --git a/src/module-elasticsuite-tracker/Helper/Data.php b/src/module-elasticsuite-tracker/Helper/Data.php index 361770217..bd2012b89 100644 --- a/src/module-elasticsuite-tracker/Helper/Data.php +++ b/src/module-elasticsuite-tracker/Helper/Data.php @@ -230,7 +230,7 @@ public function getAnonymizationDelay() } /** - * Return the tracking data retention delay, in days + * Return the tracking data retention delay, in months * * @return int */ diff --git a/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php b/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php index c44730070..16fd3863d 100644 --- a/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php +++ b/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php @@ -119,11 +119,11 @@ public function getVisitorIds(int $customerId) * * @param array $eventData Event */ - private function addCustomerLink($eventData) + private function addCustomerLink(&$eventData) { - // The customerId is set in session if the Magento_Persistent module is enabled and a persistent session exists. - if ($this->customerSession->getCustomerId() !== null) { - $customerId = $this->customerSession->getCustomerId(); + // The customerId should be sent by the frontend, if any. + $customerId = $eventData['customer']['id'] ?? null; + if ($customerId !== null && ((int) $customerId > 0)) { $sessionId = $eventData['session']['uid'] ?? null; $visitorId = $eventData['session']['vid'] ?? null; @@ -142,6 +142,7 @@ private function addCustomerLink($eventData) $this->customerLinkResource->saveLink($data); } + unset($eventData['customer']['id']); // Do not persist the customer_id in ES index to preserve anonymization. } } } diff --git a/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php b/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php index 2df256a25..0caf4ce5b 100644 --- a/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php +++ b/src/module-elasticsuite-tracker/Model/CustomerDataTrackingManager.php @@ -45,22 +45,18 @@ public function __construct(CustomerSession $customerSession) */ public function getCustomerDataToTrack() { + $variables = [ + 'group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, + ]; + if (!$this->customerSession->getId()) { - return []; + return $variables; } $customer = $this->customerSession->getCustomer(); - $shippingAddress = $customer->getDefaultShippingAddress(); + $variables['group_id'] = (int) $customer->getGroupId() ?? \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID; + $variables['id'] = (int) $customer->getId(); - $dob = new DateTime($customer->getDob() ?? ''); - $now = new DateTime(); - - return [ - 'age' => (int) $now->format('Y') - (int) $dob->format('Y'), - 'gender' => $customer->getGender(), - 'zipcode' => $shippingAddress ? $shippingAddress->getPostcode() : '', - 'state' => $shippingAddress ? $shippingAddress->getRegion() : '', - 'country' => $shippingAddress ? $shippingAddress->getCountry() : '', - ]; + return $variables; } } diff --git a/src/module-elasticsuite-tracker/Plugin/SessionStartCheckerPlugin.php b/src/module-elasticsuite-tracker/Plugin/SessionStartCheckerPlugin.php new file mode 100644 index 000000000..81e994147 --- /dev/null +++ b/src/module-elasticsuite-tracker/Plugin/SessionStartCheckerPlugin.php @@ -0,0 +1,70 @@ + + * @copyright 2024 Smile + * @license Open Software License ("OSL") v. 3.0 + */ + +namespace Smile\ElasticsuiteTracker\Plugin; + +use Magento\Framework\App\Request\Http; +use Magento\Framework\Session\SessionStartChecker; + +/** + * Prevent session creation when going through a tracker hit URL. + * Session creation can have performance issues when several ajax calls are sent in parallel. + * + * @category Smile + * @package Smile\ElasticsuiteTracker + * @author Romain Ruaud + */ +class SessionStartCheckerPlugin +{ + /** + * @var Http + */ + private $request; + + /** + * @param Http $request HTTP Request + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + */ + public function __construct( + Http $request + ) { + $this->request = $request; + } + + /** + * Prevents session starting when going through a tracker hit URL. + * + * @param SessionStartChecker $subject Session start checker + * @param bool $result Legacy result + * + * @return bool + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterCheck(SessionStartChecker $subject, bool $result): bool + { + if ($result === false) { + return false; + } + + $requestPath = trim($this->request->getPathInfo(), '/'); + + if ($requestPath === 'elasticsuite/tracker/hit/image/h.png') { + $result = false; + } elseif ($requestPath === 'rest/V1/elasticsuite-tracker/hit') { + $result = false; + } + + return $result; + } +} diff --git a/src/module-elasticsuite-tracker/etc/di.xml b/src/module-elasticsuite-tracker/etc/di.xml index 1a25f59f1..0c1ffe92e 100644 --- a/src/module-elasticsuite-tracker/etc/di.xml +++ b/src/module-elasticsuite-tracker/etc/di.xml @@ -117,4 +117,9 @@ + + + + + diff --git a/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml b/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml index 5cf078b30..0497bbe34 100644 --- a/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml +++ b/src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml @@ -29,6 +29,9 @@ + + + diff --git a/src/module-elasticsuite-tracker/view/frontend/web/js/tracking.js b/src/module-elasticsuite-tracker/view/frontend/web/js/tracking.js index 2bc736e18..cbbda5c6e 100644 --- a/src/module-elasticsuite-tracker/view/frontend/web/js/tracking.js +++ b/src/module-elasticsuite-tracker/view/frontend/web/js/tracking.js @@ -175,7 +175,7 @@ const smileTracker = (function () { } function getCustomerDataCodeToTrack() { - return ['age', 'gender', 'zipcode', 'state', 'country']; + return ['id', 'group_id', 'company_id']; } function setTrackerStyle(imgNode) { @@ -186,6 +186,7 @@ const smileTracker = (function () { // Append a transparent pixel to the body function sendTag(forceCollect = false) { initSession.bind(this)(); + initCustomerData.bind(this)(); if (this.config && this.config.hasOwnProperty('storeId')) { addPageVar.bind(this)('store_id', this.config.storeId); From 3ddfdb96454cd2d84826d3255fc0ef7871178cdd Mon Sep 17 00:00:00 2001 From: Romain Ruaud Date: Tue, 29 Oct 2024 10:31:57 +0100 Subject: [PATCH 2/2] Ensure to proceed customer link before going into DB --- .../Model/Customer/TrackingService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php b/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php index 16fd3863d..087c625c6 100644 --- a/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php +++ b/src/module-elasticsuite-tracker/Model/Customer/TrackingService.php @@ -76,8 +76,8 @@ public function hit($eventData): void public function addEvent($eventData) { if ($this->helper->isEnabled()) { - $this->eventQueue->addEvent($eventData); $this->addCustomerLink($eventData); + $this->eventQueue->addEvent($eventData); } }