Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Basic setup for Consent V2 #15

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
63 changes: 63 additions & 0 deletions Setup/Patch/Data/PersonalizationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace Phpro\CookieConsent\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Store\Model\Store;
use Phpro\CookieConsent\Api\CookieGroupRepositoryInterface;
use Phpro\CookieConsent\Api\Data\CookieGroupInterface;
use Phpro\CookieConsent\Model\CookieGroupFactory;

class PersonalizationData implements DataPatchInterface
{
/**
* @var CookieGroupFactory
*/
private $cookieGroupFactory;

/**
* @var CookieGroupRepositoryInterface
*/
private $cookieGroupRepository;


public function __construct(
CookieGroupFactory $cookieGroupFactory,
CookieGroupRepositoryInterface $cookieGroupRepository
) {
$this->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);
}
}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@
"sort-packages": true,
"allow-plugins": {
"phpro/grumphp-shim": true,
"magento/composer-dependency-version-audit-plugin": true
"magento/composer-dependency-version-audit-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
"post-install-cmd": [
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard,../../phpcompatibility/php-compatibility/PHPCompatibility)"
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard,../../magento/php-compatibility-fork,../../phpcompatibility/php-compatibility)"
],
"post-update-cmd": [
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard,../../phpcompatibility/php-compatibility/PHPCompatibility)"
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard,../../magento/php-compatibility-fork,../../phpcompatibility/php-compatibility)"
]
}
}
5 changes: 4 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ To delete any group, click the dropdown in the Action column and select Delete.

To create a new group, click Add new Cookie Group.

The **essential**, **analytical** & **marketing** cookie groups are created automatically on installation of the module.
The **essential**, **analytical**, **marketing** & **personalization** cookie groups are created automatically on installation of the module.
These groups can be altered to your needs.

### Create
Expand Down Expand Up @@ -56,5 +56,8 @@ store views for that specific cookie group. **Only the Name & Description field

More information for GTM configuration can be found [here](./GTM.md)

### Google Consent

More information for Google consent configuration can be found [here](Consent.md)


17 changes: 17 additions & 0 deletions docs/Consent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Consent Mode V2

## Configuration
### Enable the Google Consent feature
**Stores > Settings > Configuration > General > Cookie Consent -> Google Consent**

### Remarks
- Make sure you are using the default system names of the cookie groups:
- essential
- analytical
- marketing
- personalization
- Make sure your Google Tag Manager instance is injected AFTER the 'phpro_cookie_gtag' block. It's important to have the consent first be set to 'DENIED' before loading Google Tag Manager.




8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<comment>The value represent the expiration of the cookie in days..</comment>
</field>
</group>
<group id="gtag" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google Consent</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>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'</comment>
</field>
</group>
</section>
</system>
</config>
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<cookie_name_preferences>cookieConsentPrefs</cookie_name_preferences>
<cookie_expiration>182</cookie_expiration>
</general>
<gtag>
<enabled>0</enabled>
</gtag>
</phpro_cookie_consent>
</default>
</config>
7 changes: 7 additions & 0 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?xml version="1.0"?>
<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="after.body.start">
<block name="phpro_cookie_gtag"
template="Phpro_CookieConsent::gtag.phtml"
ifconfig="phpro_cookie_consent/gtag/enabled"
before="-"/>
</referenceContainer>

<referenceContainer name="before.body.end">
<block class="Magento\Framework\View\Element\Template"
name="phpro_cookie_consent_notice"
Expand Down
37 changes: 37 additions & 0 deletions view/frontend/templates/gtag.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script>
window.dataLayer = window.dataLayer || [];

function gtag() {
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 (event) {
const consent = event.detail;
const consentMode = {
'ad_storage': consent.cg_marketing ? 'granted': 'denied',
'analytics_storage': consent.cg_analytical ? 'granted': 'denied',
'personalization_storage': consent.cg_personalization ? 'granted': 'denied',
'functionality_storage': consent.cg_essential ? 'granted': 'denied',
'security_storage': consent.cg_essential ? 'granted': 'denied',
'ad_user_data': consent.cg_marketing ? 'granted': 'denied',
'ad_personalization': consent.cg_marketing && consent.cg_personalization ? 'granted': 'denied',
};

gtag('consent', 'update', consentMode);
localStorage.setItem('consentMode', JSON.stringify(consentMode));
})
</script>
6 changes: 4 additions & 2 deletions view/frontend/web/js/model/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ define([
return (!!$.cookie(name));
},

getConsentCookieObject: function(name) {
getConsentCookieObject: function (name) {
return $.cookie(name);
},

Expand All @@ -24,6 +24,7 @@ define([
data['expire'] = expiration;
data['secure'] = secure;
$.cookie(name, JSON.stringify(data), {expires: this.getExpiration(expiration)});
document.dispatchEvent(new CustomEvent('consent-changed', {detail: data}));
},

saveSelected: function (name, expiration, secure, systemNames) {
Expand All @@ -36,6 +37,7 @@ define([
data['expire'] = expiration;
data['secure'] = secure;
$.cookie(name, JSON.stringify(data), {expires: this.getExpiration(expiration)});
document.dispatchEvent(new CustomEvent('consent-changed', {detail: data}));
},

closeCookieNotice: function () {
Expand All @@ -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;
}
Expand Down
Loading