Analytics - _trackEvent before _setAccount? Does order mean?

So this will work:

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-65432-1']); _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); })(); _gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']); 

But what if a visitor can follow a link, for example http://example.com?autoPlay=Wind ? So, the video has already been requested and starts playing, generating:

 var _gaq = _gaq || []; _gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']); 

event. But then the normal tracking code is still executed at the bottom of the page, so we have async for:

 var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-65432-1']); _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); })(); 

So do I need to move at least _setAccount to the top of the page, or will it work, as it is already in the queue, by the time I call ga.js ? Or I could just create my own queue ... But here I see a key that they made sure not to delete _gaq if something is already in it before _setAccount . And also there are several trackers for different contexts ... So, this means that it is completely legal to ignore the order at all? Sorry for the simple question, but I cannot find anything in what is allowed with async and what is not.

Thanks for the help!

+4
source share
2 answers

You must have _setAccount in front of any commands that log data, such as _trackPageview or _trackEvent .

If you look at the tracking pixel sent by _trackEvent when _setAccount is not already running, you will see that it registers with the default account, something like UA-XXXXX-X .

I used this in development or creation systems where I want the analytics code to run, but I don't want it to pollute production tracking data. Comment out the line _setAccount , and all data will end up logging to a non-existent account.


Assuming you never initialized a tracker named "mumble" (with _gaq.push(['mumble._setAccount', 'UA-65432-1']) , the following code shows the id of an uninitiated tracker: "UA-XXXXX-X" :

 echo(_gat.getTrackerByName('mumble')_getAccount()) 
+5
source

Now he does:

 var _gaq = _gaq || []; if(_gaq.unshift){ _gaq.unshift(['_setAccount', 'UA-65432-1']); } else { _gaq.push(['_setAccount', 'UA-65432-1']); } _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); })(); 

So the solution is that while push added to the end of the array, and unshift does this before the start.

The problem may be that in the case of iframe, ga is already loaded, and then it's not an array, but an object with a custom push method. In this case, let's just go back to the source code. On your homepage, the account id was the same and it works anyway.

+2
source

All Articles