I’ve recently been working on a website that needs to track external link clicks in Google Analytics. This is something that the WordPress.com stats plugin does by default, but not Google. In order to track external links with Google Analytics, you’ll need to set up a custom tracker event.

The method suggested by Google involves manually applying code to each link. This is fine if you want to just track a few specific links, but jQuery is more useful if you want to track every external link on the page.
Custom Tracking Events
The meat of the custom event is this line (which should be fired when an external link is clicked) is:
_gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
- “Outbound Links” is the category of events to track
- “e.currentTarget.host” is the “action”, in this case, the domain the user is clicking to.
- “url” is the “label”, which I use to send the full url of the external link.
If you’re unfamiliar with custom events, here’s the docs page at Google.
Code for Detecting and Tracking Outbound Link Clicks
This code snippet is highly commented and peppered with console.logs so that you can verify it’s working correctly and see how it works. It should only be used in development environments- there’s a compressed version in the next section for use on live sites.
// Outbound Link Tracking with Google Analytics
// Requires jQuery 1.7 or higher (use .live if using a lower version)
// For more info see: http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55527
$("a").on('click',function(e){
var url = $(this).attr("href");
// Console logs shows the domain name of the link being clicked and the current window
console.log('e.currentTarget.host: ' + e.currentTarget.host);
console.log('window.location.host: ' + window.location.host);
// If the domains names are different, it assumes it is an external link
// Be careful with this if you use subdomains
if (e.currentTarget.host != window.location.host) {
console.log('external link click');
// Outbound link! Fires the Google tracker code.
_gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
// Checks to see if the ctrl or command key is held down
// which could indicate the link is being opened in a new tab
if (e.metaKey || e.ctrlKey) {
console.log('ctrl or meta key pressed');
var newtab = true;
}
// If it is not a new tab, we need to delay the loading
// of the new link for a just a second in order to give the
// Google track event time to fully fire
if (!newtab) {
console.log('default prevented');
e.preventDefault();
console.log('loading link after brief timeout');
//setTimeout('document.location = "' + url + '"', 100);
}
}
else {
console.log('internal link click');
}
});
Compressed Version
This is the same code as above, but with comments and console.logs stripped out:
// Outbound Link Tracking with Google Analytics
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$("a").on('click',function(e){
var url = $(this).attr("href");
if (e.currentTarget.host != window.location.host) {
_gat._getTrackerByName('demand')._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
if (e.metaKey || e.ctrlKey) {
var newtab = true;
}
if (!newtab) {
e.preventDefault();
setTimeout('document.location = "' + url + '"', 100);
}
}
});
Viewing the Results
To see if your custom tracking events are firing correctly, check your Google Analytics dashboard under “Content > Events > Overview”. It may take a couple hours before you start to see the results. In this screenshot you can clearly see when the tracking began:

In WordPress
If you are using a plugin for Google Analytics, like Google Analyticator, there is a settings fields where you can add javascript to be included with the tracker. You can also just include it with your other scripts.
Another option to track links easily in WordPress is to use the stats from Jetpack, which give you outbound click data by default.
Awesome !
I just put it on my website…
Do you this code after or before the GA tracking code ?
Thanks
It doesn’t matter. I’d recommend combining it with any other scripts you have, or if you need to put it inline, in the footer below the close body tag.
Nice clean quick code! However, it does not appear to be respecting the target attribute of the link. Specifically, target=_blank.
I’d did this quick mod…
Added : var target = $(this).attr(“target”);
and Changed : if (e.metaKey || e.ctrlKey || target==”_blank”)
Do you know is there is a way to track email clicks for WordPress.COM? Ie, if I want to market my site to specific people via email, but I want to know who clicked through (or even IF people clicked through via an email link), is there a way to do this?
Thanks!
Kristen
Most bulk mail services (like MailChimp) allow you to track outbound links. You could also set up a Bitly shortlink which records visits.
This code is not working in IE 7 and 8.