Using Google Analytics to track click throughs from your website

This post has been superceded by a more recent post on Google Analytics Event Tracking.

The Problem

It has always been difficult to track when someone clicks a link on your website which takes them to an external website (a click through or clickthru if you prefer). The problem is that the resulting request is made to the external website and is logged there, it doesn’t get logged on your own website at all.

For the web designer wanting to log clickthrus this is a problem, the traditional answer is to put the code which opens the new site in a file. When the user clicks on a link some code is executed locally (and logged locally) which then makes the call to the external site. This works fine but it makes it quite difficult for search engines to follow the link to the external site, sometimes impossible. If Google can’t follow the link to the external site then you may be giving them visitors but you are not helping their page rank in Google.

This is obviously a problem that many clever people have been thinking about for some time, one of the best solutions I have found is Search Engine Friendly Click Tracking which uses javascript to log any click on a link to a file or database.

The solution

But while checking some details in Google Analytics I found a section on using javascript events to track outbound pageviews. This is essentially the same technique as mentioned above but integrated with Google Analytics rather than standalone. It seems to me that this little feature using the new version of the Google Analytics tracking code is the holy grail of Search Engine Optimisation!

King Arthur sees a vision of Google Analytics

All you need to do is;

  1. make sure that you are using the new version of the Google Analytics Tracking code.
  2. That the code is inserted in your webpage above any links that you want to track (I put it just after the <body> tag in my test page).
  3. Add a javascript event tag to each link you want to track.

The actual tag is quite simple, this is Google’s example;

<a href=”” onClick=”javascript: pageTracker._trackPageview (‘/outgoing/’);“>

When somebody clicks on the link Google Analytics will log the click as a call to /outgoing/ in your analytics report.

And of course the href code is retained in its natural form so it is easy for search engines to follow the link, you can give the target site the link love it deserves.

I think this is a brilliant development, a big thank you to the wizards at Google for their continued good work!

Bonuses from Google Analytics

Using Goals to monetize outbound links

In Google Analytics you can assign goals to pages which make you money, say the checkout page on your shop. You can do the same thing with your new trackable outbound links, so if you carry advertising on your site you can see exactly how much you are earning. You could assign a value of say 10p to any pageview in the /outgoing/ directory and use Google Analytics to work out your bills.

Tracking calls to images and PDF files

Also, this technique can be applied to any sort of link. I discovered it while looking for a why of tracking views of a PDF file. If you have a file, like a PDF or an image, where you can’t insert the Google Analytics code inside the file then you can do something like;

<a href=”” onClick=”javascript: pageTracker._trackPageview (‘/my_document.pdf’);“>

And you can track views of the PDF or image file just like they were a regular web page.

Thank you Google

Thank you to Google for another great tool. Now that I know the answer I have no trouble finding the solution with a search in Google but I hope this article saves someone else some time.

Tags: , , ,

9 Responses to “Using Google Analytics to track click throughs from your website”

  1. Keith says:

    This is interesting, but google analytics _trackPageview does not have the ability to log clicks to a (mysql) database. Any workaround?

  2. cotsweb says:

    Hi Keith,
    I’m not sure if I quite understand your comment but I’ll have a stab at answering.

    If you are wanting to record clicks in your own MySQL database as well as using Google Analytics then I think you would need to replace the onclick call to pageTracker._trackPageview with a call to your own javascript routine which first wrote a record to your MySQL database and then called pageTracker._trackPageview
    But I think this might get a bit messy as Javascript runs on the client and MySQL runs on the server, getting it working smoothly might be difficult.

    Or you could go into Google Analytics and export the data from there. I think you can get Analytics to export data to a CSV file or something similar which you could then read with a simple program and store in your MySQL database. You can get Analytics to automatically email you a file each day/week/month or whatever suits your purpose.

    I haven’t tried either of these methods so don’t assume that I really know what I’m talking about but I hope this answer helps. Or have I totally misinterepreted your question?

  3. lee hughes says:


    am new to the whole web design thing. Am looking for a way to track rss subscription. Will this provide that?
    From my (limited) understanding I just apply what you said above to my rss feed link? (am creating my own).

    I put my google code right at the bottom of my page (as suggested) will putting another code in affect my results in any way? Or is it the same code? Does putting it where you suggest make any difference to the normal tracking?

    The code you provided

    I have to apply this to every link that I want to track?

    How do I view the results in Google Analytics?

    Thanks, sorry for all the questions I just wanted to get it straight in my head 🙂

  4. cotsweb says:

    Hi Lee,
    I had a quick look at your site and I think what you have done should work.

    I would edit the
    to just say

    And I would move the google analytics code to just under the opening body tag, for this purpose it needs to be above where you call it. Don’t duplicate the code, just move it, it should still work ok.

    That is one major expedition you have planned, good luck!

    A late edit to answer the rest of your questions;

    To view your stats go into Google Analytics and drill down from Content Overview (click on view report) you should find an entry for /feed.xml which will show you how many people clicked on your RSS button.

    You can add the javascript call to any link but don’t add it to anything where the destination already has the google analytics code in it or you will be double counting. It is best reserved for links where you can’t include google analytics code in the destination file, that is external links or links to things like xml and pdf files.

  5. undone says:

    Hi everyone,
    yes using onClick=”javascript: pageTracker._trackPageview works fine but it is still dependent on whether or not someone will click on the respective link. now my question is how could one track the views of a PDF which is indexed in the organic search, which makes the use of onClick=”javascript: pageTracker._trackPageview to the link useless, as one could read it and download it without clicking the link. any ideas??

  6. cotsweb says:

    An interesting question, and I’m afraid I don’t know the answer.

    Of course you should be able to see the file access in old fashioned web server logs but that isn’t as convenient or as reliable as proper analytics. If the PDF file has been cached somewhere then you won’t see any hit on your own server.

    What you really need to do is embed the javascript for Google Analytics within the PDF file, so when somebody opens the file it invokes GA to log it for you.
    I think this might be possible using something like javascript for acrobat but I don’t know.

    Also there may be security problems, it is one thing invoking javascript when browsing the web but quite another if a downloaded document does it to talk to a remote site every time you open a document.

    If somebody has a proper solution to this problem I’d love to see it.

  7. Ian says:

    Hi there, I’m wondering if it is then possible to track an image loading on an external site in Google Analytics?

    eg: my site is
    I create a blank gif image and upload to my site and say I name it:

    Then on an external site I am promoting, on the ‘thankyou’ page (conversion page) I have this:

    img src=”” alt=”” width=”1? height=”1?

    Would there be some way to check Google Analytics to see how many times it was loaded plus track which keywords lead to it been triggered?

    Or is there some other code I could add to the above image like my UA-XXXXXX-XX number to get this information?

    Thanks for your time, kind regards, Ian

  8. cotsweb says:

    Hi Ian,

    You can do what Google call Cross-Domain tracking if you have some control over what is put on the external site.

    Otherwise I think you would need to have the external site call a cgi script on your server which then returned the tracking image rather than call the tracking image directly, you could then put some tracking code in the script. This is the way that businesses like DoubleClick do it.

    I am no expert in this sort of thing so I could be completely wrong, I would be interested to hear how you solve the problem.


  9. […] 4 years ago I wrote a post on the subject of Using Google Analytics to track click throughs from your website which described how to use PageTracker to log clicks on links which wouldn’t normally be […]

Leave a Reply