Tracking Clicks with GA Pt. 3: Advanced Implementation

April 9, 2007 by Justin Cutroni

Welcome to Part 3 of Tracking Clicks with Google Analytics! Last time I discussed the process and theory behind tracking visitor click data in GA. This technique is primarily used to track non-HTML files and outbound links. In this post we’ll discuss a quicker, easier way to implement the click tracking. Please make sure you’ve read Part 2 before reading this post.

Overview

Whenever we need to track clicks we use the following JavaScript code. This code automatically adds the urchinTracker function to appropriate part of your HTML at the appropriate time. With this script there is no need to change the HTML in your pages. You simple add this script to all the pages on your site and it does all the work for you.

You’re probably wondering how this all woks. I’m not going to get into the nuts and bolts, but this uses the DOM or Document Object Model. Using the DOM in the code gives us the ability to modify the HTML in real time.

About the Script

This script will only track clicks on anchor tags. It will not track clicks on images or other HTML elements.

If the anchor tag points to a different domain then urchinTracker() is executed before the visitor leaves the site and a pageview named ‘/outbound/<url>’ is created in Google Analytics. The <url> part is replaced with the value from the HREF attribute in the anchor tag.

If the anchor tag points to a non-HTML file then the scrip creates a pageview named ‘<path>’ where <path> is the value in the HREF attribute of the link.

Here’s an example of the script in action. The link below will take you to a test page where you can click on links to other domains and files Rather than send the data to GA, the script has been modified to display a message (note: a new window or tab will open in you browser).

GA Click Tracker Test Page

To view the code just look at the source of the above page. Right click on the page and choose ‘View Source’.

Installing the Script

Installing this script is simple. Copy and paste the code from the test page and place it in your web page. I place it before the closing BODY tag. After the script has been installed you can configure it to work with your site. Also, make sure that the Google Analytics tracking code is installed on the page.

Customizing the Script

There are a few customizations you can make to the code.

First, there is a variable named debug. When it is set to 1 the script will display a pop up window when a link is clicked. Set debug to 0 to disable the pop up. By default, debug is set to 1 (on).

Next, you can define the types of non-HTML files that this script will track. Find the variable named fileTypes. The value should look like this:

(".doc",".xls",".exe",".zip",".pdf")

To track additional file types just add the extension to the list. Make sure you match the text-formatting of the existing list.

Finally, you can change the way external links appear in the GA reports. By default, all external links will be preceded by ‘/outbound/’. If you would like them to appear differently change the extIdentifier variable.

Summary

There are lots of scripts like this. This is just our version. I hope it gives you some idea of what is possible and inspires you to create one of your own or modify ours. Just remember that this script will create additional data in your GA profiles. You many need to add a filter to deal with the extra data.

Please feel free to use and modify this script. The only thing we ask is you share the changes with others.


Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Technorati
  • del.icio.us
  • Slashdot
  • StumbleUpon
  • Ma.gnolia
  • YahooMyWeb
  • co.mments
  • Reddit
