From 0390b397152593865185e45e854fe9e0d7a73bbe Mon Sep 17 00:00:00 2001 From: GHaddonAD Date: Wed, 18 Oct 2023 22:16:49 +0100 Subject: [PATCH 1/3] Add cookie group option to GDPR settings --- Block/DataLayer.php | 9 +++++++++ Helper/Data.php | 13 +++++++++++++ Model/Config/Source/GdprOption.php | 5 ++++- etc/adminhtml/system.xml | 10 +++++++++- view/frontend/templates/js.phtml | 1 + view/frontend/web/js/datalayer.js | 14 +++++++++++++- 6 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Block/DataLayer.php b/Block/DataLayer.php index 35a2d45..66714b6 100755 --- a/Block/DataLayer.php +++ b/Block/DataLayer.php @@ -78,6 +78,15 @@ public function getCookieRestrictionName($store_id = null) } } + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupName($store_id = null) + { + return $this->_gtmHelper->getCookieRestrictionGroupName($store_id); + } + /** * @param null $store_id * @return int diff --git a/Helper/Data.php b/Helper/Data.php index c4a6906..6f9a866 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -76,6 +76,19 @@ public function getCookieRestrictionName($store_id = null) ); } + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupName($store_id = null) + { + return $this->scopeConfig->getValue( + 'googletagmanager/gdpr/restriction_cookie_group_name', + ScopeInterface::SCOPE_STORE, + $store_id + ); + } + /** * @param null $store_id * @return bool diff --git a/Model/Config/Source/GdprOption.php b/Model/Config/Source/GdprOption.php index e835ca0..6201c9f 100644 --- a/Model/Config/Source/GdprOption.php +++ b/Model/Config/Source/GdprOption.php @@ -14,6 +14,8 @@ class GdprOption implements ArrayInterface const IF_COOKIE_EXIST = 2; const IF_COOKIE_NOT_EXIST = 3; + const IF_COOKIE_VALUE_EXIST = 4; + /** * @return array */ @@ -22,7 +24,8 @@ public function toOptionArray() return [ ['value' => self::USE_COOKIE_RESTRICTION_MODE, 'label' => __('Use Magento Cookie Restriction Mode')], ['value' => self::IF_COOKIE_EXIST, 'label' => __('Enable Google Analytics Tracking if Cookie Exist')], - ['value' => self::IF_COOKIE_NOT_EXIST, 'label' => __('Disable Google Analytics Tracking if Cookie Exist')] + ['value' => self::IF_COOKIE_NOT_EXIST, 'label' => __('Disable Google Analytics Tracking if Cookie Exist')], + ['value' => self::IF_COOKIE_VALUE_EXIST, 'label' => __('Enable Google Analytics Tracking if Cookie contains active group name')] ]; } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 253422b..8c15561 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -113,7 +113,15 @@ - 2,3 + 2,3,4 + 1 + + required-entry alphanumeric + + + + + 4 1 required-entry alphanumeric diff --git a/view/frontend/templates/js.phtml b/view/frontend/templates/js.phtml index 4569b4b..9c639c7 100755 --- a/view/frontend/templates/js.phtml +++ b/view/frontend/templates/js.phtml @@ -33,6 +33,7 @@ $containerCode = $block->getEmbeddedCode(); "isCookieRestrictionModeEnabled": isCookieRestrictionModeEnabled() ?>, "currentWebsite": getCurrentWebsiteId() ?>, "cookieName": "getCookieRestrictionName() ?>", + "cookieGroupName": "getCookieRestrictionGroupName() ?>", "dataLayer": "getDataLayerName() ?>", "accountId": "getAccountId() ?>", "data": getDataLayerJson() ?>, diff --git a/view/frontend/web/js/datalayer.js b/view/frontend/web/js/datalayer.js index 24bcb24..6103ae1 100644 --- a/view/frontend/web/js/datalayer.js +++ b/view/frontend/web/js/datalayer.js @@ -52,7 +52,9 @@ define([ { var allowServices = false, allowedCookies, - allowedWebsites; + allowedWebsites, + cookieGroupSettings, + allowedCookieGroupSettings; if (!config.isGdprEnabled || (!config.isGdprEnabled && !config.addJsInHeader)) { allowServices = true; @@ -70,6 +72,16 @@ define([ allowServices = $.mage.cookies.get(config.cookieName) !== null; } else if (config.gdprOption === 3) { allowServices = $.mage.cookies.get(config.cookieName) === null; + } else if (config.gdprOption === 4) { + cookieGroupSettings = $.mage.cookies.get(config.cookieName); + + if (cookieGroupSettings !== null) { + allowedCookieGroupSettings = JSON.parse(cookieGroupSettings); + + if (allowedCookieGroupSettings[config.cookieGroupName] === 1) { + allowServices = true; + } + } } return allowServices; From 4b3543c4e661d63c52ecfe82f0ec41cb139d8c0d Mon Sep 17 00:00:00 2001 From: GHaddonAD Date: Tue, 24 Oct 2023 14:11:36 +0100 Subject: [PATCH 2/3] Add support for checking Cookie values for GDPR compliance. --- Block/DataLayer.php | 36 +++++++++++++++++ Helper/Data.php | 52 +++++++++++++++++++++++++ Model/Config/Source/GdprOption.php | 3 +- etc/adminhtml/system.xml | 62 ++++++++++++++++++++++++++++++ view/frontend/templates/js.phtml | 4 ++ view/frontend/web/js/datalayer.js | 27 ++++++++++++- 6 files changed, 180 insertions(+), 4 deletions(-) diff --git a/Block/DataLayer.php b/Block/DataLayer.php index 66714b6..431876c 100755 --- a/Block/DataLayer.php +++ b/Block/DataLayer.php @@ -87,6 +87,42 @@ public function getCookieRestrictionGroupName($store_id = null) return $this->_gtmHelper->getCookieRestrictionGroupName($store_id); } + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupNameValue($store_id = null) + { + return $this->_gtmHelper->getCookieRestrictionGroupNameValue($store_id); + } + + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupAcceptAll($store_id = null) + { + return $this->_gtmHelper->getCookieRestrictionGroupAcceptAll($store_id); + } + + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupAcceptAllValue($store_id = null) + { + return $this->_gtmHelper->getCookieRestrictionGroupAcceptAllValue($store_id); + } + + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupNegate($store_id = null) + { + return $this->_gtmHelper->getCookieRestrictionGroupNegate($store_id); + } + /** * @param null $store_id * @return int diff --git a/Helper/Data.php b/Helper/Data.php index 6f9a866..2d9fa98 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -89,6 +89,58 @@ public function getCookieRestrictionGroupName($store_id = null) ); } + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupNameValue($store_id = null) + { + return $this->scopeConfig->getValue( + 'googletagmanager/gdpr/restriction_cookie_group_name_value', + ScopeInterface::SCOPE_STORE, + $store_id + ); + } + + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupNegate($store_id = null) + { + return $this->scopeConfig->getValue( + 'googletagmanager/gdpr/restriction_cookie_group_negate', + ScopeInterface::SCOPE_STORE, + $store_id + ); + } + + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupAcceptAll($store_id = null) + { + return $this->scopeConfig->getValue( + 'googletagmanager/gdpr/restriction_cookie_group_accept_all', + ScopeInterface::SCOPE_STORE, + $store_id + ); + } + + /** + * @param null $store_id + * @return string + */ + public function getCookieRestrictionGroupAcceptAllValue($store_id = null) + { + return $this->scopeConfig->getValue( + 'googletagmanager/gdpr/restriction_cookie_group_accept_all_value', + ScopeInterface::SCOPE_STORE, + $store_id + ); + } + /** * @param null $store_id * @return bool diff --git a/Model/Config/Source/GdprOption.php b/Model/Config/Source/GdprOption.php index 6201c9f..8468d0f 100644 --- a/Model/Config/Source/GdprOption.php +++ b/Model/Config/Source/GdprOption.php @@ -13,7 +13,6 @@ class GdprOption implements ArrayInterface const USE_COOKIE_RESTRICTION_MODE = 1; const IF_COOKIE_EXIST = 2; const IF_COOKIE_NOT_EXIST = 3; - const IF_COOKIE_VALUE_EXIST = 4; /** @@ -25,7 +24,7 @@ public function toOptionArray() ['value' => self::USE_COOKIE_RESTRICTION_MODE, 'label' => __('Use Magento Cookie Restriction Mode')], ['value' => self::IF_COOKIE_EXIST, 'label' => __('Enable Google Analytics Tracking if Cookie Exist')], ['value' => self::IF_COOKIE_NOT_EXIST, 'label' => __('Disable Google Analytics Tracking if Cookie Exist')], - ['value' => self::IF_COOKIE_VALUE_EXIST, 'label' => __('Enable Google Analytics Tracking if Cookie contains active group name')] + ['value' => self::IF_COOKIE_VALUE_EXIST, 'label' => __('Enable Google Analytics Tracking based on Cookie Values')] ]; } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 8c15561..36e8e2c 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -126,6 +126,68 @@ required-entry alphanumeric + + + + 4 + 1 + + Magento\Config\Model\Config\Source\Yesno + + e.g If the cookie is stored as something like {"performance":1} or {"performance":0} select YES. If only contains comma seperated list leave as NO. + + + + + + 4 + 1 + 1 + + alphanumeric + + Usually either "1" or "true". Enter value without quotes. + + + + + + 4 + 1 + + alphanumeric + + + + + 4 + 1 + + Magento\Config\Model\Config\Source\Yesno + + e.g If the cookie is stored as something like {"accept":1} or {0:0} select YES. If the cookie value only contains comma seperated list leave as NO. + + + + + + 4 + 1 + 1 + + alphanumeric + + Usually either "1" or "true". Enter value without quotes. + + + + + + 4 + 1 + + Magento\Config\Model\Config\Source\Yesno + To enable disable tracking goto Stores > Configuration > General > Web > Default Cookie Settings > Cookie Restriction Mode diff --git a/view/frontend/templates/js.phtml b/view/frontend/templates/js.phtml index 9c639c7..6b1c10f 100755 --- a/view/frontend/templates/js.phtml +++ b/view/frontend/templates/js.phtml @@ -34,6 +34,10 @@ $containerCode = $block->getEmbeddedCode(); "currentWebsite": getCurrentWebsiteId() ?>, "cookieName": "getCookieRestrictionName() ?>", "cookieGroupName": "getCookieRestrictionGroupName() ?>", + "cookieGroupNameValue": "getCookieRestrictionGroupNameValue() ?>", + "cookieGroupAcceptAll": "getCookieRestrictionGroupAcceptAll() ?>", + "cookieGroupAcceptAllValue": "getCookieRestrictionGroupAcceptAllValue() ?>", + "cookieGroupNameNegate": "getCookieRestrictionGroupNegate() ?>", "dataLayer": "getDataLayerName() ?>", "accountId": "getAccountId() ?>", "data": getDataLayerJson() ?>, diff --git a/view/frontend/web/js/datalayer.js b/view/frontend/web/js/datalayer.js index 6103ae1..eeefea6 100644 --- a/view/frontend/web/js/datalayer.js +++ b/view/frontend/web/js/datalayer.js @@ -78,8 +78,31 @@ define([ if (cookieGroupSettings !== null) { allowedCookieGroupSettings = JSON.parse(cookieGroupSettings); - if (allowedCookieGroupSettings[config.cookieGroupName] === 1) { - allowServices = true; + // If accept all is expected to be an array + if (config.cookieGroupAcceptAll.trim() !== '' && config.cookieGroupAcceptAllValue.trim() !== '') { + // Check array key as string or int + if (allowedCookieGroupSettings[config.cookieGroupAcceptAll] == config.cookieGroupAcceptAllValue || + allowedCookieGroupSettings[parseInt(config.cookieGroupAcceptAll)] == config.cookieGroupAcceptAllValue) { + return config.cookieGroupNameNegate == 0 ? 'true':'false'; + } + } + // If accept all is not expected to be an array + else if (config.cookieGroupAcceptAll.trim() !== '' && config.cookieGroupAcceptAllValue.trim() === '') { + if (allowedCookieGroupSettings == config.cookieGroupAcceptAll) { + return config.cookieGroupNameNegate == 0 ? 'true':'false'; + } + } + + if (config.cookieGroupNameValue.trim() !== '') { + // Check array key as string or int + if (allowedCookieGroupSettings[config.cookieGroupName] == config.cookieGroupNameValue || + allowedCookieGroupSettings[parseInt(config.cookieGroupName)] == config.cookieGroupNameValue) { + allowServices = config.cookieGroupNameNegate == 0 ? 'true':'false'; + } + } else { + if (allowedCookieGroupSettings.includes(config.cookieGroupName)) { + allowServices = config.cookieGroupNameNegate == 0 ? 'true':'false'; + } } } } From bedc1eceaf09f8c289dd08c5eed488b1bdbb8ab6 Mon Sep 17 00:00:00 2001 From: GHaddonAD Date: Tue, 24 Oct 2023 15:36:05 +0100 Subject: [PATCH 3/3] Add fix for non JSON cookie values --- view/frontend/web/js/datalayer.js | 62 ++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/view/frontend/web/js/datalayer.js b/view/frontend/web/js/datalayer.js index eeefea6..1ea0ba7 100644 --- a/view/frontend/web/js/datalayer.js +++ b/view/frontend/web/js/datalayer.js @@ -76,32 +76,44 @@ define([ cookieGroupSettings = $.mage.cookies.get(config.cookieName); if (cookieGroupSettings !== null) { - allowedCookieGroupSettings = JSON.parse(cookieGroupSettings); - - // If accept all is expected to be an array - if (config.cookieGroupAcceptAll.trim() !== '' && config.cookieGroupAcceptAllValue.trim() !== '') { - // Check array key as string or int - if (allowedCookieGroupSettings[config.cookieGroupAcceptAll] == config.cookieGroupAcceptAllValue || - allowedCookieGroupSettings[parseInt(config.cookieGroupAcceptAll)] == config.cookieGroupAcceptAllValue) { - return config.cookieGroupNameNegate == 0 ? 'true':'false'; + + // Check if JSON + if (isJSON(cookieGroupSettings)) { + + allowedCookieGroupSettings = JSON.parse(cookieGroupSettings); + + // If accept all is expected to be an array + if (config.cookieGroupAcceptAll.trim() !== '' && config.cookieGroupAcceptAllValue.trim() !== '') { + // Check array key as string or int + if (allowedCookieGroupSettings[config.cookieGroupAcceptAll] == config.cookieGroupAcceptAllValue || + allowedCookieGroupSettings[parseInt(config.cookieGroupAcceptAll)] == config.cookieGroupAcceptAllValue) { + return config.cookieGroupNameNegate == 0 ? 'true' : 'false'; + } } - } - // If accept all is not expected to be an array - else if (config.cookieGroupAcceptAll.trim() !== '' && config.cookieGroupAcceptAllValue.trim() === '') { - if (allowedCookieGroupSettings == config.cookieGroupAcceptAll) { - return config.cookieGroupNameNegate == 0 ? 'true':'false'; + // If accept all is not expected to be an array + else if (config.cookieGroupAcceptAll.trim() !== '' && config.cookieGroupAcceptAllValue.trim() === '') { + if (allowedCookieGroupSettings == config.cookieGroupAcceptAll) { + return config.cookieGroupNameNegate == 0 ? 'true' : 'false'; + } } - } - if (config.cookieGroupNameValue.trim() !== '') { - // Check array key as string or int - if (allowedCookieGroupSettings[config.cookieGroupName] == config.cookieGroupNameValue || - allowedCookieGroupSettings[parseInt(config.cookieGroupName)] == config.cookieGroupNameValue) { - allowServices = config.cookieGroupNameNegate == 0 ? 'true':'false'; + if (config.cookieGroupNameValue.trim() !== '') { + // Check array key as string or int + if (allowedCookieGroupSettings[config.cookieGroupName] == config.cookieGroupNameValue || + allowedCookieGroupSettings[parseInt(config.cookieGroupName)] == config.cookieGroupNameValue) { + allowServices = config.cookieGroupNameNegate == 0 ? 'true' : 'false'; + } + } else { + if (allowedCookieGroupSettings.includes(config.cookieGroupName)) { + allowServices = config.cookieGroupNameNegate == 0 ? 'true' : 'false'; + } } } else { - if (allowedCookieGroupSettings.includes(config.cookieGroupName)) { - allowServices = config.cookieGroupNameNegate == 0 ? 'true':'false'; + // Assume comma separated list + const cookieArray = cookieGroupSettings.split(","); + + if (cookieArray.includes(config.cookieGroupName) || cookieArray.includes(config.cookieGroupAcceptAll)) { + allowServices = config.cookieGroupNameNegate == 0 ? 'true' : 'false'; } } } @@ -163,4 +175,12 @@ define([ } + function isJSON(str) { + try { + return (JSON.parse(str) && !!str); + } catch (e) { + return false; + } + } + });