Professional Web Design & Development

Need help with a project? We provide custom design and development services including WordPress & Drupal CMS, Magento Commerce Solutions and Mobile application development.

Request Quote

How to Track Clicks on Outgoing Links

This is a cool howto, for you to use Google Analytics itself to track outgoing links, that too without adding the necessary JavaScript to each and every link. This is pretty easy, you will utilize Google Analytics and jQuery. This method will add outbound link tracking to any website. Make sure that your links to other sites open a new window, which will enable you to track them. to jQuery.com and download the latest version. Then add the script included into the header of your website.

<script type=”text/javascript” src=”js/jquery.flash.js”></script>

Now on your Google Analytics, if you do not have an account just set up an account and follow the directions to get your website tracking done.

Everyone gets script that looks like following:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
var pageTracker = _gat._getTracker("YOUR ACCOUNT NUMBER HERE");
pageTracker._trackPageview();
</script>

In the header section of your webpage, you will need to add a few lines of JavaScript, to add JQuery. You will need to setup the jQuery .ready function and add the code to that.

<script type="text/javascript">
$(document).ready(function(){
});
</script>

Simply use a jQuery selector to select all a tags on the page with a rel=”external”. To do this, pass jQuery the a tag element with the attribute selector option.

$(a["rel*='external'"])

with this you are basically adding a click function to our jQuery object.

$(a["rel*='external'"]).click(function(){
}):

Lastly you need to just put in this function call to the Google Analytics code and we pass it the href that our a tag is pointing to, as follows:

pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));

This code makes the call to Google Analytics and passes a page path of “/outgoing/” and the url of where the click was headed. In your Google Analytics under content, you will see pages that start with “/outgoing/”. These are clicks that took people off your site. See below for how it looks in Google Analytics.

We hope you find this useful!

You might also like:

  1. Make your JavaScript to be XHTML compliant
  2. CSS Pagination Links
  3. 15 CSS Tools to save time and effort : Really useful
  4. Export Illustrator Files to Photoshop
  5. WordPress 2.6 Features List : Review

3 Comments

  1. Hey.

    cool stuff.

    I tried to do something similar, and just have one question. Is it still possible to see where the users came from, when they clicked the outgoing link?

    Regards

    Martin

  2. Great article for tracking outgoing links. Can this scripting idea be used in emails as well?

  3. David

    This works for simple links, but what about links that have both href input and javascript actions (e.g. onclick, onmouseover, onmouseout). This wouldnt work. You need a script that disables those javascript events initially, runs your custom code on click, then re-enables the javascript events – of which should immediately run. Have you solved for this as yet? Is there any way to catch outbound events server side ii6 AND iis7?

Leave a Reply