@Findus23 opened this Issue on July 7th 2020 Member

When putting the default Matomo tracking code into a function, tracking stops working as matomo.js expects window._paq to exist and the variable is just local inside the function.

For comparison the default Google Analytics tracking code:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');

slightly more readable form:

window['GoogleAnalyticsObject'] = 'ga';
window['ga'] = window['ga'] || function () {
    (window['ga'].q = window['ga'].q || []).push(arguments)
}, window['ga'].l = 1 * new Date();
a = document.createElement('script'),
    m = document.getElementsByTagName('script')[0];
a.async = 1;
a.src = 'https://www.google-analytics.com/analytics.js';
m.parentNode.insertBefore(a, m)

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');

completly unminified form:

window.GoogleAnalyticsObject = 'ga';
window.ga = window.ga || function () {
    (window.ga.q = window.ga.q || []).push(arguments)
};
now = new Date()
window.ga.l = now.getTime();
scriptTag = document.createElement('script');
firstScript = document.getElementsByTagName('script')[0];
scriptTag.async = true;
scriptTag.src = 'https://www.google-analytics.com/analytics.js';
firstScript.parentNode.insertBefore(scriptTag, firstScript)

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
@tsteur commented on July 8th 2020 Member
This Issue was closed on July 9th 2020
Powered by GitHub Issue Mirror