Industry Leader in Digital Business

Tracking external links with Google Analytics

When starting with Google Analytics, the tracking statistics might seem complex and overwhelming at first. However, after a certain time it becomes clear that important information is missing. Lately, I ran into the problem that the analytics are by default not tracking any external links, making it hard to define their usefulness on websites. Certain that Google had an solution to this problem, I searched and found information about external link tracking, explaining the event tracking methods provided by the Google Analytics API. Though the provided information does indeed do a great job at tracking external links and other events, implementing it on a large website would be an extremely hard task since every external links must be equipped with an ‘onclick’-event. Using some basic JavasScript knowledge, I came up with the following script that automatically adds this event to all absolute links (starting with the protocol ‘http://’) on the page. Please note that your website should use relative links for internal links to prevent them from being tracked with this script and that (for Internet Explorer compatibility) you should edit the cURI value at the top.

{code}var uTracker = uTracker ? uTracker : {

// Main domain (excluded from tracking)
cURI: ‘’,

initLinks: function() {
var allTags = document.getElementsByTagName(‘a’);

// Check all links
var cTag, cLink;
for (var i = 0; i < allTags.length; i++) {
cTag = allTags[i];
cLink = cTag.getAttribute(‘href’);

// Potential external link
if (cLink && cLink.substr(0, 7) == ‘http://’) {

// Exclude internal links
if (cLink.substr(0, uTracker.cURI.length) == uTracker.cURI) {

// Add event = ‘_blank’;
uTracker.addListener(cTag, ‘click’, uTracker.recordOutboundLink);


recordOutboundLink: function(e) {
try {
e = e ? e : window.event;
var cTarget = ? : e.srcElement;
pageTracker._trackEvent(‘External links’, cTarget.href);
} catch(err) {}

addListener: function(el, type, expression) {

// Add event (FF & other)
if (window.addEventListener) {
el.addEventListener(type, expression, false);
return true;

// Add event (IE)
if (window.attachEvent) {
el.attachEvent(‘on’ + type, expression);
return true;

return false;
uTracker.addListener(window, ‘load’, uTracker.initLinks);{/code}

The script is rather self-explanatory (especially due to the comments explaining every codeblock) and can be easily extended to also track other protocols (like ftp or https) or other types of links (anchor links for example). I tested the script in FireFox 3.6, Internet Explorer 7 and Chrome, so it should work for the majority of the browsers. Even so, if the script fails to execute it will not add the events, thus enabling the browser to follow the original HREF location specified without disrupting the visitors experience. Any feedback, suggestions for improvements or questions are more than welcome.

Modification (22/04/2010): Added domain variable to prevent tracking of internal links in Internet Explorer (which will add the domain to the HREF-attribute automatically in some cases even if relative URL’s are used). Also removed the usage of pop-up as many browsers nowadays block pop-ups.

Comments (1)

Great post! Very useful when you want to track the effects of (several) advertisements on your webpage.

Comments are closed.