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

I've just had a look at the

(/wiki/#code), as I'm trying to get [google](/wiki/#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 http://www.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.

javascript: Programming language of the web, mostly how I make my living.

⬅️ :: ➡️

Paul Clarke's blog - I live in A small town. Wed to Clare and dad to 2, I'm a full stack web developr, + I do mostly js / nodejs, some ruby, python, php etc. I like pubbing, parkrun, eating, home automation and other diy jiggery-pokery, history, tree stuff, Television, squirrels, pirates, lego, and TIME TRAVEL.