Skip to content

Commit 081b266

Browse files
committed
Improvements for Google Consent Mode
1 parent 6531b99 commit 081b266

File tree

6 files changed

+61
-20
lines changed

6 files changed

+61
-20
lines changed

Block/Adminhtml/System/Config/Form/ProtectCustomerData.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ class ProtectCustomerData extends Field
2222
public function render(AbstractElement $element): string
2323
{
2424
$url = $this->getUrl('*/*/*/section/web');
25-
$comment = 'When enabled, data won\'t be sent to Google, until the customer provide consent.<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.';
25+
$comment = '
26+
<strong>Note</strong>: this option works only when default Magento Cookie Restriction Mode is enabled at
27+
<a href="' . $url . '" target="_blank">Stores > Configuration > General > Web > Default Cookie Settings</a>.
28+
If Magento Cookie Restriction Mode is disabled, then GTM JavaScript will be loaded before consent ignoring the "Load GTM Script Before Consent" option.<br/><br/>
29+
Even if GTM JavaScript is loaded before customer`s consent,
30+
GTM still waits for consent to send user data related to advertising and analytics.';
2931

3032
$element->setComment($comment);
3133
return parent::render($element);

Block/GtmCode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ public function isLoadBeforeConsent(): bool
9494
return $this->config->isLoadBeforeConsent();
9595
}
9696

97+
/**
98+
* Retrieve true if cookie restriction mode enabled
99+
*
100+
* @return bool
101+
*/
102+
public function isCookieRestrictionModeEnabled()
103+
{
104+
return $this->config->isCookieRestrictionModeEnabled();
105+
}
106+
97107
/**
98108
* Get current website ID
99109
*

Model/Config.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ public function getBrandAttribute(string $storeId = null): string
231231
*/
232232
public function isProtectCustomerDataEnabled(string $storeId = null): bool
233233
{
234-
return (bool)$this->getConfig(self::XML_PATH_PROTECT_CUSTOMER_DATA, $storeId) &&
235-
$this->isCookieRestrictionModeEnabled($storeId);
234+
return (bool)$this->getConfig(self::XML_PATH_PROTECT_CUSTOMER_DATA, $storeId);
236235
}
237236

238237
/**
@@ -243,8 +242,8 @@ public function isProtectCustomerDataEnabled(string $storeId = null): bool
243242
*/
244243
public function isLoadBeforeConsent(string $storeId = null): bool
245244
{
246-
return (bool)$this->getConfig(self::XML_PATH_LOAD_BEFORE_CONSENT, $storeId) &&
247-
$this->isCookieRestrictionModeEnabled($storeId);
245+
return $this->getConfig(self::XML_PATH_LOAD_BEFORE_CONSENT, $storeId) ||
246+
!$this->isCookieRestrictionModeEnabled($storeId);
248247
}
249248

250249

etc/adminhtml/system.xml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,29 @@ To create a new secret, navigate in the
373373
<group id="customer_data" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
374374
<label>GDPR/CCPA/LGPD (Customer Data Protection Regulation)</label>
375375
<field id="protect" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
376-
<label>Protect Customer Data</label>
376+
<label>Enable Google Consent Mode</label>
377377
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
378-
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\ProtectCustomerData</frontend_model>
378+
<comment><![CDATA[
379+
<p>If set to "Yes" Google Tag Manager Extension will set consent as "denied" by default for measurement capabilities. For example:
380+
<pre style="border: 1px dashed #5f5f5f;padding: 5px;background: #f4f4f4;">
381+
gtag('consent', 'default', {
382+
'ad_storage': 'denied',
383+
'ad_user_data': 'denied',
384+
'ad_personalization': 'denied',
385+
'analytics_storage': 'denied'
386+
...
387+
wait_for_update: 2000
388+
});
389+
</pre>
390+
Lear more about <a href="https://developers.google.com/tag-platform/security/guides/consent?consentmode=advanced" target="_blank" title="Google Consent Mode">Google Consent Mode</a>.<br/>
391+
This option is also compatible with Google Consent Mode V2.
392+
]]>
393+
</comment>
379394
</field>
380395
<field id="load_before_consent" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
381396
<label>Load GTM Script Before Consent</label>
382397
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
383-
<comment>If set to "Yes" Google Tag Manager JavaScript will be loaded before the customer provide consent.
384-
It will still be waiting for consent for sending user data related to advertising and analytics.</comment>
398+
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\ProtectCustomerData</frontend_model>
385399
<depends>
386400
<field id="protect">1</field>
387401
</depends>

view/frontend/templates/js_code.phtml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,28 @@ if (!isset($escaper)) {
3838
'ad_personalization': 'denied',
3939
'ad_storage': 'denied',
4040
'analytics_storage': 'denied',
41-
'wait_for_update': 500,
41+
<?php /* cookieyes.com start */ ?>
42+
43+
'functionality_storage': 'denied',
44+
'personalization_storage': 'denied',
45+
'security_storage': 'granted',
46+
<?php /* cookieyes.com end */ ?>
47+
48+
'wait_for_update': 2000
4249
});
50+
<?php /* cookieyes.com start */ ?>
51+
52+
gtag("set", "ads_data_redaction", true);
53+
gtag("set", "url_passthrough", true);
54+
<?php /* cookieyes.com end */ ?>
4355

4456
<?php if ($block->isLoadBeforeConsent()) { ?>
57+
4558
window.mfGtmLoadBeforeCookieAllowed = true;
4659
mfLoadGtm();
4760
<?php } ?>
4861

62+
<?php if ($this->isCookieRestrictionModeEnabled()) { ?>
4963
(function () {
5064
function getCookieValue(cookieName) {
5165
let name = cookieName + '=';
@@ -82,14 +96,15 @@ if (!isset($escaper)) {
8296
function grantConsent()
8397
{
8498
window.mfGtmUserCookiesAllowed = true;
85-
<?php if ($block->isProtectCustomerDataEnabled()) { ?>
8699
gtag('consent', 'update', {
87-
ad_user_data: 'granted',
88-
ad_personalization: 'granted',
89-
ad_storage: 'granted',
90-
analytics_storage: 'granted'
100+
'ad_user_data': 'granted',
101+
'ad_personalization': 'granted',
102+
'ad_storage': 'granted',
103+
'analytics_storage': 'granted',
104+
'functionality_storage': 'granted',
105+
'personalization_storage': 'granted',
106+
'security_storage': 'granted'
91107
});
92-
<?php } ?>
93108
}
94109

95110
if (customerDataAllowed()) {
@@ -104,6 +119,7 @@ if (!isset($escaper)) {
104119
}, 1000);
105120
}
106121
})();
122+
<?php } ?>
107123

108124
<?php } else { ?>
109125
window.mfGtmUserCookiesAllowed = true;

view/frontend/templates/no_js_code.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if (!isset($escaper)) {
1212
$escaper = $block;
1313
}
1414
?>
15-
<?php if (!$block->isProtectCustomerDataEnabled()) { ?>
15+
<?php if (!$block->isProtectCustomerDataEnabled() || $block->isLoadBeforeConsent()) { ?>
1616
<!-- Google Tag Manager (noscript) -->
1717
<?php if ('use_public_id' === $block->getConfig()->getInstallGtm()) { ?>
1818
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?= $escaper->escapeHtml($block->getPublicId()) ?>"

0 commit comments

Comments
 (0)