Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rich-id/cookies-regulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Nov 29, 2021
2 parents 5c01ed4 + ad04192 commit 4e10d57
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 0.1.5

New feature:
* Add support for `anonymize_ip` option for Google Analytics

## 0.1.4

New features:
* Add hotjar and googleAnalytics to the provided services

Bug fixes:
* Fix the date given in the decision callback

## 0.1.2

New features:
Expand Down
62 changes: 55 additions & 7 deletions js/cookies-regulation-services.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,70 @@
window.cookiesRegulationServices = {};

// Google Tag Manager
function insertScript(url) {
const script = document.createElement('script');
script.async = true;
script.src = url;

const head = document.getElementsByTagName('head')[0];
head.appendChild(script);
}

// Google Tag Manager
window.cookiesRegulationServices.googleTagManager = {
requiredOptions: ['id'],
cookiesIdentifiers: ['^ga.*$', '^_ga.*$', '^_gc.*$', '^_gi.*$', '^_hj.*$', '^__utma.*$', '^__utmb.*$', '^__utmc.*$', '^__utmt.*$', '^__utmz.*$', '^__gads.*$'],
callback: function (options) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
'event': 'gtm.js'
});

var scripts = document.getElementsByTagName('script')[0];
insertScript('https://www.googletagmanager.com/gtm.js?id=' + options.id);
}
};

// Google Analytics
window.cookiesRegulationServices.googleAnalytics = {
requiredOptions: ['id'],
cookiesIdentifiers: ['^ga.*$', '^_ga.*$', '^_gc.*$', '^_gi.*$', '^_hj.*$', '^__utma.*$', '^__utmb.*$', '^__utmc.*$', '^__utmt.*$', '^__utmz.*$', '^__gads.*$'],
callback: function (options) {
window.dataLayer = window.dataLayer || [];
window.gtag = function gtag() {
window.dataLayer.push(arguments);
};

const additionalData = {anonymize_ip: false};

if (options.anonymize_ip === true) {
additionalData.anonymize_ip = true;
}

insertScript('https://www.googletagmanager.com/gtag/js?id=' + options.id);
gtag('js', new Date());
gtag('config', options.id, additionalData);
}
};

// Hotjar
window.cookiesRegulationServices.hotjar = {
requiredOptions: ['id'],
cookiesIdentifiers: ['hjClosedSurveyInvites', '^_hj[a-zA-Z0-9]*$'],
callback: function (options) {
window._hjSettings = {
hjid: options.id,
hjsv: 6,
};

window.hj = window.hj || function() {
(window.hj.q = window.hj.q || []).push(arguments)
};

var googleTagManagerScript = document.createElement('script');
googleTagManagerScript.async = true;
googleTagManagerScript.src ='https://www.googletagmanager.com/gtm.js?id=' + options.id;
const url = 'https://static.hotjar.com/c/hotjar-'
+ window._hjSettings.hjid
+ '.js?sv='
+ window._hjSettings.hjsv;

scripts.parentNode.insertBefore(googleTagManagerScript, scripts);
insertScript(url);
}
};
2 changes: 1 addition & 1 deletion js/cookies-regulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CookiesRegulation {
let preferences = data[0];
let reloadPage = data[1];

let metadata = { uuid: uuidv4(), date: new Date().toLocaleString() };
let metadata = { uuid: uuidv4(), date: new Date().toISOString() };
let decision = { preferences: preferences, metadata: metadata };

this.decisionStorage.write(decision);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rich-id/cookies-regulation",
"version": "0.1.3",
"version": "0.1.5",
"scripts": {
"build": "parcel build js/cookies-regulation.js",
"build-release": "parcel build --no-source-maps js/cookies-regulation.js",
Expand Down

0 comments on commit 4e10d57

Please sign in to comment.