Skip to content

Commit

Permalink
Merge pull request #890 from afoucret/feature_gdpr_25
Browse files Browse the repository at this point in the history
GDPR implementation
  • Loading branch information
afoucret authored Apr 26, 2018
2 parents 3edd2b6 + e608329 commit 3abe964
Show file tree
Hide file tree
Showing 25 changed files with 739 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2018 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteTracker\Api;

/**
* Customer Tracking Service interface.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
interface CustomerTrackingServiceInterface
{
/**
* Add a tracked event.
*
* @param array $eventData The event Data
*
* @return mixed
*/
public function addEvent($eventData);

/**
* Anonymize customer data for a given customer Id and a delay.
* If the delay is null, the anonymization process is run immediately.
*
* @param int $customerId The customer Id.
* @param \DateTime|null $delay The date after which all the customer data must be cleared.
*
* @return void
*/
public function anonymizeCustomerData(int $customerId, \DateTime $delay = null);

/**
* Process cleaning of all expired customer data.
*
* @return void
*/
public function deleteExpired();

/**
* Retrieve all visitor ids matching a given customer
*
* @param int $customerId The customer id
*
* @return array
*/
public function getVisitorIds(int $customerId);
}
39 changes: 36 additions & 3 deletions src/module-elasticsuite-tracker/Block/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
class Config extends \Magento\Framework\View\Element\Template
{
/**
* The default tracking consent script, used as a fallback if none defined in layout.
*/
const DEFAULT_CONSENT_SCRIPT = 'Smile_ElasticsuiteTracker/js/user-consent';

/**
* Magento Configuration
*
Expand All @@ -43,6 +48,13 @@ class Config extends \Magento\Framework\View\Element\Template
*/
private $trackerHelper;

/**
* Javascript script that will handle User consent.
*
* @var string
*/
private $userConsentScript;

/**
* PHP Constructor
*
Expand All @@ -59,9 +71,10 @@ public function __construct(
) {
parent::__construct($context, $data);

$this->scopeConfig = $context->getScopeConfig();
$this->jsonHelper = $jsonHelper;
$this->trackerHelper = $trackerHelper;
$this->scopeConfig = $context->getScopeConfig();
$this->jsonHelper = $jsonHelper;
$this->trackerHelper = $trackerHelper;
$this->userConsentScript = $data['userConsentScript'] ?? self::DEFAULT_CONSENT_SCRIPT;
}

/**
Expand Down Expand Up @@ -116,4 +129,24 @@ public function getStoreId()
{
return $this->trackerHelper->getStoreId();
}

/**
* Return the JS script to be used to check if user did consent tracking.
*
* @return string
*/
public function getUserConsentScript()
{
return $this->userConsentScript;
}

/**
* Get config passed to the user consent script.
*
* @return array
*/
public function getUserConsentConfig()
{
return $this->getData('userConsentConfig') ?? [];
}
}
13 changes: 7 additions & 6 deletions src/module-elasticsuite-tracker/Block/Variables/Page/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public function getVariables()
$order = $order = $this->checkoutSession->getLastRealOrder();

if ($order) {
$variables['order.id'] = $order->getIncrementId();
$variables['order.subtotal'] = $order->getBaseSubtotalInclTax();
$variables['order.discount_total'] = $order->getDiscountAmount();
$variables['order.shipping_total'] = $order->getShippingAmount();
Expand All @@ -75,8 +74,9 @@ public function getVariables()
$variables['order.payment_method'] = $order->getPayment()->getMethod();
$variables['order.salesrules'] = $order->getAppliedRuleIds();

$itemId = 0;
foreach ($order->getAllItems() as $item) {
$variables = array_merge($variables, $this->getOrderItemVariables($item));
$variables = array_merge($variables, $this->getOrderItemVariables($item, $itemId++));
}
}

