Skip to content

Commit 9df6d11

Browse files
authored
Merge pull request #1445 from algolia/update/MAGE-798
UseCookie Update for insight
2 parents 3b54b49 + 037122f commit 9df6d11

File tree

12 files changed

+206
-24
lines changed

12 files changed

+206
-24
lines changed

Block/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ public function getConfiguration()
288288
'urls' => [
289289
'logo' => $this->getViewFileUrl('Algolia_AlgoliaSearch::images/algolia-logo-blue.svg'),
290290
],
291+
'cookieConfiguration' => [
292+
'consentCookieName' => $config->getDefaultConsentCookieName(),
293+
'cookieAllowButtonSelector' => $config->getAllowCookieButtonSelector(),
294+
'cookieRestrictionModeEnabled' => $config->isCookieRestrictionModeEnabled(),
295+
'cookieDuration' =>$config->getAlgoliaCookieDuration()
296+
],
291297
'ccAnalytics' => [
292298
'enabled' => $config->isClickConversionAnalyticsEnabled(),
293299
'ISSelector' => $config->getClickConversionAnalyticsISSelector(),

Helper/ConfigHelper.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Serialize\SerializerInterface;
1212
use Magento\Store\Model\ScopeInterface;
1313
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Cookie\Helper\Cookie as CookieHelper;
1415

1516
class ConfigHelper
1617
{
@@ -23,6 +24,10 @@ class ConfigHelper
2324
public const API_KEY = 'algoliasearch_credentials/credentials/api_key';
2425
public const SEARCH_ONLY_API_KEY = 'algoliasearch_credentials/credentials/search_only_api_key';
2526
public const INDEX_PREFIX = 'algoliasearch_credentials/credentials/index_prefix';
27+
public const COOKIE_DEFAULT_CONSENT_COOKIE_NAME = 'algoliasearch_credentials/algolia_cookie_configuration/default_consent_cookie_name';
28+
public const ALLOW_COOKIE_BUTTON_SELECTOR = 'algoliasearch_credentials/algolia_cookie_configuration/allow_cookie_button_selector';
29+
public const ALGOLIA_COOKIE_DURATION = 'algoliasearch_credentials/algolia_cookie_configuration/cookie_duration';
30+
2631

2732
public const IS_INSTANT_ENABLED = 'algoliasearch_instant/instant/is_instant_enabled';
2833
public const REPLACE_CATEGORIES = 'algoliasearch_instant/instant/replace_categories';
@@ -185,6 +190,11 @@ class ConfigHelper
185190
*/
186191
protected $groupCollection;
187192

193+
/**
194+
* @var CookieHelper
195+
*/
196+
protected $cookieHelper;
197+
188198
/**
189199
* @param Magento\Framework\App\Config\ScopeConfigInterface $configInterface
190200
* @param StoreManagerInterface $storeManager
@@ -196,6 +206,7 @@ class ConfigHelper
196206
* @param Magento\Framework\Event\ManagerInterface $eventManager
197207
* @param SerializerInterface $serializer
198208
* @param GroupCollection $groupCollection
209+
* @param CookieHelper $cookieHelper
199210
*/
200211
public function __construct(
201212
Magento\Framework\App\Config\ScopeConfigInterface $configInterface,
@@ -207,7 +218,8 @@ public function __construct(
207218
Magento\Framework\App\ProductMetadataInterface $productMetadata,
208219
Magento\Framework\Event\ManagerInterface $eventManager,
209220
SerializerInterface $serializer,
210-
GroupCollection $groupCollection
221+
GroupCollection $groupCollection,
222+
CookieHelper $cookieHelper
211223
) {
212224
$this->configInterface = $configInterface;
213225
$this->currency = $currency;
@@ -219,6 +231,7 @@ public function __construct(
219231
$this->eventManager = $eventManager;
220232
$this->serializer = $serializer;
221233
$this->groupCollection = $groupCollection;
234+
$this->cookieHelper = $cookieHelper;
222235
}
223236

224237

@@ -989,7 +1002,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
9891002
if (!$attrs){
9901003
$attrs = $this->getSorting($storeId);
9911004
}
992-
1005+
9931006
$currency = $this->getCurrencyCode($storeId);
9941007
$attributesToAdd = [];
9951008
foreach ($attrs as $key => $attr) {
@@ -1567,6 +1580,45 @@ public function getConversionAnalyticsAddToCartSelector($storeId = null)
15671580
return $this->configInterface->getValue(self::CC_ADD_TO_CART_SELECTOR, ScopeInterface::SCOPE_STORE, $storeId);
15681581
}
15691582

1583+
/**
1584+
* @param $storeId
1585+
* @return mixed
1586+
*/
1587+
public function getDefaultConsentCookieName($storeId = null)
1588+
{
1589+
return $this->configInterface->getValue(
1590+
self::COOKIE_DEFAULT_CONSENT_COOKIE_NAME,
1591+
ScopeInterface::SCOPE_STORE,
1592+
$storeId
1593+
);
1594+
}
1595+
1596+
/**
1597+
* @param $storeId
1598+
* @return mixed
1599+
*/
1600+
public function getAllowCookieButtonSelector($storeId = null)
1601+
{
1602+
return $this->configInterface->getValue(
1603+
self::ALLOW_COOKIE_BUTTON_SELECTOR,
1604+
ScopeInterface::SCOPE_STORE,
1605+
$storeId
1606+
);
1607+
}
1608+
1609+
/**
1610+
* @param $storeId
1611+
* @return mixed
1612+
*/
1613+
public function getAlgoliaCookieDuration($storeId = null)
1614+
{
1615+
return $this->configInterface->getValue(
1616+
self::ALGOLIA_COOKIE_DURATION,
1617+
ScopeInterface::SCOPE_STORE,
1618+
$storeId
1619+
);
1620+
}
1621+
15701622
/**
15711623
* @param $storeId
15721624
* @return array
@@ -1699,4 +1751,12 @@ public function isAutocompleteNavigatorEnabled($storeId = null)
16991751
$storeId
17001752
);
17011753
}
1754+
1755+
/**
1756+
* @return bool
1757+
*/
1758+
public function isCookieRestrictionModeEnabled()
1759+
{
1760+
return (bool)$this->cookieHelper->isCookieRestrictionModeEnabled();
1761+
}
17021762
}

Helper/Configuration/NoticeHelper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
4444
'getClickAnalyticsNotice',
4545
'getPersonalizationNotice',
4646
'getRecommendNotice',
47+
'getCookieConfigurationNotice',
4748
];
4849

4950
/** @var array[] */
@@ -207,6 +208,27 @@ protected function getClickAnalyticsNotice()
207208
'message' => $noticeContent,
208209
];
209210
}
211+
212+
protected function getCookieConfigurationNotice()
213+
{
214+
$noticeContent = '';
215+
$selector = '';
216+
$method = 'after';
217+
218+
// If the feature is enabled in the Algolia dashboard but not activated on the Magento Admin
219+
$noticeContent = '
220+
<div class="algolia_block_cookie">
221+
Find out more about Algolia Cookie Configuration in <a href="https://www.algolia.com/doc/integration/magento-2/how-it-works/analytics-overview/?client=php#algolia-cookie-configuration?utm_source=magento&utm_medium=extension&utm_campaign=magento_2&utm_term=shop-owner&utm_content=doc-link" target="_blank">documentation</a>.
222+
</div>';
223+
$selector = '#algoliasearch_credentials_algolia_cookie_configuration';
224+
$method = 'after';
225+
226+
$this->notices[] = [
227+
'selector' => $selector,
228+
'method' => $method,
229+
'message' => $noticeContent,
230+
];
231+
}
210232

