views:

967

answers:

6

The Async Tracking 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?

+3  A: 

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...)

Victor
it would still fail if _gaq was declared before but wasn't an array but a number or string :)
pawel
I don't have issue per se with '_gaq' but I have found nowhere in the google analytics documentation a warning about having a variable called _gaq.
parvas
That would be like an open invitation to hackers, like the robots.txt. You could interpret "please don't use xxx" as "our code is vulnerable when you use xxx". :-)
Prutswonder
prutswonder: can you please elaborate.
parvas
Well, if Google did issue a warning about using the _gaq variable, wouldn't you be tempted to experiment with it?Just like the robots.txt: If you could read it, and it contained the line `Disallow: /admin/`, wouldn't you try adding '/admin/' to the URL to see what it did?
Prutswonder
There is no documentation that says you shouldn't have a variable named _gaq, but if you want to use GA (including the non-async version), you should avoid using that namespace.
Brian
A: 

This means that if _gaq is already defined it use that else it declares an empty array. With push you can override settings. If the _gaq object wasnt defined the 2 "lines" after that would result in an error.

Yes the _gaq object is expect in the script which you include in the code there (ga.js).

Fabian
A: 

Yes, it does exactly what you think it does :) It is a shorthand for

if(!_gaq){ var _gaq = [] }
pawel
okay. fair enough. what is it for ?
parvas
What is the _gaq variable itself for, or the assigment of an empty array to this variable? It looks like the tracking script uses _gaq global variable to hold some data before sending them to the server. Compressed source is a bit hard to follow, but there is var ba="_gaq" on the first line, and var i = window[ba]; (which returns the _gaq global variable) on the last line. Making _gaq an array if it is not defined just makes it possoble to use .push() method on it.
pawel
A: 

Using || in assignment is a common programming trick which takes advantage of the evaluation direction of the operator, which is left to right. That means that it evaluates the left side first. Then, and only if that is false (or a false equivalent), does it evaluate the right side.

You can also take advantage of the || or && operators in a simple if statement, so that

if (a > 5) {
  do_a();
}

if (!some_boolean) {
  do_b();
}

become

a > 5 && do_a();
some_boolean || do_b(); // Note that the negation operator `!` is gone!

which are both way nicer to look at.

The reason languages allow this, is because it is a waste of time evaluating the right side if the left side will make the entire line fail anyways. So it just ignores it unless it's needed.

Tor Valamo
+4  A: 

This line is there to allow multiple GA snippets in the same page. It ensures that the second snippet doesn't overwrite a _gaq defined by the first.

GA asynchronous tracking works by first defining _gaq as an array. This array acts like a queue, which allows you to push (append) configuration and tracking "commands" (like _trackPageview) onto the end of the queue. Your commands are stored in this array until ga.js fully downloads.

When ga.js is ready, it executes all the commands in the _gaq array and replaces _gaq with an object. This object also has a push method, but instead of queueing up commands, it executes them immediately, because ga.js is available to process them.

This mechanism allows you to make configuration and tracking commands without knowing if the browser has finished downloading ga.js. This is needed because the async snippet downloads ga.js without blocking other code on the page from running. Things would get hairy if that other code (your configuration commands) needed to know the state of ga.js being downloaded.

All of this absolutely does depend on the use of the name _gaq. You shouldn't try to name it if you want asynchronous tracking to work.

Brian
A: 

gaq is google analytics queue. it's a stack for GA methods, like event tracking, page attribution, etc. you use the push() method to put GA stuff on there. reduces event interference, everyone should do this, or at least learn the concept.

duggi