Using Event Tracking in Google Analytics

Wow my second post I’m on a roll here (not !!!). Well it was a nice fantasy anyway. No one has commented on my first post so it’s looks like I’m getting away with it so far. Let’s see if commenting on Google Analytics (GA) will wake the the flame beast…

So I recently started to look at GA to track events on a website I’m helping out on. It took some effort to get all the accounts set up. I used stack Exchange and Google to help figure out exactly what to do in terms of getting the correct code snippets.

If you don’t use stack exchange then I have to say it is the most awesome site and for those who are too attention challenged to RTFM it is a life saver.

The first thing I did then was to add the following Asynch code snippet to my page to get the GA engine up and running.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXYYY-Z']); // This is where you put your Analytics Account

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

This alone is enough to get GA up and running and you will see that it starts to generate stats on your account in realtime. Now came the tricky part !!!

I next wanted to make sure that an event is triggered each time a particular advert was displayed by the page. so I wrote some server side code to add to the page the following events.

_gaq.push(['_trackEvent', 'AdvertCategory', 'Specific Ad','logged In']);
_gaq.push(['_trackEvent', 'AdvertCategory2', 'Specific Ad2','not logged in']);

I put these just below the _gaq.push(['_trackPageview']); line so they would be run each time the page loads.

Next came the 72 hour wait for events to start showing up… Not cool Google….

The second part of the problem was to send an event “On Click” the Google docs were pretty clear about how easy it was but I guess i was naive in thinking it would work….

Google Doc’s says add an event tracking on-click using the following code.

_gaq.push(['_trackEvent', category, action, opt_label, opt_value]);

However what they fail to mention is that if you attach it to a link that is to another site (an advertisers site for example) that the event will NOT get processed as it won’t get time. I lost hours here fiddling around and waiting for events to show up.

In reality you need to put a wrapper around the “on-click” event and trigger it followed by a short delay to trigger the link to the external site. Here’s what I used.

function wrapper_function(that,category,action,opt_label,opt_value) {
_gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
window.setTimeout("window.location.href='" + that.href + "'", 1000);

on the link it’s self it’s real easy then just add the following….

<A href="; onclick="wrapper_function(this,'AdvertClicked', 'Advertiser','Logged In');return false;">Insert Advert Image Here

So that’s pretty much it. I hope to check out flash based ad’s next to see if they will respond to this sort of strategy, let’s see !!!

Lastly, A big shout out to the folks that give good answers on the forums. You are cool and the world is a better place for the altruism you display. I hope some day soon my feeble skills will allow me to answer questions and not get down voted as a result (wink)….


Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: