checking if the Google Analytics _gaq object is loaded and available
Solution 1
In the recommended javascript code you get from analytics, it includes the following row:
var _gaq = _gaq || [];
So, the array should always be available if you keep this line in your code. If you are adding the analytics code later, just add the line above before your main scripts and it will work.
Note that this snippet is harmless even if you defined _gaq
before, since it only defines it as a new Array it if it is previously undefined.
This is a great way to use asynchronous scripts, the array is defined first locally, and you can push commands to this array whenever you need. When the analytics script is loaded, it can use those commands when it wants. So no need for checking if the array is undefined or anything like that.
Solution 2
If you're using Universal Analytics (analytics.js
) then switch this:
_gaq.push(['_trackEvent', 'Downloaded Video', 'Yes']);
to this:
ga('send', 'event', 'Downloaded Video', 'Yes');
Solution 3
When ga.js
is loaded, it defines a window._gat
(Mind the T instead of Q) global object, you just need to test if it is defined or not (but beware it is loaded asynchronously, then you may delay your test).
Solution 4
You generally don't need to check if it's defined because the Google Analytics code snippet does this for you. However if you are doing something like event tracking in other sections of your stie as the question suggests it's sometimes good to check if the script is loaded since it's asynchronous.
I wrote a jQuery plugin wrapper for GA and wanted to check in the code whether or not GA was in fact installed before I tried to push any data at it. This was to cover the case that someone tried to use the plugin without GA installed or before GA was initialized.
if (typeof(_gaq) == 'undefined') {
console.error("Google Analytics is not installed");
}
Solution 5
As @David said, as long as you have the var _gaq = _gaq || [];
snippet defined earlier, you're fine and there's no need to check for _gaq
being undefined.
The reason for _gaq
being defined as an empty array if it was previously undefined is so that it has a push
method. This allows your analytics code to execute before the GA code is loaded, or in some case when it's not available.
When GA loads, _gaq
will be redefined as an object with a push
method, and any existing contents of the old _gaq array will be executed.
crmpicco
Senior Analyst Developer (MoodleCloud) at Moodle, AWS Solutions Architect (Associate), Zend Certified Engineer and Google Analytics Qualified Individual
Updated on June 29, 2022Comments
-
crmpicco almost 2 years
I have some Google Analytics Tracking Code (GATC) on my site which triggers calls to the
_gaq.push
method in Google's code.In the scenario that GA is not available, or
_gaq
has not loaded, I want to ensure that I do not have any JavaScript errors on the page. By checking that_gaq
is not identical to 'undefined' - will this suffice to check if it's available and is this x-browser? I've had a look at Google's documentation, but it doesn't mention anything about this.I'm aware of checking if the object is
null
, but i'm not sure if this is necessary.if (typeof(_gaq) !== 'undefined') { _gaq.push(['_trackEvent', 'Downloaded Video', 'Yes']); _gaq.push(['rollup._trackEvent', 'Downloaded Video', 'Yes']); }
-
crmpicco over 11 yearsI don't want to re-define the
_gaq
variable, as I have this set in the code that Google provide. I have the push calls elsewhere in my own code. Re-defining _gaq seems risky. -
David Hellsing over 11 yearsI’m not saying you should redefine it. My point is that if you already added it like the docs say, you can’t check for undefined anymore – and you don’t need to.
-
Mr.Hardy over 10 yearsThis should be marked as the correct answer. Thanks for your help.
-
crmpicco over 10 years@Mr.Hardy I'm not using Universal Analytics, so I can't accept this as the correct answer.
-
Jag over 10 yearsThis is the correct answer for me. We only render tracking scripts (google plus others) on production sites thus on dev/UAT _gaq is not defined. And this works perfectly. thanks
-
Open SEO about 10 years_gaq is created by the tracking snippet, not when ga.js is loaded. This is not enough. See stackoverflow.com/a/13450713/307687 instead
-
AsTeR over 9 yearsdevelopers.google.com/analytics/devguides/collection/… the reference suggests the third argument to be an object and no forth.
-
landed almost 9 yearsthe above only works like this for me ga('send', { 'hitType': 'event', // Required. 'eventCategory': 'button', // Required. 'eventAction': 'click', // Required. 'eventLabel': 'nav buttons', 'eventValue': 4 }); but it looks like you can pass a string like this ga("send","event".... that didnt work for me...