211233
protected function getPersonalizationNotice()
212234
{

Helper/Entity/ProductHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
452452
* Handle replicas
453453
*/
454454
$sortingIndices = $this->configHelper->getSortingIndices($indexName, $storeId);
455-
455+
456456
$replicas = [];
457457

458458
if ($this->configHelper->isInstantEnabled($storeId)) {
@@ -483,7 +483,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic
483483
$this->logger->log('Setting replicas to "' . $indexName . '" index.');
484484
$this->logger->log('Replicas: ' . json_encode($replicas));
485485
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
486-
486+
487487
if (!$this->configHelper->useVirtualReplica($storeId)) {
488488
foreach ($sortingIndices as $values) {
489489
$replicaName = $values['name'];
@@ -574,7 +574,7 @@ public function getObject(Product $product)
574574
);
575575

576576
$defaultData = $transport->getData();
577-
577+
578578
$visibility = $product->getVisibility();
579579

580580
$visibleInCatalog = $this->visibility->getVisibleInCatalogIds();
@@ -1056,7 +1056,7 @@ protected function addAdditionalAttributes($customData, $additionalAttributes, P
10561056
}
10571057

10581058
$attributeResource = $attributeResource->setData('store_id', $product->getStoreId());
1059-
1059+
10601060
$value = $product->getData($attributeName);
10611061

10621062
if ($value !== null) {

Helper/InsightsHelper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,10 @@ public function setUserToken(Customer $customer)
171171
return $userToken;
172172
}
173173

174+
/**
175+
* @return string|null
176+
*/
177+
public function getUserAllowedSavedCookie() {
178+
return $this->configHelper->isCookieRestrictionModeEnabled() ? !!$this->cookieManager->getCookie($this->configHelper->getDefaultConsentCookieName()) : true;
179+
}
174180
}

Observer/Insights/CheckoutCartProductAddAfter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function execute(Observer $observer)
6767
$product = $observer->getEvent()->getProduct();
6868
$storeId = $quoteItem->getStoreId();
6969

70-
if (!$this->insightsHelper->isAddedToCartTracked($storeId) && !$this->insightsHelper->isOrderPlacedTracked($storeId)) {
70+
if (!$this->insightsHelper->isAddedToCartTracked($storeId) && !$this->insightsHelper->isOrderPlacedTracked($storeId) || !$this->insightsHelper->getUserAllowedSavedCookie()) {
7171
return;
7272
}
7373

etc/adminhtml/system.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,39 @@
110110
</comment>
111111
</field>
112112
</group>
113+
<group id="algolia_cookie_configuration" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
114+
<label>Algolia Cookie Configuration</label>
115+
<attribute type="expanded">1</attribute>
116+
<comment>
117+
<![CDATA[
118+
If your Magento cookie settings, specifically <b>General > Web > Default Cookie Settings > Cookie Restriction Mode</b> is set to "No," we will consider it as Implicit Cookie Consent. In this case, the useCookie will be set to True by default for all insight events. Conversely, if <b>Cookie Restriction Mode is set to 'Yes,'</b> Insight events will not be allowed without explicit cookie consent.
119+
]]>
120+
</comment>
121+
<field id="default_consent_cookie_name" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
122+
<label>Consent Cookie Name</label>
123+
<comment>
124+
<![CDATA[
125+
Enter the name for your website's consent cookie.
126+
]]>
127+
</comment>
128+
</field>
129+
<field id="allow_cookie_button_selector" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
130+
<label>Allow Cookie Button Selector</label>
131+
<comment>
132+
<![CDATA[
133+
Enter Allow Cookie Button Selector.
134+
]]>
135+
</comment>
136+
</field>
137+
<field id="cookie_duration" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
138+
<label>Algolia Cookie Duration</label>
139+
<comment>
140+
<![CDATA[
141+
Add Cookie Duration in milliseconds. Default is 15552000000 (6 Months).
142+
]]>
143+
</comment>
144+
</field>
145+
</group>
113146
</section>
114147
<section id="algoliasearch_autocomplete" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
115148
<label>Autocomplete Menu</label>

etc/config.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
<enable_query_suggestions_index>1</enable_query_suggestions_index>
1313
<enable_pages_index>1</enable_pages_index>
1414
</credentials>
15+
<algolia_cookie_configuration>
16+
<default_consent_cookie_name>user_allowed_save_cookie</default_consent_cookie_name>
17+
<allow_cookie_button_selector>#btn-cookie-allow</allow_cookie_button_selector>
18+
<cookie_duration>15552000000</cookie_duration>
19+
</algolia_cookie_configuration>
1520
</algoliasearch_credentials>
1621
<algoliasearch_autocomplete>
1722
<autocomplete>
@@ -46,6 +51,11 @@
4651
<category_separator> /// </category_separator>
4752
</categories>
4853
</algoliasearch_categories>
54+
<algoliasearch_cc_analytics>
55+
<cc_analytics_group>
56+
<default_consent_cookie_name>user_allowed_save_cookie</default_consent_cookie_name>
57+
</cc_analytics_group>
58+
</algoliasearch_cc_analytics>
4959
<algoliasearch_personalization>
5060
<personalization_group>
5161
<personalization_click_events_group>

view/frontend/requirejs-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var config = {
3434
mixins: {
3535
'Magento_Catalog/js/catalog-add-to-cart': {
3636
'Algolia_AlgoliaSearch/insights/add-to-cart-mixin': true
37-
},
37+
}
3838
}
3939
}
4040
};

view/frontend/web/autocomplete.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,15 @@ define(
543543
const queryId = $this.attr('data-queryId');
544544
const position = $this.attr('data-position');
545545

546-
const eventData = algoliaInsights.buildEventData(
547-
'Clicked', objectId, indexName, position, queryId
548-
);
549-
algoliaInsights.trackClick(eventData);
550-
$this.attr('data-clicked', true);
546+
let useCookie = algoliaConfig.cookieConfiguration.cookieRestrictionModeEnabled ? !!getCookie(algoliaConfig.cookieConfiguration.consentCookieName) : true;
547+
if (useCookie !== false) {
548+
algoliaInsights.initializeAnalytics();
549+
const eventData = algoliaInsights.buildEventData(
550+
'Clicked', objectId, indexName, position, queryId
551+
);
552+
algoliaInsights.trackClick(eventData);
553+
$this.attr('data-clicked', true);
554+
}
551555
});
552556
}
553557

0 commit comments

Comments
 (0)