How google analytics asynchronous event tracking works, and why it doesn't work

Home

How google analytics asynchronous event tracking works, and why it doesn't work

Feb242011

I've just had a look at the code, as I'm trying to get google analytics event tracking working:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-266418-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


First, I just figured out what it's doing. _gaq starts off as an array, you push things to it. Then it loads google-analytics.com/ga.js which runs. This looks in the page for the array _gaq, processes the things in the queue, then removes it and sets up an object called _gaq in its place. This object has a method called "push" so you can still call _gaq.push() later on in your page. Clever stuff. So why's it not working for me? None of my events are showing up. I'm doing this:

window._gaq && _gaq.push(['_trackEvent', cat, name, val]);

and my events are not being tracked... window._gaq must be present in order for _gaq.push to ever work right? So what's going on? Just checking that cat, name, and val are all of a legitimate type.

Hard to test this stuff as I think you can only make changes today, then see if it worked tomorrow. Frustrating.

:: :: Comment / reply

Over to you, discussion about anything local, but please be considerate... Folkestone on TV, Kent gigs, Find a flatmate, rent a room, property queries, current news, what's new, local events, for sale, local services, what I like about Folkestone, ask questions, offer advice, plug yourself... the more it's used the more useful a resource it will become. If you want a gig listed click here to add it (fixed now!), or just click here to post a message...

Also from the same week

Some more posts from the same week as .

RE: Has The Harbour been renamed The True Brit? [Tuesday 22 February 2011]

Interesting rumour about Noel Gallagher! Always seems to be someone moving to Sandgate... [Tuesday 22 February 2011]

RE: Interesting rumour about Noel Gallagher! [Wednesday 23 February 2011]

Any celebrity neighbours add a little something to the area [Wednesday 23 February 2011]

Has The Harbour been renamed The True Brit? [Wednesday 26 January 2011]

RE: Has The Harbour been renamed The True Brit? [Thursday 17 February 2011]