0
votes

According to the documentation, Google Analytics asynchronous tracking code is "asynchronous" in the sense that you can push items to the _gaq array before ga.js is loaded. Once it is loaded, push calls are executed immediately:

At this point, _gaq is no longer an Array, but an [Analytics] Object, and instant execution of the tracking methods is possible.

Does this mean that calling push in onclick (after ga.js is loaded) can block my other code until the call is complete? Or is it really asynchronous? In this case, can navigating away from my page prevent the call to push from completing?

2

2 Answers

1
votes

I can confirm that navigating away from the page prevents Analytics from tracking anything. Possible workaround is to open the target page in a blank window using target="_blank" or wrapping the _gaq.push call in a custom function that waits for the onload of tracking gif. The latter causes a delay before the response to the user click, which might get more significant on high latency network (e.g. mobile).

0
votes

I found an unobfuscated version of ga.js. The tracking information is sent to Google using an image object that is not added to the DOM. A handler is added to the image's onload event. Based on this, I don't believe that calling push will block my code (at least not for long), and whatever it is doing in onload will be asynchronous. This means that navigating away from the page can possibly prevent some of my analytics tracking from being logged. If anyone can confirm these things, I will mark your answer as correct.