Website Design Quick Tip: How to Track Outgoing Links with Google Analytics

Posted December 6, 2011 - 11:39 am. By Ryan Feyer. comments

Just a quick tip here for those interested in tracking outbound links on their website. Outbound, outgoing or external links are links on your website that point to another website. Traditionally, Google Analytics doesn’t track this, but below is a simple piece of Javascript you can add to your website to track these clicks as an event.


$(document).ready(function() {
$("a[href^=http]").not('a[href*="DOMAIN.COM"]').click(function() { 
tgt = $(this).attr('target'); 
logEvent('Outgoing', this.href, 'Link', tgt); 
if (tgt != '_blank') {return false;} 
}); 
}); 
function logEvent(cat, act, lbl, tgt) { 
_gaq.push(['_trackEvent', cat, act, lbl]); 
if (tgt != '_blank') { 
setTimeout('document.location = "' + act + '"', 100); 
} 
}

A few things to note:

  • This requires the use of Google Analytics’ asynchronous code.
  • This requires the use of jQuery.
  • Replace the ‚ÄúDOMAIN.COM‚Äù with your base URL. This is so it only collects clicks to links outside your website.
  • Data from this will be found in the ‚ÄúEvents‚Äù sub-section in Google Analytics.

You can also use a script similar to this to track PDF views, video plays, etc.

For more information on event tracking in Google Analytics, read the guide here.

Other links:

Leave a comment: