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 || []; var _paq = window._paq || _paq || [];
/* loadMatomo: load the tracking code of the matomo instance */ /* loadMatomo: load the tracking code of the matomo instance */
...@@ -25,6 +20,7 @@ function loadMatomo() { ...@@ -25,6 +20,7 @@ function loadMatomo() {
} }
} }
/* setResultText: change the text in the footer */
function setResultText(acceptCookies){ function setResultText(acceptCookies){
if (acceptCookies == true) { if (acceptCookies == true) {
$('#gdpr-result-text').html(settings.accept_all_text); $('#gdpr-result-text').html(settings.accept_all_text);
...@@ -35,38 +31,74 @@ function setResultText(acceptCookies){ ...@@ -35,38 +31,74 @@ function setResultText(acceptCookies){
/* showCookieBanner: show the banner */ /* showCookieBanner: show the banner */
function showCookieBanner() { function showCookieBanner() {
var popup = jQuery('#lap-cookies-banner'); var popup = $('#lap-cookies-banner');
popup.hide(); popup.hide();
popup.slideDown('slow'); popup.slideDown('slow');
} }
/* hideCookieBanner: show the banner */ /* hideCookieBanner: show the banner */
function hideCookieBanner() { function hideCookieBanner() {
var popup = jQuery('#lap-cookies-banner'); var popup = $('#lap-cookies-banner');
popup.hide(); popup.hide();
} }
/* clearCookies: clear all cookies */ /* clearCookies: clear all cookies */
function clearCookies() { 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('; '); var cookies = document.cookie.split('; ');
for (var i in cookies) { for (var i in cookies) {
var name = cookies[i].split('=')[0]; var name = cookies[i].split('=')[0];
if (name.startsWith('_pk_')) { if (name.startsWith('_pk_')) {
Cookies.set(name, 0, { expires: -1, path: '/' }); 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() { $( document ).ready(function() {
/* accept */ /* accept */
$('.lap-cookies-accept').click(function (e) { $('.lap-cookies-accept').click(function (e) {
e.preventDefault(true); e.preventDefault(true);
Cookies.set('lap_cookie_agree', 1, { expires: parseInt(settings.expires), path: '/' }); setCookie(settings.cookieName, 1);
hideCookieBanner(); hideCookieBanner();
jQuery('.lap-cookies-accept').addClass('disabled'); $('.lap-cookies-accept').addClass('disabled');
jQuery('.lap-cookies-refuse').removeClass('disabled'); $('.lap-cookies-refuse').removeClass('disabled');
jQuery(this).blur(); $(this).blur();
loadMatomo(); loadMatomo();
setResultText(true); setResultText(true);
...@@ -76,45 +108,41 @@ $( document ).ready(function() { ...@@ -76,45 +108,41 @@ $( document ).ready(function() {
$('.lap-cookies-refuse').click(function (e) { $('.lap-cookies-refuse').click(function (e) {
e.preventDefault(true); e.preventDefault(true);
clearCookies(); clearCookies();
Cookies.set('lap_cookie_agree', 0, { expires: parseInt(settings.expires), path: '/' }); setCookie(settings.cookieName, 0);
hideCookieBanner(); hideCookieBanner();
jQuery('.lap-cookies-refuse').addClass('disabled'); $('.lap-cookies-refuse').addClass('disabled');
jQuery('.lap-cookies-accept').removeClass('disabled'); $('.lap-cookies-accept').removeClass('disabled');
jQuery(this).blur(); $(this).blur();
setResultText(false); setResultText(false);
}); });
/* default mechanism */ // Detect if the visitor is a bot or not
dnt = false; // Prevent for search engine take the cookie alert message as main content of the page
if (window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack || ('external' in window && 'msTrackingProtectionEnabled' in window.external)) { var isBot = settings.bots.test(navigator.userAgent);
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 */ // Check if DoNotTrack is activated
jQuery('.lap-cookies-accept, .lap-cookies-refuse').addClass('disabled'); var dnt = navigator.doNotTrack || navigator.msDoNotTrack || window.doNotTrack;
dnt = true; var isToTrack = (dnt !== null && dnt !== undefined) ? (dnt && dnt !== 'yes' && dnt !== 1 && dnt !== '1') : true;
}
} var doNotTrack = false;
if (dnt == false) { // Do nothing if it is a bot
// get the indicator cookie // If DoNotTrack is activated, do nothing too
var lapCookieAgree = ''; if (isBot || !isToTrack || hasConsent() === false) {
var name = 'lap_cookie_agree='; hideCookieBanner();
var cookieArray = decodeURIComponent(document.cookie).split('; '); $('.lap-cookies-refuse').addClass('disabled');
cookieArray.forEach(cookie => { clearCookies();
if (cookie.indexOf(name) == 0) { doNotTrack = true;
lapCookieAgree = cookie.substring(name.length, cookie.length); }
}
}); // User has already consent to use cookies to tracking
// logic if (doNotTrack == false) {
if (lapCookieAgree == '0') { if (hasConsent() === true) {
/* visitor refused cookies => remove Matomo trace if present */ $('.lap-cookies-accept').addClass('disabled');
jQuery('.lap-cookies-refuse').addClass('disabled');
clearCookies();
} else if (lapCookieAgree == '1') {
/* visitor accepted cookies => load Matomo */
jQuery('.lap-cookies-accept').addClass('disabled');
loadMatomo(); loadMatomo();
} else { } else {
/* visitor did not choose yet */ // If it's not a bot, no DoNotTrack and not already accept, so show banner
showCookieBanner(); 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