Expand All @@ -86,16 +86,16 @@ public function getVariables()
/**
* Retrieve tracking variables for an order item
*
* @param \Magento\Sales\Model\Order\Item $item The order item
* @param \Magento\Sales\Model\Order\Item $item The order item
* @param int $itemId The order item id, dynamically generated.
*
* @return array
*/
private function getOrderItemVariables($item)
private function getOrderItemVariables($item, $itemId)
{
$variables = [];

if (!$item->isDummy()) {
$itemId = $item->getId();
$prefix = "order.items.$itemId";
$variables[$prefix . '.sku'] = $item->getSku();
$variables[$prefix . '.product_id'] = $item->getProductId();
Expand All @@ -105,7 +105,8 @@ private function getOrderItemVariables($item)
$variables[$prefix . '.label'] = $item->getName();
$variables[$prefix . '.salesrules'] = $item->getAppliedRuleIds();

if ($product = $item->getProduct()) {
$product = $item->getProduct();
if ($product) {
$categoriesId = $product->getCategoryIds();
if (count($categoriesId)) {
$variables[$prefix . '.category_ids'] = implode(",", $categoriesId);
Expand Down
16 changes: 9 additions & 7 deletions src/module-elasticsuite-tracker/Controller/Tracker/Hit.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ class Hit extends \Magento\Framework\App\Action\Action
const PIXEL = '6wzwc+flkuJiYGDg9fRwCQLSjCDMwQQkJ5QH3wNSbCVBfsEMYJC3jH0ikOLxdHEMqZiTnJCQAOSxMDB+E7cIBcl7uvq5rHNKaAIA';

/**
* @var \Smile\ElasticsuiteTracker\Api\EventQueueInterface
* @var \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface
*/
private $logEventQueue;
private $trackingService;

/**
* Constructor.
*
* @param \Magento\Framework\App\Action\Context $context Context.
* @param \Smile\ElasticsuiteTracker\Api\EventQueueInterface $logEventQueue Event queue.
* @param \Magento\Framework\App\Action\Context $context Context.
* @param \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $trackingService Tracking Service.
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Smile\ElasticsuiteTracker\Api\EventQueueInterface $logEventQueue
\Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $trackingService
) {
parent::__construct($context);
$this->logEventQueue = $logEventQueue;
$this->trackingService = $trackingService;
}

/**
Expand All @@ -56,7 +56,9 @@ public function execute()
$this->getResponse()->setHeader('Content-Type', 'image/png');
$this->getResponse()->sendResponse();

$this->logEventQueue->addEvent($this->decodeParams($this->getRequest()->getParams()));
$eventData = $this->decodeParams($this->getRequest()->getParams());

$this->trackingService->addEvent($eventData);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions src/module-elasticsuite-tracker/Cron/ExpireCustomerLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2017 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteTracker\Cron;

/**
* Cronjob that will anonymize log events that have reached their "delete_after" date if the anonymization is enabled.
*
* @category Smile
* @package Smile\ElasticsuiteTracker
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class ExpireCustomerLink
{
/**
* @var \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface
*/
private $trackingService;

/**
* @var \Smile\ElasticsuiteTracker\Helper\Data
*/
private $helper;

/**
* Constructor.
*
* @param \Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $trackingService Tracking service.
* @param \Smile\ElasticsuiteTracker\Helper\Data $helper Tracking helper.
*/
public function __construct(
\Smile\ElasticsuiteTracker\Api\CustomerTrackingServiceInterface $trackingService,
\Smile\ElasticsuiteTracker\Helper\Data $helper
) {
$this->trackingService = $trackingService;
$this->helper = $helper;
}

/**
* Perform the cleaning of the expired entries if necessary and if anonymization is enabled.
*/
public function execute()
{
if ($this->helper->isAnonymizationEnabled()) {
$this->trackingService->deleteExpired();
}
}
}
57 changes: 51 additions & 6 deletions src/module-elasticsuite-tracker/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
*/
const CONFIG_COOKIE = 'smile_elasticsuite_tracker/session';

/**
* Anonymization status configuration path
* @var string
*/
const CONFIG_IS_ANONYMIZATION_ENABLED_XPATH = 'smile_elasticsuite_tracker/anonymization/enabled';

/**
* Anonymization delay configuration path
* @var string
*/
const CONFIG_ANONYMIZATION_DELAY_XPATH = 'smile_elasticsuite_tracker/anonymization/delay';

/**
* Magento Configuration
*
Expand All @@ -53,18 +65,26 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
*/
private $urlBuilder;

/**
* @var \Magento\Framework\Session\SessionManagerInterface
*/
private $sessionManager;

/**
* PHP Constructor
*
* @param \Magento\Framework\App\Helper\Context $context The current context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager The Store Manager
* @param \Magento\Framework\App\Helper\Context $context The current context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager The Store Manager
* @param \Magento\Framework\Session\SessionManagerInterface $sessionManager Session Manager
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Session\SessionManagerInterface $sessionManager
) {
$this->urlBuilder = $context->getUrlBuilder();
$this->storeManager = $storeManager;
$this->urlBuilder = $context->getUrlBuilder();
$this->storeManager = $storeManager;
$this->sessionManager = $sessionManager;
parent::__construct($context);
}

Expand Down Expand Up @@ -95,7 +115,11 @@ public function getBaseUrl()
*/
public function getCookieConfig()
{
return $this->scopeConfig->getValue(self::CONFIG_COOKIE);
$config = $this->scopeConfig->getValue(self::CONFIG_COOKIE);
$config['domain'] = $this->sessionManager->getCookieDomain();
$config['path'] = $this->sessionManager->getCookiePath();

return $config;
}

/**
Expand All @@ -107,4 +131,25 @@ public function getStoreId()
{
return $this->storeManager->getStore()->getId();
}


/**
* Check if Anonymization is enabled.
*
* @return bool
*/
public function isAnonymizationEnabled()
{
return $this->scopeConfig->isSetFlag(self::CONFIG_IS_ANONYMIZATION_ENABLED_XPATH);
}

/**
* Retrieve anonymization delay (in days).
*
* @return int
*/
public function getAnonymizationDelay()
{
return (int) $this->scopeConfig->getValue(self::CONFIG_ANONYMIZATION_DELAY_XPATH);
}
}
Loading

0 comments on commit 3abe964

Please sign in to comment.