Tracking Outbound Links that Open in New Windows using Google Analytics

Many websites still open external links in new windows and some of the most popular conventional methods of outbound link tracking is to use a little function that is called in the onClick attribute of a link. The common issues website owners face include that either the link does not resolve to it’s destination or link opens destination in a new window and the parent goes to the destination too. It would also be recommended to factor in a delay of a click to ensure that Google Analytics tracks the click. The delay should not be noticed by a user being a fraction of a second.

Example below of delay is incomplete and will not work, based on http://www.google.co.uk/support/analytics/bin/answer.py?answer=55527

<script type="text/javascript">

function recordOutboundLink(link, category, action) {   try{     var pageTracker = _gat._getTracker("UA-XXXXX-X");     pageTracker._trackEvent(category, action);     setTimeout('document.location = "' + link.href + '"', 100)   } catch(err) {} }
</script>

Using an Asynchronous Tracking Code

Below is an example of an a HTML5 asynchronous Google Analytics tracking code.

<script type="text/javascript">

var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']]; 
// Change UA-XXXXX-X to be your site's ID

(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1; g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js'; s.parentNode.insertBefore(g,s)}(document,'script')); </script>

 

Format 1
Uses _gat, the global object is used to create and retrieve tracker ojbects

<a onclick="_gat._getTrackerByName()._trackEvent('[Category]','[Action]', '[opt_label]');" href="[URL]" target="_blank">[link text]</a>

<a onclick="_gat._getTrackerByName()._trackEvent('Outbound Links', 'clicks', 'https://www.sumobaby.net/');" href="https://www.sumobaby.net/" target="_blank">web design</a>

 

Format 2
Uses the global command array (_gaq.push) to call the _trackEvent method

<a onclick="_gaq.push(['_trackEvent',[Category]','[Action]', '[opt_label]');" href="[URL]" target="_blank">web development</a>

<a onclick="_gaq.push(['_trackEvent','Outbound Links','click',this.href]);" href="https://www.sumobaby.net/" target="_blank">web development</a>

2 Comments

  1. Joe
    February 18, 2013 - Reply

    What is the difference between “Format 1” and “Format 2”?
    ie. Why are there two different formats?

    Thanks

    • Sumobaby
      February 18, 2013

      Format 2 makes use of the global command array (_gaq.push) to call the _trackEvent method where as _gat._getTrackerByName()._trackEvent accesses the method directly.

      Since this post was written I might advise adopting format 2 as a way of working.

Drop a comment

Your email address will not be published. Required fields are marked *