Tracking Exit Links in Google Analytics

Posted by danny on August 30, 2008 in Google Analytics, Web Development |

I’ve been spending my time writing a script that will help track exit links (external links) in Google Analytics. This script will traverse through all anchor tags within the body and apply tracking codes on tags with the href attribute.

If the anchor tag has an onclick attribute already, it appends the tracking code to it and it shouldn’t affect the functionality.

I will try to post screenshots of how it looks in Google Analytics once I gather some data. To use it, simply paste the following code after the google analytics code and before the </body> tag.

<script type="text/javascript" src="<PATH-TO-SCRIPT>"></script>
<script type="text/javascript">
	dtalk_ga.applyVirtualExits();
</script>

You can view the source code here,

/**
 *	@author:	Danny Ng (http://www.dannytalk.com)
 *	@date:		30/08/08
 *	@updated:	26/09/08
 *	@notes:		Free to use and distribute without altering this notice. Would appreciate a link back.
 *	@usage:		Place the following code before the </body> tag and after google analytics code,
 *
 *				<script type="text/javascript" src="<PATH-TO-SCRIPT>"></script>
 *				<script type="text/javascript">
 *					dtalk_ga.applyVirtualExits();
 *				</script>
 */

var dtalk_ga = {
	applyVirtualExits: function() {
		var trackPage = function(a)
		{
			var domain_name = document.location.toString().toLowerCase().split('/')[2];
			var exit_domain = a.href.split('/')[2].toLowerCase();
			if (domain_name.toLowerCase().indexOf(exit_domain) == -1)
			{
				if (typeof pageTracker != 'undefined')
					pageTracker._trackPageview('/exit/' + a.href);
			}
		};
		var anchors = document.getElementsByTagName('a');
		for (var i = 0; i < anchors.length; i++)
		{
			if (anchors[i].href && anchors[i].href != (document.location + '#'))
			{
				if (typeof anchors[i].onclick == 'undefined')
					anchors[i].onclick = function(e) {
						trackPage(this);
					};
				else if (typeof anchors[i].onclick == 'function')
				{
					var old_onclick = anchors[i].onclick;
					anchors[i].onclick = function(e) {
						old_onclick();
						trackPage(this);
					};
				}
			}
		}
	}
};

Hopefully I can find more ways of writing scripts that will help with Google Analytics.

Bookmark This!

http://www.dannytalk.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.dannytalk.com/wp-content/plugins/sociofluid/images/twitter_48.png

Related Posts

Tags: ,

9 Comments

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2006-2010 dannytalk ™ A Danny Ng Blog All rights reserved.
Desk Mess Mirrored v1.7 theme from BuyNowShop.com.