Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if analytics.js is loaded

I have to use a local analytics.js that I serve from my server. I just want to use the local version if necessary - so is there a solution for checking if the call for analytics.js has failed?

I thought about checking it with a global window.onerror, but I don't think a failed call for an external file causes an error. I've tried checking if ga() is available, but it is even if analytics.js isn't loaded.

Any ideas? If you are wondering, not all users of this site has internet access, that's why I'm serving a local version. There is more things happening in this case, like adding a sendHitTask to redirect the answer from analytics.js to the local server.

EDIT A solution where you check if the user has Internet access would also be OK. But I have not found any solution for this either that works on all modern browsers.

like image 972
Tony Gustafsson Avatar asked Mar 26 '15 15:03

Tony Gustafsson


People also ask

How do I know if GA4 is working?

After you have installed GA4, click the Preview button in the top right corner. A popup will ask you to enter the URL which you want to test and debug. It might be the address of a homepage or it might be a specific page's URL, and then press Start.

How long does it take for Google Analytics to update?

Processing latency is 24-48 hours. Standard accounts that send more than 200,000 sessions per day to Analytics will result in the reports being refreshed only once a day. This can delay updates to reports and metrics for up to two days.

How long does it take Google Analytics to start tracking?

Many of your reports and explorations can take 24-48 hours to process data from your website or app. You can use the Realtime and DebugView reports to confirm that you're collecting data from your website or app successfully.

How do I check Google Analytics tags?

Use Google Tag Assistant Google Tag Assistant is a browser extension for Chrome. Once you install and enable this free extension, it will check the implementation of tracking scripts on pages you visit. After loading a page and clicking on Google Tag Assistant, you'll see a report about the tags found on the page.


2 Answers

There's a function to track if the library has loaded. From the docs:

ga(function(tracker) {
   var defaultPage = tracker.get('page');
});

The passed in function is executed when the library is loaded, so you could set a variable to keep track of whether or not it has loaded. You'd have to put it on some sort of timer to decide when you want to consider it failed:

var loaded = false;
ga(function() {
   loaded = true;
});

// after one second do something if the library hasn't loaded
setTimeout(function(){
    if (!loaded){
        //do something
    }
},1000);
like image 92
maembe Avatar answered Sep 30 '22 04:09

maembe


instead of waiting for a callback, you can easily get it with

if(window.ga && ga.loaded) {
    // yeps... it is loaded!
}

you can easily see this in the Firefox documentation

same trick can be applied if you want to see if the tracker is blocked (by any plugin for example)

if(window.ga && ga.q) {
    // yeps... blocked! >:o
}
like image 42
balexandre Avatar answered Sep 30 '22 03:09

balexandre