Verified Commit 0e654a5a authored by Laurent Heirendt's avatar Laurent Heirendt
Browse files

rewrite of gdpr based on eu-banner

parent aa8876e0
/* Matomo global variable
note: most code has been inspired from:
https://edps.europa.eu/sites/edp/modules/features/edpsweb_cookies/js/cookies-popup.js
*/
var _paq = window._paq || _paq || [];
/* loadMatomo: load the tracking code of the matomo instance */
......@@ -25,6 +20,7 @@ function loadMatomo() {
}
}
/* setResultText: change the text in the footer */
function setResultText(acceptCookies){
if (acceptCookies == true) {
$('#gdpr-result-text').html(settings.accept_all_text);
......@@ -35,38 +31,74 @@ function setResultText(acceptCookies){
/* showCookieBanner: show the banner */
function showCookieBanner() {
var popup = jQuery('#lap-cookies-banner');
var popup = $('#lap-cookies-banner');
popup.hide();
popup.slideDown('slow');
}
/* hideCookieBanner: show the banner */
function hideCookieBanner() {
var popup = jQuery('#lap-cookies-banner');
var popup = $('#lap-cookies-banner');
popup.hide();
}
/* clearCookies: clear all cookies */
function clearCookies() {
var hostname = document.location.hostname.replace(/^www\./, ''),
commonSuffix = '; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
// remove the analytics cookies
var cookies = document.cookie.split('; ');
for (var i in cookies) {
var name = cookies[i].split('=')[0];
if (name.startsWith('_pk_')) {
Cookies.set(name, 0, { expires: -1, path: '/' });
}
for (var i in cookies) {
var name = cookies[i].split('=')[0];
if (name.startsWith('_pk_')) {
document.cookie = name + '=; domain=.' + hostname + commonSuffix;
}
}
setResultText(false);
}
/* Create/update cookie */
function setCookie(name, value) {
var date = new Date();
date.setTime(date.getTime() + settings.cookieTimeout);
document.cookie = name + '=' + value + ';expires=' + date.toGMTString() + ';path=/';
}
/*
* Set consent cookie or localStorage
*/
function setConsent(consent) {
setCookie(settings.cookieName, consent);
setResultText(consent);
}
/*
* Check if user gave consent
*/
function hasConsent() {
var isCookieSet = document.cookie.indexOf(settings.cookieName + '=1' ) > -1;
if (isCookieSet) {
setResultText(true);
return true;
} else {
setResultText(false);
return false;
}
setResultText(false);
}
$( document ).ready(function() {
/* accept */
$('.lap-cookies-accept').click(function (e) {
e.preventDefault(true);
Cookies.set('lap_cookie_agree', 1, { expires: parseInt(settings.expires), path: '/' });
setCookie(settings.cookieName, 1);
hideCookieBanner();
jQuery('.lap-cookies-accept').addClass('disabled');
jQuery('.lap-cookies-refuse').removeClass('disabled');
jQuery(this).blur();
$('.lap-cookies-accept').addClass('disabled');
$('.lap-cookies-refuse').removeClass('disabled');
$(this).blur();
loadMatomo();
setResultText(true);
......@@ -76,45 +108,41 @@ $( document ).ready(function() {
$('.lap-cookies-refuse').click(function (e) {
e.preventDefault(true);
clearCookies();
Cookies.set('lap_cookie_agree', 0, { expires: parseInt(settings.expires), path: '/' });
setCookie(settings.cookieName, 0);
hideCookieBanner();
jQuery('.lap-cookies-refuse').addClass('disabled');
jQuery('.lap-cookies-accept').removeClass('disabled');
jQuery(this).blur();
$('.lap-cookies-refuse').addClass('disabled');
$('.lap-cookies-accept').removeClass('disabled');
$(this).blur();
setResultText(false);
});
/* default mechanism */
dnt = false;
if (window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack || ('external' in window && 'msTrackingProtectionEnabled' in window.external)) {
if (window.doNotTrack == "1" || navigator.doNotTrack == "yes" || navigator.doNotTrack == "1" || navigator.msDoNotTrack == "1" || ('external' in window && 'msTrackingPronectionEnabled' in window.external && window.external.msTrackingProtectionEnabled())) {
/* Do Not Track is enabled => stop */
jQuery('.lap-cookies-accept, .lap-cookies-refuse').addClass('disabled');
dnt = true;
}
}
if (dnt == false) {
// get the indicator cookie
var lapCookieAgree = '';
var name = 'lap_cookie_agree=';
var cookieArray = decodeURIComponent(document.cookie).split('; ');
cookieArray.forEach(cookie => {
if (cookie.indexOf(name) == 0) {
lapCookieAgree = cookie.substring(name.length, cookie.length);
}
});
// logic
if (lapCookieAgree == '0') {
/* visitor refused cookies => remove Matomo trace if present */
jQuery('.lap-cookies-refuse').addClass('disabled');
clearCookies();
} else if (lapCookieAgree == '1') {
/* visitor accepted cookies => load Matomo */
jQuery('.lap-cookies-accept').addClass('disabled');
// Detect if the visitor is a bot or not
// Prevent for search engine take the cookie alert message as main content of the page
var isBot = settings.bots.test(navigator.userAgent);
// Check if DoNotTrack is activated
var dnt = navigator.doNotTrack || navigator.msDoNotTrack || window.doNotTrack;
var isToTrack = (dnt !== null && dnt !== undefined) ? (dnt && dnt !== 'yes' && dnt !== 1 && dnt !== '1') : true;
var doNotTrack = false;
// Do nothing if it is a bot
// If DoNotTrack is activated, do nothing too
if (isBot || !isToTrack || hasConsent() === false) {
hideCookieBanner();
$('.lap-cookies-refuse').addClass('disabled');
clearCookies();
doNotTrack = true;
}
// User has already consent to use cookies to tracking
if (doNotTrack == false) {
if (hasConsent() === true) {
$('.lap-cookies-accept').addClass('disabled');
loadMatomo();
} else {
/* visitor did not choose yet */
// If it's not a bot, no DoNotTrack and not already accept, so show banner
showCookieBanner();
}
}
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment