Skip to content

Commit 3c5e3bc

Browse files
committed
add check customer cookie
1 parent 4a23735 commit 3c5e3bc

File tree

7 files changed

+157
-6
lines changed

7 files changed

+157
-6
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form;
10+
11+
use Magento\Config\Block\System\Config\Form\Field;
12+
use Magento\Framework\Data\Form\Element\AbstractElement;
13+
14+
class ProtectCustomerData extends Field
15+
{
16+
/**
17+
* Render protect customer data
18+
*
19+
* @param AbstractElement $element
20+
* @return string
21+
*/
22+
public function render(AbstractElement $element): string
23+
{
24+
$url = $this->getUrl('*/*/*/section/web');
25+
$comment = 'When enabled, data won\'t be sent to Google, until the customer allows cookies.<br/><br/>
26+
<strong>Note</strong>, that this option will work only when Cookie Restriction Mode at
27+
<a href="' . $url . '" target="_blank">Stores > Configuration > General > Web > Default Cookie Settings</a>
28+
is enabled.';
29+
30+
$element->setComment($comment);
31+
return parent::render($element);
32+
}
33+
}

Block/GtmCode.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magefan\GoogleTagManager\Block;
1010

11+
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Framework\View\Element\Template;
1213
use Magefan\GoogleTagManager\Model\Config;
1314

@@ -44,6 +45,27 @@ public function getPublicId(): string
4445
return $this->config->getPublicId();
4546
}
4647

48+
/**
49+
* Check if protect customer data is enabled
50+
*
51+
* @return bool
52+
*/
53+
public function isProtectCustomerDataEnabled(): bool
54+
{
55+
return $this->config->isProtectCustomerDataEnabled();
56+
}
57+
58+
/**
59+
* Get current website ID
60+
*
61+
* @return int
62+
* @throws NoSuchEntityException
63+
*/
64+
public function getWebsiteId(): int
65+
{
66+
return (int)$this->_storeManager->getStore()->getWebsiteId();
67+
}
68+
4769
/**
4870
* Init GTM datalayer
4971
*

Model/Config.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Config\Model\Config\Backend\Admin\Custom;
1314

1415
class Config
1516
{
@@ -33,6 +34,11 @@ class Config
3334
public const XML_PATH_ATTRIBUTES_PRODUCT = 'mfgoogletagmanager/attributes/product';
3435
public const XML_PATH_ATTRIBUTES_BRAND = 'mfgoogletagmanager/attributes/brand';
3536

37+
/**
38+
* Customer data protection regulation config
39+
*/
40+
public const XML_PATH_PROTECT_CUSTOMER_DATA = 'mfgoogletagmanager/customer_data/protect';
41+
3642
/**
3743
* @var ScopeConfigInterface
3844
*/
@@ -138,6 +144,29 @@ public function getBrandAttribute(string $storeId = null): string
138144
return (string)$this->getConfig(self::XML_PATH_ATTRIBUTES_BRAND, $storeId);
139145
}
140146

147+
/**
148+
* Retrieve true if protect customer data is enabled
149+
*
150+
* @param string|null $storeId
151+
* @return bool
152+
*/
153+
public function isProtectCustomerDataEnabled(string $storeId = null): bool
154+
{
155+
return (bool)$this->getConfig(self::XML_PATH_PROTECT_CUSTOMER_DATA, $storeId) &&
156+
$this->isCookieRestrictionModeEnabled($storeId);
157+
}
158+
159+
/**
160+
* Retrieve true if cookie restriction mode enabled
161+
*
162+
* @param string|null $storeId
163+
* @return bool
164+
*/
165+
public function isCookieRestrictionModeEnabled(string $storeId = null)
166+
{
167+
return (bool)$this->getConfig(Custom::XML_PATH_WEB_COOKIE_RESTRICTION, $storeId);
168+
}
169+
141170
/**
142171
* Retrieve store config value
143172
*

etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hid
104104
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\Button</frontend_model>
105105
</field>
106106
</group>
107+
<group id="customer_data" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
108+
<label>GDPR/CCPA/LGPD (Customer Data Protection Regulation)</label>
109+
<field id="protect" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
110+
<label>Protect Customer Data</label>
111+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
112+
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\ProtectCustomerData</frontend_model>
113+
</field>
114+
</group>
107115
</section>
108116
</system>
109117
</config>

etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<product>sku</product>
2525
<brand/>
2626
</attributes>
27+
<customer_data>
28+
<protect>1</protect>
29+
</customer_data>
2730
</mfgoogletagmanager>
2831
</default>
2932
</config>

view/frontend/templates/js_code.phtml

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,63 @@ if (!isset($escaper)) {
1313
}
1414
?>
1515
<!-- Google Tag Manager -->
16-
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
17-
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
18-
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
19-
'https:<?= '/' . '/' ?>www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
20-
})(window,document,'script','dataLayer','<?= $escaper->escapeHtml($block->getPublicId()) ?>');</script>
21-
<!-- End Google Tag Manager -->
16+
<script>
17+
function mfLoadGtm() {
18+
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
19+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
20+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
21+
'https:<?= '/' . '/' ?>www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
22+
})(window,document,'script','dataLayer','<?= $escaper->escapeHtml($block->getPublicId()) ?>');
23+
}
24+
25+
<?php if ($block->isProtectCustomerDataEnabled()) { ?>
26+
document.addEventListener('DOMContentLoaded', function () {
27+
function getCookieValue(cookieName) {
28+
let name = cookieName + '=';
29+
let cookieSplit = document.cookie.split(';');
30+
31+
for (let i = 0; i < cookieSplit.length; i++) {
32+
let a = cookieSplit[i];
33+
34+
while (a.charAt(0) === ' ') {
35+
a = a.substring(1);
36+
}
37+
38+
if (a.indexOf(name) === 0) {
39+
return a.substring(name.length, a.length);
40+
}
41+
}
42+
return '';
43+
}
44+
45+
function customerDataAllowed() {
46+
let cookie = getCookieValue(
47+
'<?= $escaper->escapeHtml(\Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE) ?>'
48+
);
49+
if (cookie) {
50+
cookie = JSON.parse(decodeURIComponent(cookie));
51+
if (cookie[<?= $escaper->escapeHtml($block->getWebsiteId()) ?>]) {
52+
return true;
53+
}
54+
}
55+
56+
return false
57+
}
58+
59+
if (customerDataAllowed()) {
60+
mfLoadGtm();
61+
} else {
62+
let interval = setInterval(function () {
63+
if (!customerDataAllowed()) return;
64+
clearInterval(interval);
65+
mfLoadGtm();
66+
}, 1000);
67+
}
68+
});
69+
70+
<?php } else { ?>
71+
mfLoadGtm();
72+
<?php } ?>
73+
74+
</script>
75+
<!-- End Google Tag Manager -->

view/frontend/templates/no_js_code.phtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ if (!isset($escaper)) {
1313
}
1414
?>
1515
<!-- Google Tag Manager (noscript) -->
16+
<?php if (!$block->isProtectCustomerDataEnabled()) { ?>
1617
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?= $escaper->escapeHtml($block->getPublicId()) ?>"
1718
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
19+
<?php } ?>
1820
<!-- End Google Tag Manager (noscript) -->

0 commit comments

Comments
 (0)