Subscribe:
  1. 16 Responses to “Tracking Clicks with GA Pt. 3: Advanced Implementation”

  2. I’ve been looking for a free script like this for a long time!
    I’ve read your blog for at least a few months and it is definitely one of the more useful blogs.
    Thanks so much!

    By Rehan Asif on Apr 10, 2007

  3. Hi, your script is very helpful for me to track all the links. However, I have one question.

    For mailto, it doesn’t seem to track them properly. I turned on the debug mode but the alert just shows the page which is calling the mailto link. Is it possible to show which particular mailto that people are clicking on?

    Thanks

    By Andrew on Aug 31, 2007

  4. Hi Andrew,

    Thanks for the feedback. The script is a bit old, and does not handle mailto: links. It probably should, I just haven’t updated it. We have a more advanced script that we use internally, but I can’t give that one away. :)

    Feel free to tinker with the code!

    Thanks for reading.

    Justin

    By Justin on Aug 31, 2007

  5. Hi Justin,

    Will this script work for site overlay functionality on GA?

    Also with a slightly related question. We have many pages which have the same exact links on a single page, but site overlay rolls them up as 1 number. Is it possible to track the separately? I’ve tried just adding a query string to the end of the url, but that prevents it from tracking completely. I did this in conjunction with a modification to the profile’s info on the “Exclude URL Query Parameters”.

    I’m just using a test query sting link like so:
    http://usnews.rankingsandreviews.com/cars-trucks/2007-Dodge-Magnum/?A
    http://usnews.rankingsandreviews.com/cars-trucks/2007-Dodge-Magnum/?B

    Then i put in the field “A”.

    Any suggestions?

    Thanks Much,
    Bryan

    By Bryan on Nov 8, 2007

  6. Hi Bryan,

    No, the script will not work with site overlay. The reason is that the URLs create by urchinTracker are virtual, they do not exist on your site.

    As to your second question, using an additional query string parameter to differentiate each URL on a page is correct. However, if you exclude that parameter using the profile settings then it will be completely stripeed from GA.

    When GA goes to build the site overlay report it takes the values in the request uri field, adds then to the website URL as defined in the profile settings, and then requests that page from your site. If the URL does not match an URL for your site, then the report will break.

    Thanks for the comment and I hope that helps,

    Justin

    By Justin on Nov 17, 2007

  7. Hi Justin,
    Have you tested this with Firefox? As installed on my site the tracking seems to work only for IE and Safari, not Firefox or Netscape. The only change I’ve made is to put the script in an external file rather than in the body of the page.
    Thank you
    John

    By John Crossley on Nov 26, 2007

  8. Hey John,

    It should work for FireFox, I have not experienced any issues with FF. Maybe there is a problem with the new version of FF.

    Justin

    By Justin Cutroni on Dec 2, 2007

  9. This looks like a nice script, though I’m using Google Analytics’s new ga.js script instead of urchin. I know that at some point in the future we’re supposed to get outbound link tracking with ga.js, but since this hasn’t happened yet, I was hoping to use your script as a simple solution in the interim.

    The current method for tracking ga.js outbound links is pretty much the same as tracking outbound links with urchin. The both use the the onlick property, but where urchin’s call is: javascript:urchinTracker(’/clicks/images/foo.jpg’);
    ga.js’s call is:
    pageTracker._trackPageview(’/clicks/images/foo.jpg’);

    So, I if I were to modify your script to work with ga.js, am I correct to assume that I would just need to change all the instances of urchinTracker() with pageTracker._trackPageview()?

    From what I can tell, this should work, but I just wanted to get some input from you about this before I break something.

    Thank you!

    By Andy on Jan 24, 2008

  10. I maybe be missing something but where is a Javascript that we need to add to our site to get this to work?

    By Komal Agrawal on Feb 21, 2008

  11. Hi Andy,

    You’re correct. If you’re using the new tracking code then you need to replace urchinTracker with trackPageview. I’ll be working on a new version of the script soon and it will support the new tracking code.

    Thanks for reading,

    Justin

    By Justin Cutroni on Jun 24, 2008

  12. Hey Justin
    GREAT BLOG, congratulations
    I would like track the outbound links and I’m trying alternatives. Now, I have placed as test the next code ( in the link page )

    LINK

    and I don’t find the tag OUTBOUNDREGISTER/VERSION in the USERS / DEFINED USER in G.A. and neither I find it on CONTENT.

    What you think I ‘m doing as error ?

    I’m using this GA code:

    _uacct = “UA-xxxxxx-1″;
    urchinTracker();

    PLEASE, you are an expert about it, can U help me?
    have a nice day, David

    By David on Jun 27, 2008

  13. David,

    You need to do one of two things to track clicks on links. Your first option is to a manually add the urchinTracker() function to the link, as I explained in part 2 of this series. Or, your second option, is to include this script on your page, as I describe in this post.

    Based on your code above I can’t really tell which method you want to use. But I did notice that the link you create contains no code, so I’m assuming you want to use the method described in this post. In this case make sure you you’ve included the DOM script.

    Hope that helps,

    Justin

    Hope that help.

    By Justin Cutroni on Jun 28, 2008

  14. THANKS JUSTIN !!!

    in the second code option, ¿ every link placed on that page will be tracked ?, if I have there an adsense, for example, it will be include in a PDF tracking ? are there any code in the link code to specyfic each link ?.

    By David on Jul 2, 2008

  15. David,

    The code will only track links to certain types of files. The default list is:

    .doc
    .xls
    .exe
    .zip
    .pdf

    You can alter the code to include more, or less, file types. It will not track AdSense links.

    Thanks for the question,

    Justin

    By Justin Cutroni on Jul 2, 2008

  1. 2 Trackback(s)

  2. Apr 10, 2007: Analytics Talk » Tracking Clicks with Google Analytics Pt. 0
  3. Jul 5, 2007: Kaikki irti Google Analytics -kävijäseurannasta | Nettibisnes.Info

Post a Comment