From 6db7827541a7668a474bb88cea81ee3809a4e04d Mon Sep 17 00:00:00 2001 From: Thierry Florac Date: Mon, 3 Jan 2022 16:55:05 +0100 Subject: [PATCH] Added support for Matomo Cloud --- tarteaucitron.services.js | 64 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/tarteaucitron.services.js b/tarteaucitron.services.js index b6cf031..d32dd03 100644 --- a/tarteaucitron.services.js +++ b/tarteaucitron.services.js @@ -3810,7 +3810,7 @@ tarteaucitron.services.koban = { 2. Push the service : - (tarteaucitron.job = tarteaucitron.job || []).push('matomo'); + (tarteaucitron.job = tarteaucitron.job || []).push('matomo'); // (or 'matomocloud' for cloud version) 3. HTML You don't need to add any html code, if the service is authorized, the javascript is added. otherwise no. @@ -3921,6 +3921,68 @@ tarteaucitron.services.matomohightrack = { } }; + +tarteaucitron.services.matomocloud = { + "key": "matomocloud", + "type": "analytic", + "name": "Matomo Cloud (privacy by design)", + "uri": "https://matomo.org/faq/general/faq_146/", + "needConsent": false, + "cookies": ['_pk_ref', '_pk_cvar', '_pk_id', '_pk_ses', '_pk_hsr', 'mtm_consent', 'matomo_ignore', 'matomo_sessid'], + "js": function () { + "use strict"; + if (tarteaucitron.user.matomoId === undefined) { + return; + } + + window._paq = window._paq || []; + window._paq.push(["setSiteId", tarteaucitron.user.matomoId]); + window._paq.push(["setTrackerUrl", tarteaucitron.user.matomoHost + "matomo.php"]); + window._paq.push(["setDoNotTrack", 1]); + window._paq.push(["trackPageView"]); + window._paq.push(["setIgnoreClasses", ["no-tracking", "colorbox"]]); + window._paq.push(["enableLinkTracking"]); + window._paq.push([function () { + var self = this; + function getOriginalVisitorCookieTimeout() { + var now = new Date(), + nowTs = Math.round(now.getTime() / 1000), + visitorInfo = self.getVisitorInfo(); + var createTs = parseInt(visitorInfo[2]); + var cookieTimeout = 33696000; // 13 mois en secondes + var originalTimeout = createTs + cookieTimeout - nowTs; + return originalTimeout; + } + this.setVisitorCookieTimeout(getOriginalVisitorCookieTimeout()); + }]); + + tarteaucitron.addScript('https://cdn.matomo.cloud/matomo.js', '', '', true, 'defer', true); + + // waiting for Matomo to be ready to check first party cookies + var interval = setInterval(function () { + if (typeof Matomo === 'undefined') return + + clearInterval(interval) + + // make Matomo cookie accessible by getting tracker + Matomo.getTracker(); + + // looping through cookies + var theCookies = document.cookie.split(';'); + for (var i = 1; i <= theCookies.length; i++) { + var cookie = theCookies[i - 1].split('='); + var cookieName = cookie[0].trim(); + + // if cookie starts like a matomo one, register it + if (cookieName.indexOf('_pk_') === 0) { + tarteaucitron.services.matomo.cookies.push(cookieName); + } + } + }, 100) + } +}; + + // Hotjar /* 1. Set the following variable before the initialization :