What I am trying to do is to redirect the user to the next page right after sending an event to Google Analytics:
_gaq.push(['_trackEvent', 'xxx' ...]);
window.location = 'some url ...';
The above code manages to register events because I can see them in the GA report. However I suspect that some events were lost because the browser redirects the user to the new page before the track pixel loads.
My questions are:
- Does _gaq.push() block until the event has successfully reached Google’s server?
- If not, what is the best way to make achieve what I need?
Thanks!
This is my observation based on some quick research. That function is part of Google Analytic’s asynchronous API, so it should not block anything, otherwise, it’s not async. 🙂
Digging through the obfuscated ga.js, you can kinda see that the tracking is probably called onbeforeunload, which fires when you leave a page.
It really doesn’t matter if the tracking pixel loads completely, it’s a fire & forget. As long as the request is initiated and reaches Google’s server, it’ll get tracked. The response (in this case, the pixel image) is a by-product.