JtR
JtR

Reputation: 20676

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.

Upvotes: 5

Views: 491

Answers (1)

some
some

Reputation: 49592

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.

Upvotes: 1

Related Questions