Skip to content

Commit

Permalink
Merge branch '2.10.x' into 2.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Oct 29, 2024
2 parents ae1c68b + f584cd0 commit 76a253d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/module-elasticsuite-tracker/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;

Expand All @@ -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.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Romain Ruaud <[email protected]>
* @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 <[email protected]>
*/
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;
}
}
5 changes: 5 additions & 0 deletions src/module-elasticsuite-tracker/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@
</arguments>
</type>

<!-- Prevent session start for tracking urls -->
<type name="Magento\Framework\Session\SessionStartChecker">
<plugin name="elasticsuite_tracker_disable_session" type="Smile\ElasticsuiteTracker\Plugin\SessionStartCheckerPlugin"/>
</type>

</config>
3 changes: 3 additions & 0 deletions src/module-elasticsuite-tracker/etc/elasticsuite_indices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<field name="session.uid" type="keyword" />
<field name="session.vid" type="keyword" />

<!-- Customer data -->
<field name="customer.group_id" type="integer" />

<!-- Page metadata -->
<field name="page.site" type="keyword" />
<field name="page.store_id" type="integer" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const smileTracker = (function () {
}

function getCustomerDataCodeToTrack() {
return ['age', 'gender', 'zipcode', 'state', 'country'];
return ['id', 'group_id', 'company_id'];
}

function setTrackerStyle(imgNode) {
Expand All @@ -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);
Expand Down

0 comments on commit 76a253d

Please sign in to comment.