Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How google analytics' asynchronous api is able to track clicking links

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.

like image 578
JtR Avatar asked Nov 15 '22 06:11

JtR


1 Answers

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.

like image 160
some Avatar answered Dec 15 '22 14:12

some