Browse Source

Merge pull request #278 from OwnWeb/fix/matomo-first-party-cookies

Fix Matomo/Piwik cookies detection 🐛
Amauri CHAMPEAUX 6 years ago
parent
commit
d514890675
1 changed files with 22 additions and 0 deletions
  1. 22 0
      tarteaucitron.services.js

+ 22 - 0
tarteaucitron.services.js

@@ -2462,6 +2462,28 @@ tarteaucitron.services.matomo = {
         }]);
 
         tarteaucitron.addScript(tarteaucitron.user.matomoHost + 'piwik.js', '', '', true, 'defer', true);
+
+        // waiting for piwik to be ready to check first party cookies
+        var interval = setInterval(function() {
+            if (typeof Piwik === 'undefined') return
+
+            clearInterval(interval)
+
+            // make piwik/matomo cookie accessible by getting tracker
+            Piwik.getTracker();
+
+            // looping throught 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 piwik one, register it
+                if (cookieName.indexOf('_pk_') === 0) {
+                    tarteaucitron.services.matomo.cookies.push(cookieName);
+                }
+            }
+        }, 100)
     }
 };