Skip to content

Commit

Permalink
Improvements for Google Consent Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Mar 20, 2024
1 parent 6531b99 commit 081b266
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
10 changes: 6 additions & 4 deletions Block/Adminhtml/System/Config/Form/ProtectCustomerData.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class ProtectCustomerData extends Field
public function render(AbstractElement $element): string
{
$url = $this->getUrl('*/*/*/section/web');
$comment = 'When enabled, data won\'t be sent to Google, until the customer provide consent.<br/><br/>
<strong>Note</strong>, that this option will work only when Cookie Restriction Mode at
<a href="' . $url . '" target="_blank">Stores > Configuration > General > Web > Default Cookie Settings</a>
is enabled.';
$comment = '
<strong>Note</strong>: this option works only when default Magento Cookie Restriction Mode is enabled at
<a href="' . $url . '" target="_blank">Stores > Configuration > General > Web > Default Cookie Settings</a>.
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/>
Even if GTM JavaScript is loaded before customer`s consent,
GTM still waits for consent to send user data related to advertising and analytics.';

$element->setComment($comment);
return parent::render($element);
Expand Down
10 changes: 10 additions & 0 deletions Block/GtmCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public function isLoadBeforeConsent(): bool
return $this->config->isLoadBeforeConsent();
}

/**
* Retrieve true if cookie restriction mode enabled
*
* @return bool
*/
public function isCookieRestrictionModeEnabled()
{
return $this->config->isCookieRestrictionModeEnabled();
}

/**
* Get current website ID
*
Expand Down
7 changes: 3 additions & 4 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ public function getBrandAttribute(string $storeId = null): string
*/
public function isProtectCustomerDataEnabled(string $storeId = null): bool
{
return (bool)$this->getConfig(self::XML_PATH_PROTECT_CUSTOMER_DATA, $storeId) &&
$this->isCookieRestrictionModeEnabled($storeId);
return (bool)$this->getConfig(self::XML_PATH_PROTECT_CUSTOMER_DATA, $storeId);
}

/**
Expand All @@ -243,8 +242,8 @@ public function isProtectCustomerDataEnabled(string $storeId = null): bool
*/
public function isLoadBeforeConsent(string $storeId = null): bool
{
return (bool)$this->getConfig(self::XML_PATH_LOAD_BEFORE_CONSENT, $storeId) &&
$this->isCookieRestrictionModeEnabled($storeId);
return $this->getConfig(self::XML_PATH_LOAD_BEFORE_CONSENT, $storeId) ||
!$this->isCookieRestrictionModeEnabled($storeId);
}


Expand Down
22 changes: 18 additions & 4 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,29 @@ To create a new secret, navigate in the
<group id="customer_data" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
<label>GDPR/CCPA/LGPD (Customer Data Protection Regulation)</label>
<field id="protect" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Protect Customer Data</label>
<label>Enable Google Consent Mode</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\ProtectCustomerData</frontend_model>
<comment><![CDATA[
<p>If set to "Yes" Google Tag Manager Extension will set consent as "denied" by default for measurement capabilities. For example:
<pre style="border: 1px dashed #5f5f5f;padding: 5px;background: #f4f4f4;">
gtag('consent', 'default', {
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'analytics_storage': 'denied'
...
wait_for_update: 2000
});
</pre>
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/>
This option is also compatible with Google Consent Mode V2.
]]>
</comment>
</field>
<field id="load_before_consent" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Load GTM Script Before Consent</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>If set to "Yes" Google Tag Manager JavaScript will be loaded before the customer provide consent.
It will still be waiting for consent for sending user data related to advertising and analytics.</comment>
<frontend_model>Magefan\GoogleTagManager\Block\Adminhtml\System\Config\Form\ProtectCustomerData</frontend_model>
<depends>
<field id="protect">1</field>
</depends>
Expand Down
30 changes: 23 additions & 7 deletions view/frontend/templates/js_code.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@ if (!isset($escaper)) {
'ad_personalization': 'denied',
'ad_storage': 'denied',
'analytics_storage': 'denied',
'wait_for_update': 500,
<?php /* cookieyes.com start */ ?>

'functionality_storage': 'denied',
'personalization_storage': 'denied',
'security_storage': 'granted',
<?php /* cookieyes.com end */ ?>

'wait_for_update': 2000
});
<?php /* cookieyes.com start */ ?>

gtag("set", "ads_data_redaction", true);
gtag("set", "url_passthrough", true);
<?php /* cookieyes.com end */ ?>

<?php if ($block->isLoadBeforeConsent()) { ?>

window.mfGtmLoadBeforeCookieAllowed = true;
mfLoadGtm();
<?php } ?>

<?php if ($this->isCookieRestrictionModeEnabled()) { ?>
(function () {
function getCookieValue(cookieName) {
let name = cookieName + '=';
Expand Down Expand Up @@ -82,14 +96,15 @@ if (!isset($escaper)) {
function grantConsent()
{
window.mfGtmUserCookiesAllowed = true;
<?php if ($block->isProtectCustomerDataEnabled()) { ?>
gtag('consent', 'update', {
ad_user_data: 'granted',
ad_personalization: 'granted',
ad_storage: 'granted',
analytics_storage: 'granted'
'ad_user_data': 'granted',
'ad_personalization': 'granted',
'ad_storage': 'granted',
'analytics_storage': 'granted',
'functionality_storage': 'granted',
'personalization_storage': 'granted',
'security_storage': 'granted'
});
<?php } ?>
}

if (customerDataAllowed()) {
Expand All @@ -104,6 +119,7 @@ if (!isset($escaper)) {
}, 1000);
}
})();
<?php } ?>

<?php } else { ?>
window.mfGtmUserCookiesAllowed = true;
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/templates/no_js_code.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (!isset($escaper)) {
$escaper = $block;
}
?>
<?php if (!$block->isProtectCustomerDataEnabled()) { ?>
<?php if (!$block->isProtectCustomerDataEnabled() || $block->isLoadBeforeConsent()) { ?>
<!-- Google Tag Manager (noscript) -->
<?php if ('use_public_id' === $block->getConfig()->getInstallGtm()) { ?>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<?= $escaper->escapeHtml($block->getPublicId()) ?>"
Expand Down

0 comments on commit 081b266

Please sign in to comment.