Load matomo on document ready


#1

How can I load matomo on document ready?
I have to load the script when $.cookieAccepted() is true (I’ve checked its value and it’s true), but seems the matomo.php not reached.

Here is the code snippet:

$(document).ready(function () {
    if ($.cookieAccepted()) {
        var _paq = window._paq || [];
        /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
        _paq.push(['trackPageView']);
        _paq.push(['enableLinkTracking']);
        (function() {
            var u="//analytics.myCompany.com/";
            _paq.push(['setTrackerUrl', u+'matomo.php']);
            _paq.push(['setSiteId', '3']);
            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);
        })();
    }
});

(Fabian Dellwing) #2

You code seems perfectly fine to me, can you send me the URL of your site via PM so I can take a look?


(Fabian Dellwing) #3

Ok, I can confirm that there is some problem.

If I run Matomo.getAsyncTracker().getTrackerUrl(); on your site I get an empty string instead of //analytics.myCompany.com/matomo.php, so there will be nothing tracked.

I currently have no idea why the tracker is not setup the way it should. If I set the tracker URL and the site id via devtools I’m able to track pageviews.

So if you know your job with JS, you might just workaround the problem with something like this

var mt = setInterval(function () {
    if (Matomo.initialized) {
        clearInterval(mt);
        Matomo.getAsyncTracker().setTrackerUrl('https://analytics.***.fr/matomo.php');
        Matomo.getAsyncTracker().setSiteId(1);
        Matomo.getAsyncTracker().enableLinkTracking();
        Matomo.getAsyncTracker().trackPageView();
    }
}, 250);

Maybe @Lukas has an idea why the _paq pushing itself is not working.


#4

Ok for instance I’ve modified my script like this:

$(document).ready(function () {
    if ($.cookieAccepted()) {
        var _paq = window._paq || [];
        /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
        //_paq.push(['trackPageView']);
        //_paq.push(['enableLinkTracking']);
        (function() {
            var u="//analytics.mySite.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);
        })();
        var mt = setInterval(function () {
            if (Matomo.initialized) {
                clearInterval(mt);
                Matomo.getAsyncTracker().setTrackerUrl('https://analytics.mySite.com/matomo.php');
                Matomo.getAsyncTracker().setSiteId('1');
                Matomo.getAsyncTracker().enableLinkTracking();
                Matomo.getAsyncTracker().trackPageView();
            }
        }, 250);
    }
});

Thanks for your investigation!