Skip to content

Commit 41012ad

Browse files
authored
Merge pull request #4 from ihormandzyuk/8567-GTM-with-default-magento-cookie
add check customer cookie
2 parents 998444d + 0659f91 commit 41012ad

File tree

7 files changed

+156
-6
lines changed

7 files changed

+156
-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: 28 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
{
@@ -34,6 +35,11 @@ class Config
3435
public const XML_PATH_ATTRIBUTES_BRAND = 'mfgoogletagmanager/attributes/brand';
3536
public const XML_PATH_ATTRIBUTES_CATEGORIES = 'mfgoogletagmanager/attributes/categories';
3637

38+
/**
39+
* Customer data protection regulation config
40+
*/
41+
public const XML_PATH_PROTECT_CUSTOMER_DATA = 'mfgoogletagmanager/customer_data/protect';
42+
3743
/**
3844
* @var ScopeConfigInterface
3945
*/
@@ -140,6 +146,28 @@ public function getBrandAttribute(string $storeId = null): string
140146
}
141147

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

etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hid
112112
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\Button</frontend_model>
113113
</field>
114114
</group>
115+
<group id="customer_data" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
116+
<label>GDPR/CCPA/LGPD (Customer Data Protection Regulation)</label>
117+
<field id="protect" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
118+
<label>Protect Customer Data</label>
119+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
120+
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\ProtectCustomerData</frontend_model>
121+
</field>
122+
</group>
115123
</section>
116124
</system>
117125
</config>

etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
<brand/>
2626
<categories/>
2727
</attributes>
28+
<customer_data>
29+
<protect>1</protect>
30+
</customer_data>
2831
</mfgoogletagmanager>
2932
</default>
3033
</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)