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

<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) {} }

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')+''; 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', '');" href="" 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="" target="_blank">web development</a>

Comments: 2

  • Joe
    February 18, 2013 12:44 am

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


Post a Comment