Has anyone got this to work without adding the specific java script to each and every link? I’ve tried both of these solutions



Without success, maybe I’m doing something wrong.

Update 1

I have added the GA tracking code to the master page and statistics are being recorded, however the downloads are not.

  • Can anybody suggest 3rd party tools, if available, to track downloads, clicks, video watch, flash interactions, search tags etc., for selected web pages of a sharepoint 2007 implemented publishing Internet site, instead of inserting some script!
    – user11633
    Commented Oct 26, 2012 at 7:31
  • Help me out to solve the codes above. I tried copying website like SoundCloud but some of my programmes are not working. Commented May 11, 2019 at 4:11

4 Answers 4


You need to add the google analytics page tracking to the master page. I got it working read this article which helped me.


  • 2
    Master page is definitely the way to go. Commented May 5, 2011 at 12:01
  • forgot to mention that I do have the tracking code already added the master page and it is reporting statistics, just not the downloads
    – Rob
    Commented May 5, 2011 at 13:11
  • So have you added onclick code to the anchor link on the page? This doesn't happen automatically - you have to enter it manually or write javascript to parse the doc on page load and do it. Do you have a page to share?
    – Ryan
    Commented May 6, 2011 at 17:21
  • I see now thats what the taglinks.js that you linked to should be doing - break out IE dev toolbar and see if its working. Have a page to share and we may be able to help.
    – Ryan
    Commented May 6, 2011 at 17:23

Sorry forgot to say SetDomainName to none

So it should look something like this

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'Your google tag here']);
    _gaq.push(['_setDomainName', 'none']); 
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

If I understand how Google Analytics works, I don't believe you can use it track downloads. The script loads when a page is loaded to track the user. When you click to download a document, you aren't loading another page... you're just downloading the document.

As @Ryan points out, you could track document downloads on a page using GA's "virtual page views". You would probably need some additional JavaScript on the page that attaches the code to the onclick event of every link pointing to a document. A downside, is that this will not track document downloads that the user was directly linked to (ex: emailed doc link to a user).

If you need to track all downloads, you'll need to consume the IIS logs which tracks every request.

  • Yes you can - you can track additional actions (such as clicks on links that go to a download using) using 'virtual page views' e.g. _gaq.push(['_trackPageview','someurl') or you can track as events - see code.google.com/apis/analytics/docs/gaJS/…
    – Ryan
    Commented May 6, 2011 at 14:06
  • @Ryan: Interesting... so you'd need additional JS on your page that parses out any link to a document to add the tracking code. Also, what about if a user receives a direct link to a document?
    – Kit Menke
    Commented May 6, 2011 at 14:11
  • Yes - you either put that code in a onclick manually or write some javascript to parse the page onload and input. If direct link to document then nothing runs 'client side' so GA won't work - then you're looking at some server side log parsing solution.
    – Ryan
    Commented May 6, 2011 at 17:20
  • Thanks Ryan, that is good info. I've updated my answer to include it.
    – Kit Menke
    Commented May 6, 2011 at 18:02

using jQuery this this is the code I eventually figured out...

$(document).ready(function() {

    var ext = new Array(); 
    ext = ['pdf', 'zip', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'mp3', 'txt', 'vsd', 'rar', 'wma', 'avi', 'mmv']; 

    for (var i in ext)
        var fileType = 'a[href$="'+ext[i]+'"]';

        $(fileType).live('click', function() {

            var link= $(this).attr("href");        
            _gaq.push(['_trackPageview', link]);
            _gaq.push(['_trackEvent', 'Download', 'Click', $(this).text()]);

there is one draw back to this - any content loaded via ajax e.g. a grouped by column webpart will not fire the click event, see here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.