Google Analytics Workshop

register | login

Blog RSS logo

Tracking outbound links...

Christine Cahoon   Thu 12 Jul 2012   updated: Wed 29 Oct 2014

A common question during the workshops is how do we track outbound links, those links that go outside your web site.

What you need to do is add some custom JavaScript that uses the _trackEvent method to record these links and then modify the links you want to track.

First, within the tracking code, you need to delay the outbound click by a fraction of a second. This provides the browser more time load the tracking code. Without this method, it's possible that a user can click on the outbound link before the tracking code loads, in which case the event will not be recorded. This additional code in the part should look like:

function recordOutboundLink (link, category, action) {

try {

var myTracker=_gat._getTrackerByName ();

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

setTimeout ('document.location = "' + link.href + '"', 100)

} catch (err) {}

}

Next, you need to revise outbound links to call the new function without first following the link. For an example, log every click on a particular link to www.example.com, you would use the _trackEvent() method in the link's tag, such as:

The example above uses the category label 'Outbound Links'. This is a useful way to categorize all outbound links in the Event Tracking reports. It sets the specific name of the website as the second parameter in the call. With this structure in place, you could then see Outbound Links as one of the event categories and drill down to see which particular outbound links are the most popular. Be sure to use return false for the onClick handler, because without that statement the browser will follow the link before the recordOutboundLink method has a chance to execute.

For a fuller explanation, see in Google's help centre: Setting Up Tracking for Outbound Links.