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’]);
(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);

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) { }

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=”” onclick=&#8221;recordOutboundLink(this, ‘Outbound Links’, ‘’);; return false;“></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 “;” 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!



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