Think2Loud

26 Aug, 2008

Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site.

Posted by: Josh In: JavaScript

So you have a nice site or blog with lots of links to other places. Wouldn’t it be nice to use the power built into Google Analytics to track those links, without having to add the necessary JavaScript to every link? Here is a very easy way: Add outbound link tracking to a new or existing site. Provided that your links to other sites have the REL attribute set, you can track them very easily with some help from jQuery.

1. Download jQuery and install Goolge Analytics.

Go to jQuery.com and get the latest version. Then add the script include into the header of your site.

<script type="text/javascript" src="jquery-1.2.6.js"></script>

Now go to Google Analytics. Set up an account and follow the directions to get tracking running. Go ahead, do it now, I’ll wait. You should now have some script on your site that looks like:

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' 
type='text/javascript'%3E%3C/script%3E")); 
 
var pageTracker = _gat._getTracker("YOUR ACCOUNT NUMBER HERE"); pageTracker._trackPageview();

2. Add the jQuery to start tracking links.

In the head section of your page, you will need to add a few lines of JavaScript. You will need to setup the jQuery .ready function and add the code to that.

$(document).ready(function(){
 
});

Next use a jQuery selector to select all a tags on the page with a rel=”external”. To do this, pass jQuery the a tag element with the attribute selector option.

$(a["rel*='external'"])

with this we add a click function to our jQuery object.

$(a["rel*='external'"]).click(function(){
 
});

With the last part we put in the function call to the Google Analytics code and we pass it the href that our a tag is pointing to.

pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));

This code makes the call to Google Analytics and passes a page path of “/outgoing/” and the url of where the click was headed. In your Google Analytics under content, you will see pages that start with “/outgoing/”. These are clicks that took people off your site. See below for how it looks in Google Analytics.

14 Responses to "Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site."

1 | Jared

August 27th, 2008 at 7:11 am

Avatar

Great tip, I’m going to implement this on my site asap.

2 | Tracking outgoing links with jQuery and Google Analytics, in webtoolkit4.me

August 28th, 2008 at 1:58 am

Avatar

[...] Such a simple think but Google Analytics doesn’t provide something like this. It’s very easy though. Have a look at this tutorial from think2loud.com about tracking outgoing links. [...]

3 | Websites You Shouldn´’t Have Missed in August

August 31st, 2008 at 2:09 pm

Avatar

[...] - Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site [...]

4 | S A N D E E P [ I N D I A N I C ] » Blog Archive » Websites You Shouldn’t Have Missed in August 2008

August 31st, 2008 at 8:56 pm

Avatar

[...] - Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site [...]

5 | D. Carreira

September 1st, 2008 at 1:32 am

Avatar

Nice!

Clean code and nice solution!

Thanks, David Carreira

6 | rss blog » Blog Archive » Websites You Shouldn’t Have Missed in August 2008

September 2nd, 2008 at 8:47 am

Avatar

[...] - Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site [...]

7 | Websites You Shouldn’t Have Missed in August 2008

September 4th, 2008 at 12:46 pm

Avatar

[...] - Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site [...]

8 | Websites You Shouldn’t Have Missed in August 2008 | POLPDESIGN

September 9th, 2008 at 7:27 pm

Avatar

[...] - Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site [...]

9 | Technology Story» Blog Archive » Website You Shouldn’t Have Missed

September 15th, 2008 at 11:11 pm

Avatar

[...] - Use jQuery with Google Analytics to Track Clicks on Outgoing Links From Your Site [...]

10 | Conference Coordinator

September 25th, 2008 at 8:08 am

Avatar

That sounds excellent!

Will go try it now.

How do we do similar thing with clicks on email links?

Thankyou

Sarah

11 | Outgoing Links Track Feature Enhancement on Google Analytics with jQuery « Analytics, Google, jQuery, Content, Overview, Note, URLs, Panel « Sharebrain

September 26th, 2008 at 5:05 am

Avatar

[...] This tutorial will show you how to enhance outgoing links track feature, simply by using jQuery on Google Analytics. The idea is to add outbound link tracking to a new or existing site. You’ll need to set your links to other sites with “REL” attribute, therefore jQuery can track them very easily.Somehow, by implementing this tip, you’ll get the all these benefits: * Unnecessary JavaScript to every link for tracking. * Simply work by inserting a click function to jQuery object to call Google Analytics * This code will pass a page path of “/outgoing/” and the url of where the click was headed. * Clearly display the outgoing URLs, total numbers of page views and its percentage on Content Overview Panel of Google Analytics.Note up this simple tips and you can start to implement it if you think this would be useful for your website analytics. 09.26.08 | Share it: [...]

12 | » links for 2008-09-01 | Paul Cowles

November 1st, 2008 at 2:07 pm

Avatar

[...] n Think2Loud » Blog Archive » Use jQuery with Google Analytics to Track Clicks on Outgoing Links Fro… [...]

13 | Matthew Pennell

November 22nd, 2008 at 2:02 am

Avatar

Doesn’t this technique screw up your pageviews figures, though? Any page ‘hit’ recorded with _trackPageview() will add 1 to your pageview figures, so someone arriving on a page and then clicking an external link will record 2 pageviews.

You could search for all outgoing links and deduct that from the total, but it’s still something that could trip some people up.

14 | Josh

November 24th, 2008 at 7:17 am

Avatar

@Matthew

Thanks for pointing that out. You are right it may add to the hit count I will test this on my site and see what it does. But it is the way Google recommends tracking outgoing links. You can see more info here.

http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006

Comment Form