Tracking Outbound Links in Google Analytics using Events

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=&#8221;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/

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s