If I have html like the following on my own site how come it is able to track the event? Is it possible that browser starts loading the http://google.com in the same window before the tracking request is made as it is asynchronous?
<a href="http://google.com" onclick="_gaq.push(['_trackEvent', 'category', 'event']);">click</a>
In case of using the synchronous API the browser obviously waits until the script in the onclick is ran, but I can't wrap my head around this one.
When you load the google-analytics.com/ga.js-script it replaces the array variable _gaq with an object. That object has a function named push
, where all the magic is happening.
If you want to see that code you can do alert(_gaq.push)
after ga.js has been loaded.
By the way, if you serve your pages compressed you might want to check out how to include the ga.js script in shortest possible way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With