diff --git a/Setup/Patch/Data/PersonalizationData.php b/Setup/Patch/Data/PersonalizationData.php new file mode 100644 index 0000000..cd85641 --- /dev/null +++ b/Setup/Patch/Data/PersonalizationData.php @@ -0,0 +1,63 @@ +cookieGroupFactory = $cookieGroupFactory; + $this->cookieGroupRepository = $cookieGroupRepository; + } + + public static function getDependencies() + { + return [ + CookieGroupAttribute::class, + DefaultCookieData::class, + ]; + } + + public function getAliases() + { + return []; + } + + public function apply() + { + $this->createDefaultGroups(); + } + + private function createDefaultGroups(): void + { + /** @var CookieGroupInterface $group */ + $personalization = $this->cookieGroupFactory->create(); + $personalization->setStoreId(Store::DEFAULT_STORE_ID); + $personalization->setSystemName('personalization'); + $personalization->setName('Personalization'); + $personalization->setActive(true); + $personalization->setEssential(false); + $personalization->setDescription('Personalization cookies are used for ad personalization and remarketing.'); + + $this->cookieGroupRepository->save($personalization); + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 8aef732..9213732 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -24,6 +24,14 @@ The value represent the expiration of the cookie in days.. + + + + + Magento\Config\Model\Config\Source\Yesno + Enable the module to send the consent to Google using Gtag. It's important to have your Google Tag Manager initiated AFTER the 'phpro_cookie_gtag' block. This feature relies on the cookie groups with the following system names: 'essential', 'analytical', 'marketing' and 'personalization' + + diff --git a/etc/config.xml b/etc/config.xml index 2a90e69..cc4fecf 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -7,6 +7,9 @@ cookieConsentPrefs 182 + + 0 + diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml index d1ec3c6..28be596 100644 --- a/view/frontend/layout/default.xml +++ b/view/frontend/layout/default.xml @@ -1,6 +1,13 @@ + + + + + window.dataLayer = window.dataLayer || []; + + function gtag() { + console.log(arguments); + dataLayer.push(arguments); + } + + if (localStorage.getItem('consentMode') === null) { + gtag('consent', 'default', { + 'ad_storage': 'denied', + 'analytics_storage': 'denied', + 'personalization_storage': 'denied', + 'functionality_storage': 'denied', + 'security_storage': 'denied', + 'ad_user_data': 'denied', + 'ad_personalization': 'denied', + }); + } else { + gtag('consent', 'default', JSON.parse(localStorage.getItem('consentMode'))); + } + + document.addEventListener('consent-changed', function (consent) { + const consentMode = { + 'ad_storage': consent.marketing ? 'granted': 'denied', + 'analytics_storage': consent.analytical ? 'granted': 'denied', + 'personalization_storage': consent.personalization ? 'granted': 'denied', + 'functionality_storage': consent.essential ? 'granted': 'denied', + 'security_storage': consent.essential ? 'granted': 'denied', + 'ad_user_data': consent.marketing ? 'granted': 'denied', + 'ad_personalization': consent.marketing && consent.personalization ? 'granted': 'denied', + }; + + gtag('consent', 'update', consentMode); + localStorage.setItem('consentMode', JSON.stringify(consentMode)); + }) + diff --git a/view/frontend/web/js/model/cookie.js b/view/frontend/web/js/model/cookie.js index 16c06af..38d20aa 100644 --- a/view/frontend/web/js/model/cookie.js +++ b/view/frontend/web/js/model/cookie.js @@ -9,7 +9,7 @@ define([ return (!!$.cookie(name)); }, - getConsentCookieObject: function(name) { + getConsentCookieObject: function (name) { return $.cookie(name); }, @@ -24,6 +24,7 @@ define([ data['expire'] = expiration; data['secure'] = secure; $.cookie(name, JSON.stringify(data), {expires: this.getExpiration(expiration)}); + document.dispatchEvent(new Event('consent-changed', data)); }, saveSelected: function (name, expiration, secure, systemNames) { @@ -36,6 +37,7 @@ define([ data['expire'] = expiration; data['secure'] = secure; $.cookie(name, JSON.stringify(data), {expires: this.getExpiration(expiration)}); + document.dispatchEvent(new Event('consent-changed', data)); }, closeCookieNotice: function () { @@ -45,7 +47,7 @@ define([ getExpiration: function (expiration) { var today = new Date(); var expireDate = new Date(today); - expireDate.setDate(today.getDate()+expiration); + expireDate.setDate(today.getDate() + expiration); return expireDate; }