Tracking documents and external links in google analytics

Update! We have a new script for tracking downloads. Check it out our new script and how to use it.

Following on from my previous post on tracking documents with google analytics, I’ve updated the javascript to now track external links as well as non-html documents

Installation instructions for the script are the same, all external links will appear as if they were in a directory called ‘exlinks’ on the top level, then they’re divided by their host name and the rest of their url.

So if you have a link to www.iqcontent.com and www.iqcontent.com/blog/ in your site, clicks on these links will be recorded in Google Analytics as:

/exlinks/www.iqcontent.com
/exlinks/www.iqcontent.com/blog

The updated script is available as a free download

It would also be a good idea to add a second profile with just external links in it, and exclude them from the main profile, as if you have a lot of external links this will skew your page view numbers.

21 Comments

  1. Thanks for the great script! It has worked perfectly for recording hits to the pdfs on my website.

    I was hoping you might tell me how I can exclude all external links from my main profile and customise a second profile so that it includes these external links.

  2. Thanks for the script, but does it work if I use onClick=”MM_openBrWindow(‘http://anothersite.comw/page.h.....height=620‘) … to open another browser window? I tried adding the JS file and the line right above urchin line but so far it hasn’t captured any links clicked.

    Thanks for the help,

  3. @ Peter,

    The script won’t track that link, it only tracks standard a href links, it checks a page for the href attribute.

    You’ll need to modify your script for opening a new browser window to include a href value.

  4. Hi Colm,

    I have being using your script for the last few months and it has been working fine – great job!

    But I have noticed that all of a sudden as off end of April for no reason it is no longer recording all downloads from our site. In particular one page has 20 pdf documents, and there is no information regarding these PDF downloads in GA now since end of April. GA script code has not changed, and if I do a filter for /exlinks in GA it still finds some results but very limited compared to the results before end of April.

    Any idea on what has happened?

  5. IT doesn’t seam to be tracking our links very well either. Only gets hits from firefox and safari. any ideas why?

  6. “It would also be a good idea to add a second profile with just external links in it, and exclude them from the main profile, as if you have a lot of external links this will skew your page view numbers.”

    …might be nice for your readers if you also added some tips on creating said filters…just a thought.

  7. Pingback: » Tracking document downloads in Google Analytics - iQ Blog

  8. Great script – thanks! However, my two most clicked external links are recorded as:

    * /exlinks/undefined/undefined
    * /exlinks/

    I can’t work out what these links might be that would cause them to be recorded like this? Any suggestions?

  9. @Josh – creating a filter for external links is quite easy:

    1. Set up a new profile. I use the following naming convention “Site Name: External Links and Downloads”
    2. Add a filter.
    3. Filter name: Include external links and downloads
    4. Filter type: Include only traffic to a subdirectory
    5. Subdirectory: ^/exlinks/

  10. I really like the script but I have just noticed something strange. I use a document write method of .js to run a small advertising manager program which rotates images which are links to vendors.
    I thought your script was picking up the exlinks but have noticed that a new addition to this avertising manager is not showing any clicks which I know is not true. Any ideas?
    Thanks

  11. It doesn’t work on IE6 for external links on image. If you have an img tag embedded with a href tag, “srcElement” will return undefined

  12. I observe the same as Christian (comment#8).
    When I look at the content drilldown of /exlinks/ I have 2,391 pageviews on / but when I click on that / I get no more information… that is,. pageviews is then marked as 0 as are the other stats there.

    Is the contents of /exlinks->”/” internal links of my website.. and if so, how can I learn where those internal links went?

  13. @Shawn – I asked the iQ Content guys about this issue. Apparently there is a bug in the script when tracking some items, most noticeably when there is an tag within the tag.

    I’ve asked someone on my team to take a look. Perhaps we can come up with a fix ourselves!

  14. @Christian,..
    Oh wow,.. now that would be super great. I’m not really sure (yet) what you mean by a tag within a tag, but if it gets us closer to a solution, then I’m all for it ! :-) Thanks!

  15. I can’t get this to work…
    If I view the source of any page from my site now, am I not supposed to see all file href’s with an added OnClick event trap?
    I also get no /exlink refs in my Analytics contents drilldown.

    There are now 3 open and close tag pairs right before my /body tag:
    - The taglink.js URL
    - The urchintracker URL
    - The UA number + UrchinTracker() call.

    Thanks to anyone who can offer any more insight.

    Best;
    Eivind

  16. @Eivind We moderate the first comment someone makes to the site, after than you can comment all you want ;)

  17. Something weird going on with comments on this post.

  18. Does this script still work with the new Google Analytics tracker code?

    I was using it last year with the old code and it worked, but it stopped working when I changed to the new tracker.

    Has this script been updated to use the new code?

  19. We are using it with the latest version of the Google Analytics code and it works fine.

  20. Just testing this and it seems to prefix local links with the full domain – i.e. it translates relative URLs into absolute ones. I believe this will look REALLY ugly when this comes to displaying in google analytics, unless GA already caters for this case. Anyone know?

  21. OK – I’ve got it. Referring to:

    http://www.iqcontent.com/js/gataglinks.js

    The script fails in IE, presumably because “this.hostname” is garbage in the context of IE’s dodgy event model. The hostname must be determined by looking at the array_element parameter in IE. So, the line:

    if (location.host != this.hostname) {

    needs to be replaced with something like:

    var hostname = array_element.srcElement ? array_element.srcElement.hostname : this.hostname;
    if (location.host != hostname) {

    and other references to the hostname can then by replaced with the hostname variable.