diff --git a/Block/DataLayer.php b/Block/DataLayer.php index 35a2d45..431876c 100755 --- a/Block/DataLayer.php +++ b/Block/DataLayer.php @@ -78,6 +78,51 @@ 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 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 c4a6906..2d9fa98 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -76,6 +76,71 @@ 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 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 e835ca0..8468d0f 100644 --- a/Model/Config/Source/GdprOption.php +++ b/Model/Config/Source/GdprOption.php @@ -13,6 +13,7 @@ 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; /** * @return array @@ -22,7 +23,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 based on Cookie Values')] ]; } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 253422b..36e8e2c 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -113,11 +113,81 @@ - 2,3 + 2,3,4 1 required-entry alphanumeric + + + + 4 + 1 + + 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 4569b4b..6b1c10f 100755 --- a/view/frontend/templates/js.phtml +++ b/view/frontend/templates/js.phtml @@ -33,6 +33,11 @@ $containerCode = $block->getEmbeddedCode(); "isCookieRestrictionModeEnabled": isCookieRestrictionModeEnabled() ?>, "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 24bcb24..1ea0ba7 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,51 @@ 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) { + + // 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 (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 { + // Assume comma separated list + const cookieArray = cookieGroupSettings.split(","); + + if (cookieArray.includes(config.cookieGroupName) || cookieArray.includes(config.cookieGroupAcceptAll)) { + allowServices = config.cookieGroupNameNegate == 0 ? 'true' : 'false'; + } + } + } } return allowServices; @@ -128,4 +175,12 @@ define([ } + function isJSON(str) { + try { + return (JSON.parse(str) && !!str); + } catch (e) { + return false; + } + } + });