Updated Script for Tracking Outbound links In Google Analytics with jQuery | Blog | ISO100

Updated Script for Tracking Outbound links In Google Analytics with jQuery

Monday, December 08, 2008

Chris Coyier recently with a snippet of some code I use on a for work. The code snippet uses Google Analytics and jQuery to automatically track outbound links. At the time, that script looked at the existence of “http://” in the link URL to decide whether or not to attach the behavior. The new, updated script enhances behavior by comparing the link’s domain to the current page’s domain and if they are different, triggers the behavior. This is helpful for CMS or other tools that may generate full URLs including the http:// instead of just relative ones.

The New Code:


$('#content a:not(.popupwindow)').filter(function() {
	var theHref = this;
	if (theHref.hostname && theHref.hostname !== location.hostname) {
		$(theHref).not(".noAutoIcon").addClass("offSite");
		$(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
			var code=event.charCode || event.keyCode;
			if (!code || (code && code == 13)) {
				if(pageTracker){
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/outgoing/' + fixedLink;
					pageTracker._trackPageview(fixedLink);
				};
			};
		});
	};
});

What’s Different

I’m not going to re-post the detailed description of what the script does. You can go view for that. I’ll just go over what’s different now:

  1. First, you’ll notice I use a different “not” clause in the selector. This is because on the updated site, I have separate “turn off” classes for different functionality. In this scenario, we only want to turn this tracking off if we don’t want automatic popups. The automatic off-site link icon creation is separate and I wanted that managed separately.
  2. Next, the code saves the href to a temporary variable and compares the current hostname to the link’s hostname.
  3. You can see now where I look for “noAutoIcon” to conditionally exclude the application of an “offSite” class, which displays a special icon next to that link.
  4. Finally, the exclusion class for “noAutoLink” is checked and the click/return event bindings are made. This is the same as the old script.

Enjoy! Let me know if you have any questions.

Category:
Web Design
Tagged With:
webdev webdesign javascript google analytics links popup analytics jquery
Posted By:
Ian Pitts
Posted:
12/08 at 12:19 PM
Views:
15939
Short URL:
/s/b/81

Related Blog Posts:

Comments:

Very nice, thanks!

No, to add a check to see if the link is a mailto or document link in order to add the tracking for these pages as well.

By on Monday, December 15, 2008 [Comment Permalink]

I think one more benefit of this new code is that you can track “https” kinda pages pretty easily

Thanks for sharing

By on Thursday, January 08, 2009 [Comment Permalink]

I ran across the article and it’s a very good idea. good job.

Just wanted to pass along that I did a similar thing on my site, but instead of using the “page views” mechanism in GA, I’m using the new “Event Tracking” feature. Instead of having only one “url” parameter you have to hash meta data into (like “/outbound/” above), you get three string parameters to create a tiered heirarchy of “events”, which in this case are clicks on outbound links.

For instance, I have a ‘category’ of “External Link Clicks”, and the ‘action’ parameter I set to the actual URL of the clicked link, and the text of the anchor, I put in the ‘label’ parameter.

The biggest benefit though is that you can track these clicks without skewing your “page-views” statistics.

By on Tuesday, January 13, 2009 [Comment Permalink]

Hello,

I tried to put this piece of code in my pages , but nothing occured.
I use Jquery, plop this code in the header of my pages, right after the Google Analytics code, but I’m not able to track outgoing links.

Do you have some advices on where to paste the code?

Thks

By on Wednesday, February 04, 2009 [Comment Permalink]

Lionel, are you sure you configured the jQuery selector to match your site’s XHTML?

If your target a elements aren’t inside an element with an ID of “content” the function will never get triggered.

By on Saturday, February 28, 2009 [Comment Permalink]

I think that
  if ( pageTracker )
should be
  if ( self.pageTracker) 
otherwise it will throw an exception if pageTracker is not set.

By on Monday, December 14, 2009 [Comment Permalink]

The biggest benefit though is that you can track these clicks without skewing your “page-views” statistics

By on Monday, January 11, 2010 [Comment Permalink]

No, to add a check to see if the link is a mailto or document link in order to add the tracking for these pages as well.

By on Monday, January 11, 2010 [Comment Permalink]

Want to leave your mark? Post a comment:

Commenting is not available in this section entry.

copyright@2023 iso-100.All rights reserved