Skip to content

Tracking Outbound Links in Google Analytics using Events

October 21, 2011

How to implement event tracking on outbound links in Google Analytics? Easier than you might think!

1)      Make sure you have the most recent Google tracking code (called Asynchronous Code) in the head of every pages having links you want to track. Should look like this:

script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-XX']);
_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);
})();
</script>

2) Right after the above code, insert the following javascript function (the name of the function could be changed to your liking)

script type=”text/javascript”>
function recordOutboundLink(link, category, action) {
try {
_gaq.push(['_trackEvent', category, action])
setTimeout(null, 100)
} catch (err) { }
}
</script>

The function has 3 parameters:

-          link: the link itself

-          category: the category in which the event will be classified in Analytics

-          action: the action associated to the link

I used “push” method and it worked right away, but “getTrackerByName()” may also do the trick but haven’t investigated it yet…

The second line delays the click for a fraction of a second to allow the page to fully load the tracking code. I’ve used “null” as first parameter, otherwise the page wasn’t opening in a new window.

3) Then on the link you want to track, add the onClick event calling the above function:

<a href=”http://www.yoursite.com” onclick=”recordOutboundLink(this, ‘Outbound Links’, ‘www.yoursite.com’); window.open(this.href); return false;“>www.yoursite.com</a>

When the above link gets clicked, recordOutboundLink is called with the link (this), the category (This is an outbound link) and the action (I used the URL to which the link points to).

Make sure “return false;” is the last statement. If you want the page to open in a new window, add “window.open(this.href);” after the function call, just before “return false;”

And that’s it! Event tracking in Analytics should start to get some data!

Using this method makes it easy to track all sort of different links across your website. Simply call recordOutboundLink javascript function on the link you wish to track and pass a different category and action to it. Whether it is a video, a mailto or an image, you can now have a better idea of what your users are up to when visiting your website!

References:

http://www.google.com/support/analytics/bin/answer.py?hl=en-GB&answer=55527

http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html

http://www.stepforth.com/blog/2010/outbound-link-tracking-event-tracking-google-analytics/

Christchurch – Queenstown bike ride – preparation

April 17, 2011

This time, the weather will be different. Colder, weter, more winds and more rain. This is what I am expecting at least. On the morale boosting side, breathtaking landscape for amazing photographs, low uphill gradiants (from what I’ve heard) and great food. New Zealand is supposed to be a great country to cycle around, probably the best way of transport to discover and fully enjoy the experience. Let’s experience it now.

The plan is simple: push my way from Christchurch to Queewstown and have fun in the process! Christchurch – Geraldine – Lake Tekapo – Omarama – Wanaka – Queenstown is the itenerary. Because I am going for such a short period, I’ve decided to rent a bike rather than going through the pain of boxing, dealing with the box at the airport, unboxing, assembling and doing the same process all over again before coming back. I gearer up on clothing though, bringing all I need to stay warm and dry.

Something I missed in my previous bike trip was video. So this time, I won’t be empty handed on that side. I just got a GoPro Hero which I will mount on the front of the bike or my helmet. Can’t wait to see the results!!!

Follow

Get every new post delivered to your Inbox.