Hi,
I am using the Matomo tracking code in combination with the Cookiebot callback. It looks like this:
window.on('CookiebotOnAccept', function(){
if (Cookiebot.consent.statistics) {
// Matomo
var _paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//tracking.***.com/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
//- Matomo
}
});
The matomo.js is getting loaded but the page view is not tracked.
The problem seems to be that the matomo.js is probably trying to access the global variable “_paq”. But the variable doesn’t exist if you don’t use the snippet in the global context (the window scope).
The code will run if you change
var _paq = window._paq || [];
to
window._paq = [];
var _paq = window._paq;
Maybe I’m missing something I should have set before.
The current tracking code assumes that window._paq could possibly be set before. Is that really possible?
Best,
Christoph