ga-hacks.gifA word of caution – This is a tech tip and requires you to have a knowledge of html and javascript to implement and use it…

[This hack is for the legacy urchin.js tracking code.
Always refer to the Scripts & Downloads section for the latest version.

April 2011 – New async hack launched
This hack has been completely revise for the latest GA async code. Further info >>


Your site may offer a visitor a link to click through to an external web site such a subsidiary, an affiliate, advertiser or an trade organisation. For Google Analytics, a visit leaving your web site requires an edit to the page in order to track it. This is achieved by modifying your outbound links to call urchinTracker and is extremely easy to do. However, what if your web site has hundreds of separate outgoing links that are constantly evolving and being appended to? Clearly manually modifying each link becomes laborious and inefficient. To overcome this you can apply the example JavaScript code below to your web site:

<script type="text/JavaScript">
// Only links written to the page (already in the DOM) will be tagged
// Script can be called multiple times
function addExtLinkerEvents() {
var as = document.getElementsByTagName("a");
var extTrack = [""];
// replace with your web site domain
for(var i=0; i<as.length; i++) {
	var tmp = as[i].getAttribute("onclick");
	// Track links off site – i.e. no GATC
	if (tmp != null &#038;&#038; tmp.indexOf('urchinTracker') > -1) continue;
	for (var j=0; j<extTrack.length; j++) {
		if (as[i].href.indexOf(extTrack[j]) == -1) {
			var splitResult = as[i].href.split("//");
			as[i].setAttribute("onclick",((tmp != null) ? tmp : "") + 
			  "urchinTracker('/ext/" + splitResult[1]+ "');");
			// the above must be on one line - from as[i]... to ");

The script works by looking for links within the browser’s Document Object Model (DOM) that do not match the domain value given in the variable array extTrack. If the link does not match extTrack then it is considered an external link and so is modified to include the urchinTracker call. By this method, all external links will show in the Google Analytics reports as:


Where the-url-that-is-on clicked on is minus ‘http://’. You can modify the JavaScript to adjust the path as required,

IMPORTANT: The position of this code within your page is important. The code must be placed after your call to the Google Analytics Tracking Code (GATC). Alternatively, you can place the addExtLinkerEvents() call in an onLoad event handler and host the provided JavaScript in a separate file. As an example I show this below, assuming the javascript is hosted in a file called trackExternal.js, as follows:

<script src="" type="text/JavaScript">
<strong><script src="/trackExternal.js" type="text/JavaScript"></script></strong>
<script type="text/JavaScript">
	_uacct = "UA-XXXXX-Y";
<body onLoad=”addExtLinkerEvents()>
	<em>...your remaining web page content...</em>

A note on performance: Each time your page loads, this script will go through all links referenced on the page to see if it is external. Clearly the more links on your page, the harder the script must work. As long as the number of links on each page number in the hundreds and not thousands, performance should not be a problem.

Also note that for pages with a large number of links, it is possible that visitors will click on an external link before the script has modified it. The result is that click through will not be tracked by Google Analytics which is an accuracy consideration that effects all web analytics vendors.

Did you find this tip useful? I am considering writing more of these tech tips if you feel they are useful. Please provide your feedback with a comment.