JavaScript link tracking with Google Analytics

JavaScript link tracking with Google Analytics

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on December 12, 2009

Google Analytics is great to tracking your site traffic. I started out using Statcounter.com to track my site visits, but over the years i’ve wanted more information so I switched over to using Google Analytics. Google provides an excellent dashboard to monitor and dig into your sites traffic and drill down the information to see exactly what content on your site is popular. For advertising campaigns at MySpace we use a JavaScript technique to track specific clicks on content on a page, with the ability to label specific content using the ‘name’ attribute. I’m providing the jQuery and Mootools code to implement this below. By using this technique when you view what content has been clicked in the analytics dashboard, and instead of seeing a bunch of long urls with various parameters in you can easily see a readable label as to what that link really is as shown below.

google-ana

jQuery Version

So below is a simple ‘click’ function that basically gets the value of the name attribute from any link that we set on our page and sends it to the analytics code at the bottom of our document.

Here’s an example of a link:

<a href=&quot;http://www.google.co.uk/search?hl=en&rlz=1G1GGLQ_ENGB314&q=papermashup.com&btnG=Search&meta=&aq=f&oq=name=&quot;papermashup search on Google;&gt;Search Google&lt;/>
$('document').ready(function () {
    $('a').click(function(){
        Pagetracker._trackPageview('/outgoing/' + $(this).attr('name'));
		});
	});

This is the analytics tracking code that will go just before the closing body tag on your page.

<script type="text/javascript">
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"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-6393355-53");
pageTracker._trackPageview('/outgoing');
} catch(err) {}</script> 

Mootools Version

This is exactly that same technique as above with the jQuery version but replicated with Mootools.


window.addEvent('load', function() {
		$$('a[name]').each(function(el) {
			el.addEvent('click',function() {
				var dd = '/outgoing/' + el.get('gaid');
				_gat._getTracker("UA-YOUR TRACKING ID")._trackPageview(dd);
			}.bind(this));
		});
});

So to sum up. To make your links easier to read in Google analytics add the name attribute to any link so when it’s clicked it will register with Google.

Comments
5 discussions around JavaScript link tracking with Google Analytics
  1. Alterity says:

    Google Analytics is good but uses up a lot of bandwidth and is only updated once a day and not real time.

    • Ashley says:

      @Alterity, actually Google Analytics is updated every hour or so, and a handy tip to see how much traffic you’ve had today is to click the date dropdown on the right of the main graph and select todays date, its always left out when you first visit analytics.

  2. Eddie says:

    Apolgies for being a tiny bit off topic, but you mention Statcounter in the post – Google Analytics is much more powerful than Statcounter but the one thing that I miss is the ability to view recent pageload activity. I can’t seem to an equivalent function in Analytics, do you know if it exists?

  3. Pingback: uberVU - social comments

  4. Seth says:

    I have found the best combination of web analytic sites are google analytics with clicktale. After researching and trying many of the free versions i couldnt find better than clicktale because of its features and the live videos of visitors browsing sessions – a real gem!

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>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.