Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#80 Add option to allow by cookie value #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Block/DataLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Model/Config/Source/GdprOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')]
];
}
}
72 changes: 71 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,81 @@
<field id="restriction_cookie_name" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Cookie Name</label>
<depends>
<field id="*/*/option" separator=",">2,3</field>
<field id="*/*/option" separator=",">2,3,4</field>
<field id="*/*/enabled">1</field>
</depends>
<validate>required-entry alphanumeric</validate>
</field>
<field id="restriction_cookie_group_name" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Cookie Group Name</label>
<depends>
<field id="*/*/option" separator=",">4</field>
<field id="*/*/enabled">1</field>
</depends>
<validate>required-entry alphanumeric</validate>
</field>
<field id="restriction_cookie_group_name_is_array" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Cookie Group Name is array</label>
<depends>
<field id="*/*/option" separator=",">4</field>
<field id="*/*/enabled">1</field>
</depends>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>
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.
</comment>
</field>
<field id="restriction_cookie_group_name_value" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>True Value for Cookie Group</label>
<depends>
<field id="*/*/option" separator=",">4</field>
<field id="*/*/enabled">1</field>
<field id="*/*/restriction_cookie_group_name_is_array">1</field>
</depends>
<validate>alphanumeric</validate>
<comment>
Usually either "1" or "true". Enter value without quotes.
</comment>
</field>
<field id="restriction_cookie_group_accept_all" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Accept All Value</label>
<depends>
<field id="*/*/option" separator=",">4</field>
<field id="*/*/enabled">1</field>
</depends>
<validate>alphanumeric</validate>
</field>
<field id="restriction_cookie_group_accept_all_is_array" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Accept all value is array</label>
<depends>
<field id="*/*/option" separator=",">4</field>
<field id="*/*/enabled">1</field>
</depends>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>
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.
</comment>
</field>
<field id="restriction_cookie_group_accept_all_value" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Accept all true value</label>
<depends>
<field id="*/*/option" separator=",">4</field>
<field id="*/*/enabled">1</field>
<field id="*/*/restriction_cookie_group_accept_all_is_array">1</field>
</depends>
<validate>alphanumeric</validate>
<comment>
Usually either "1" or "true". Enter value without quotes.
</comment>
</field>
<field id="restriction_cookie_group_negate" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Negate</label>
<depends>
<field id="*/*/option" separator=",">4</field>
<field id="*/*/enabled">1</field>
</depends>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="note" translate="label" type="note" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" >
<comment>
To enable disable tracking goto Stores > Configuration > General > Web > Default Cookie Settings > Cookie Restriction Mode
Expand Down
5 changes: 5 additions & 0 deletions view/frontend/templates/js.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ $containerCode = $block->getEmbeddedCode();
"isCookieRestrictionModeEnabled": <?= /* @noEscape */ $block->isCookieRestrictionModeEnabled() ?>,
"currentWebsite": <?= /* @noEscape */ $block->getCurrentWebsiteId() ?>,
"cookieName": "<?= /* @noEscape */ $block->getCookieRestrictionName() ?>",
"cookieGroupName": "<?= /* @noEscape */ $block->getCookieRestrictionGroupName() ?>",
"cookieGroupNameValue": "<?= /* @noEscape */ $block->getCookieRestrictionGroupNameValue() ?>",
"cookieGroupAcceptAll": "<?= /* @noEscape */ $block->getCookieRestrictionGroupAcceptAll() ?>",
"cookieGroupAcceptAllValue": "<?= /* @noEscape */ $block->getCookieRestrictionGroupAcceptAllValue() ?>",
"cookieGroupNameNegate": "<?= /* @noEscape */ $block->getCookieRestrictionGroupNegate() ?>",
"dataLayer": "<?= /* @noEscape */ $block->getDataLayerName() ?>",
"accountId": "<?= /* @noEscape */ $block->getAccountId() ?>",
"data": <?= /* @noEscape */ $block->getDataLayerJson() ?>,
Expand Down
57 changes: 56 additions & 1 deletion view/frontend/web/js/datalayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ define([
{
var allowServices = false,
allowedCookies,
allowedWebsites;
allowedWebsites,
cookieGroupSettings,
allowedCookieGroupSettings;

if (!config.isGdprEnabled || (!config.isGdprEnabled && !config.addJsInHeader)) {
allowServices = true;
Expand All @@ -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;
Expand Down Expand Up @@ -128,4 +175,12 @@ define([

}

function isJSON(str) {
try {
return (JSON.parse(str) && !!str);
} catch (e) {
return false;
}
}

});