Thursday, May 9, 2024
74
rated 0 times [  81] [ 7]  / answers: 1 / hits: 46314  / 14 Years ago, mon, march 29, 2010, 12:00:00

The Async Tracking code in Google Analytics looks like this:



var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


About The first line:



var _gaq = _gaq || []; 


I think it ensures that if _gaq is already defined we should use it otherwise we should an array.



Can anybody explain what this is for?



Also, does it matter if _gaq gets renamed? In other words, does Google Analytics rely on a global object named _gaq?


More From » google-analytics

 Answers
23

Yes, it ensures that _gaq is defined, so that _gaq.push() never fails.



I would not mess with the name of the variables in GA's code... do you have any reason to? Does it conflict with any of your variables? (Then I would change mine...)


[#97217] Thursday, March 25, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devonw

Total Points: 311
Total Questions: 116
Total Answers: 111

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;