How to Track Downloads & Outbound Links in Google Analytics

How to Track Downloads & Outbound Links in Google Analytics

Posted by on Fri, Mar 22, 2013
Filed Under | Google Analytics


It has been almost 2 years since we published our wildly popular post, How to Track Downloads in Google Analytics.  Since Google Analytics does not track file downloads, email, telephone or other outbound link clicks automatically, we saw an opportunity to File Downloadsprovide this code to the community.  In our original post, we provided some explanations and code examples for tracking file downloads in Google Analytics using event tracking.

We decided to revisit our original post and provide a valuable code update that improves performance, makes tracking downloads more manageable and easier to extend.

Dynamically Track Downloads & Other External Links

For those who have a lot of links and would like to dynamically detect clicks on links to file downloads, we have provided updated code below.  As before, this code requires the jQuery JavaScript library to be set before the code.

We have updated the code to make it more manageable and easier to extend.  In addition, we are using the jQuery on() method for attaching the click event handler to links.  Since we are using the on() method you will need to use jQuery v1.7+.  If you are using an earlier version of jQuery the .live() method can be used instead.

The primary benefit of using the on() method is performance.  Instead of looping through all ‘a’ elements on a page after the page loads (takes processing power on pages with lots of links), we instead listen for any clicks on the ‘a’ elements and invoke our custom JavaScript on the fly.

Again, feel free to customize this code to suite your needs.  It can be placed in its own .js file and should be placed in the <head> of your pages.  This script automates the following:

  • Tracks file downloads as events for the following extensions: .zip, .exe, dmg, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .txt, rar, wma, mov, avi, wmv, flv, wav (again feel free to modify the list)
  • Tracks outbound clicks as events if the href value contains http:// or https:// and the domain value doesn’t match the current domain
  • Tracks mailto email clicks
  • Tracks Tel telephone clicks
<script type="text/javascript">
if (typeof jQuery != 'undefined') {
  jQuery(document).ready(function($) {
    var filetypes = /\.(zip|exe|dmg|pdf|doc.*|xls.*|ppt.*|mp3|txt|rar|wma|mov|avi|wmv|flv|wav)$/i;
    var baseHref = '';
    if (jQuery('base').attr('href') != undefined) baseHref = jQuery('base').attr('href');

    jQuery('a').on('click', function(event) {
      var el = jQuery(this);
      var track = true;
      var href = (typeof(el.attr('href')) != 'undefined' ) ? el.attr('href') :"";
      var isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]);
      if (!href.match(/^javascript:/i)) {
    	var elEv = []; elEv.value=0, elEv.non_i=false;
        if (href.match(/^mailto\:/i)) {
          elEv.category = "email";
          elEv.action = "click";
          elEv.label = href.replace(/^mailto\:/i, '');
          elEv.loc = href;
        }
        else if (href.match(filetypes)) {
          var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
          elEv.category = "download";
          elEv.action = "click-" + extension[0];
          elEv.label = href.replace(/ /g,"-");
          elEv.loc = baseHref + href;
        }
        else if (href.match(/^https?\:/i) && !isThisDomain) {
          elEv.category = "external";
          elEv.action = "click";
          elEv.label = href.replace(/^https?\:\/\//i, '');
          elEv.non_i = true;
          elEv.loc = href;
        }
        else if (href.match(/^tel\:/i)) {
          elEv.category = "telephone";
          elEv.action = "click";
          elEv.label = href.replace(/^tel\:/i, '');
          elEv.loc = href;
        }
        else track = false;

       	if (track) {
          _gaq.push(['_trackEvent', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, elEv.non_i]);
          if ( el.attr('target') == undefined || el.attr('target').toLowerCase() != '_blank') {
            setTimeout(function() { location.href = elEv.loc; }, 400);
            return false;
	  }
	}
      }
    });
  });
}
</script>

The script sets download, email and tel link clicks as interaction events while the external site clicks are non-interaction which can be adjusted if desired.

As before, the script will detect if the link is opening in a new window or not and automatically uses setTimeout() for 400ms if you are not.  This is to allow time for the hit request to process before taking the user to the new page.

Inline Download & Other External Links Tracking

If you’d rather not use the above method to place a single javascript file on your site and automate the tracking for all link elements, you can use the manual inline approach by tagging each link element individually.  The inline approach can be time consuming and is not scalable.

The original syntax for adding the code inline remains the same.

New window/tab
For links that open a new window or tab (such as using target=”_blank” for example) you will want to use the code below:

<a onclick="_gaq.push(['_trackEvent','Download','PDF',this.href]);" href="pdfs/my-file.pdf" target="_blank">Download my file</a>

In current window
For links that open within the same window, replacing the current page, you will want to use the code below:

<a onclick="var that=this;_gaq.push(['_trackEvent','Download','PDF',this.href]);setTimeout(function(){location.href=that.href;},400);return false;" href="pdfs/my-file.pdf">Download my file</a>

These type of links require a slight delay to allow time for the hit request to process before taking the user to the new page.

Detailed Download & External Link Reports

After all of your hard work you will have event tracking reports with neatly organized data around file downloads, external link, email link, and telephone link clicks.  From these reports you can gauge the usage and usefulness of your file downloads and various links.

By diving into the download category and selecting event label as the primary dimension you are able to see a report on all of the individual files downloaded from your site.

Lastly, since events are associated with the page they were fired on, you are able to apply a secondary dimension of page to find out which page contained the download or external link click that occurred.

If you need assistance with outbound link analysis, setup of this tracking code or any other analytics help, don’t hesitate to comment below or contact our Google Analytics Consulting Experts.

Enjoy this post?

Join the discussion below, subscribe to our RSS feed or share it on the web.

This post was written by:

has written 4 posts on the Web Analytics Blog.

Ryan is a Google Analytics Consultant at Blast Analytics & Marketing. With a variety of skills under his belt he spends his days assisting clients with all of their Google Analytics needs. Fueled by music, he relishes a good challenge and is driven to learn.

Add Ryan to your circles on Google+


Tags: , ,
  • http://www.huntinggearguy.com/ Adriel

    Cool! So this would be faster than a plugin like Yoast or Google Analyticator?

  • http://www.blastam.com Joe Christopher

    @adrielmichaud:disqus We prefer the method used in this blog post because it doesn’t loop through all of the ‘a’ link elements on the page to add the onclick attribute. Instead, we listen for any click and at the time of the click perform the event tracking. If you have hundreds of links on your page, this will make a big difference in performance.

    It appears that the Google Analyticator plugin uses the looping method. See http://plugins.svn.wordpress.org/google-analyticator/trunk/external-tracking.js

    For Yoast, I believe this is all handled server side as the WordPress page renders. This is a great method.

    Additionally, keep in mind that the above mentioned WP plugins do a great job of doing other things that we didn’t include above. For most users, the plugins mentioned (especially Yoast’s) may work quite well and be very easy to install. For non-WP sites or advanced users who want to customize things further, use ours as a base :)

  • http://twitter.com/yudel Larry Yudelson

    Would this work for a link contained inside an iframe that loads from an external site? Specifically, I use an iframe to load an Amazon widget, and would like to be able to track clicks to it.

  • http://www.blastam.com Joe Christopher

    @twitter-14594815:disqus

    No it would not work for that scenario. To the best of my knowledge, the browser views the contents of an iframe as a separate content instance and the parent window is not going to be able to listen for those clicks.

    This StackOverflow post will shed some light on an alternative technique: http://stackoverflow.com/questions/2381336/detect-click-into-iframe-using-javascript

    Of course, if you controlled the contents of the iframe, you could call a function in the parent window. That won’t be the case with your Amazon widget. Some things that render in an iframe, like the YouTube player will have an API that allows you to listen for certain events.

  • http://twitter.com/globalinfoNZ GlobalInfo

    Excellent post! Thanks
    I was wondering if this would work for password protected pdf files. The way my website is setup is that if a visitor clicks the pdf link, it takes her to the login screen, and only those who are authenticated are able to view/download the pdf. But for the successive files, it won’t ask the password (as the session is active).
    Thank you

  • http://www.blastam.com Joe Christopher

    @twitter-18159257:disqus Great question! One option to handle this would be to put a data attribute or a rel attribute on the link when you know the visitor is not authenticated. Then, you could modify our script to ignore clicks when that attribute exists on the link element.

    There are other ways of course, but those techniques depend on more of the technical details with how this works on your site.

    Hope that helps!
    Joe

  • http://twitter.com/globalinfoNZ GlobalInfo

    Thanks Joe. This sure gives me a hint.

  • http://www.overthere.co.uk/ Gary Hughes

    Thanks a lot for this. I’m moving from WordPress to a static blog and was looking for a way to track download counts. This is perfect!

    Just one question – in line 30 one of the conditions is ‘!isThisDomain’. This gives me ‘Uncaught ReferenceError: isThisDomain is not defined’ in Chrome’s console. What would be the best way to set the value of isThisDomain?

  • http://www.overthere.co.uk/ Gary Hughes

    Got it – I found that adding

    var isThisDomain = (event.currentTarget.host == window.location.host);

    after line 7 sorted it.

    Is there a better way?

  • Naters

    Thanks! but where do I find the analytic information? I’ve looked all through my Google analytics and I don’t see where it would be. I have a Joomla! site and I’m using the inline “onclick”.

  • http://www.blastam.com Joe Christopher

    @disqus_6URiQgcbiq:disqus This data would be under Events. Content > Events > Top Events

  • Naters

    Ah! Thank you!

  • http://www.blastam.com Joe Christopher

    @se0siris:disqus Thanks so much for pointing this out. We’ve updated the post to correct this code issue. Please use the latest version.

  • bettye

    Can anyone refer me to a site that gives simple, easy-to-follow
    instructions on how to track downloads on my website? Everything I’ve
    seen is either jibberish, requires a degree from MIT, or is user
    unfriendly. I am offering free downloads on my site of files in PDF,
    MOBI, and EPUB formats, and I can’t find ANY information out that that I
    can understand.

  • http://www.blastam.com Joe Christopher

    @google-e15425ff3e361ab6523174eacce9b9a3:disqus If you are using WordPress as your platform, you may want to check out Yoast’s plugin: http://yoast.com/wordpress/google-analytics/. This just requires a simple install and then you can configure the options via a user-interface.

    The solution we’ve provided here is certainly tailored towards a technical audience; though we’ve greatly simplified things so that users with minimal technical experience can use what we’ve provided. Typically, you can forward this blog post to the person/company that built your website and they can get it added.

    Another option is to work with Blast directly and we can provide more hands-on support to get this implemented correctly.

    Hope that helps!
    Joe

  • http://twitter.com/FredFaulknerIV Fred Faulkner IV

    I’ve tried to implement this with no luck. I have JQuery first, your script next, then the GA all in the . Manual tagging works fine, but the script doesn’t seem to be “listening” for the clicks. Pageview data passes, but not click data according to GA Debugger. I know this isn’t support per se, but your implementation is pretty straight forward I thought.

  • http://www.facebook.com/ernie.natera Ernie Natera

    Can anyone help? I entered the script but after 4 days I still get no results.
    http://svswa.org/board_of_directors.cfm

  • Ryan Chase

    @twitter-9402882:disqus If you provide me with a link I can take a quick look at your site to see if anything stands out. It sounds like something is interfering with the click event handler.

    Ryan

  • Ryan Chase

    Ernie Natera I would recommend changing the following:
    _gaq.push(['_trackEvent', 'Download', 'Pdf', '20130301',, false]);

    to:
    _gaq.push(['_trackEvent', 'Download', 'Pdf', '20130301']);

    Since you didn’t specify an event value before the non-interaction flag it is breaking. Also, it is not necessary to specify false for the non-interaction flag since it is set to false by default. You should also add taget=”_blank” for this link to follow the format of the others.

    For the other links you will want to remove � from before and after the _gaq.push call to get them to work properly.

    Ryan

  • http://twitter.com/smichaelgriffin Michael Griffin

    *Great* info; thanks so much for sharing!

  • Emily Graham

    Thanks for the useful blogs, really good stuff. One question though – should you be able to see the results instantly, i.e. you add the code (inline download) and download the file – would this appear instantly in Google Analytics Events?

  • http://www.fromthe21stfloor.com Fred Faulkner

    Hy Ryan – the site is http://www.chestnet.org. Thanks for looking into this.

  • http://twitter.com/sonofray Michael

    I have the same question. I’m looking in the Real-Time>Events beta as well as Content>Events and there’s nothing yet. My site is http://www.firsttrustretirement.com/investment_products.html

  • http://thomasholmes.me/goto/journal/ Thomas Holmes

    Hello Ryan,

    Thanks for the good and clear post. I’m only tracking 2 or 3 PDF downloads so I opted for tagging each link individually. I copy and pasted the code but there’s nothing coming up in Analytics.

    Any idea why this would not be working? My site is http://www.shopifyandyou.com/

    Thanks in advance!

    Thomas

  • Ryan Chase

    @emily_graham:disqus @twitter-15922572:disqus

    If the code is implemented properly you should see the events appear in the Real-Time > Events report right away. The events should show up in the normal Content > Events report within 24 hours, provided the code has been implemented properly.

    Michael, I checked your site and the code appears to be implemented properly. I was able to see that the events were being fired when clicking on the file download links.

    Ryan

  • Ryan Chase

    @fromthe21stfloor:disqus

    I took a look and It appears that some javascript errors further up on the page are causing the script not to work. If you check your javascript console you should see the specific errors that need to be fixed. Once you get those resolved the click tracking should start working.

    Ryan

  • disqus_ORYseND2Xx

    Will the inline script work if the PDF is being called with a ‘window.open’ command rather than a simple href=? Can I simply add the inline code after the window.open, separated with a semicolon?

  • Andrew Hawken

    Hi

    Thats really useful. However, if you have content generated dynamically, the ‘on’ hook below, will add tracking to content created after this code has run.

    jQuery(document).on(‘click’,’a’ ,function (event) {

  • Ivan

    Thanks for this code. I can’t get it to work though. I implemented the previous code before I saw that it was updated. I have the code in its own .js file. http://www.nlc.org

  • http://www.facebook.com/jameserenshaw James Renshaw

    Hi,

    Great code, thanks for the post, but I’ve noticed an error. For domains that have more than one suffix (e.g. .co.uk, or .gov.uk), the isThisDomain variable only picks up the suffix, and not the domain name. That means any external links with the same double suffix will be missed (any from UK to UK basically). I’ve modified it to look for if it matches my domain, and it now works fine. I know it’s a lot of work to correct the code for this, but it might be worth adding a note for that.

    Thanks,

  • Ryan Chase

    @Louis I took a look at your site and the event is being fired. There might be a filter applied to your profile that is preventing the data from coming through. Are you still not seeing any data for the pdf clicks?

  • Ryan Chase

    @disqus_ORYseND2Xx:disqus The inline code will work by adding it after the window.open command.

  • Ryan Chase

    @Ivan It looks like you will need to move your tracking.js file below the jQuery reference in order to get it to work.

  • Ryan Chase

    @Andrew Hawken Thanks for the tip! You are correct in that you will have to adjust the click event to the format you provided in order to detect clicks on Ajax loaded content.

  • http://www.facebook.com/dopama Martin Almazan

    I’be been looking to replace an old tracking perl script for years,

    Unfortunately, it fails for internal links that have the full absolute URL. It seems to add a slash before “http”. I use subdomains and sometimes I need to have links that way. Would there be an easy fix for this?

  • http://twitter.com/grpatten Grant Patten

    Where exactly am I supposed to go in the Google Analytics to find the tracked PDFs? Please give me the exact menu items I have to click into. “By diving into the download category and selecting event label as the primary dimension” is too vague for me…… thanks.

  • Ryan Chase

    Hi Grant,

    Within your GA account go to Content > Events > Top Events. That report will show the Event data by categories. The pdf downloads would be under the ‘download’ category. If you drill down into the ‘download’ category you should see the action of ‘click-pdf’. If you drill down into ‘click-pdf’ you should get a list of your pdf downloads and the related data.

    Hope that helps.
    Ryan

  • Francisco

    Hi Ryan,

    I found a problem in the script for my use. If somebody right-clicks and save as the pdf links on my website, they are not counted with your script.

    I fixed this by changing the ‘click’ event by ‘mousedown’. That way, I capture both left-clicks and right-clicks.

    The only inconvenient is that right-clicks do not always mean downloads.

    Is there any way to specifically capture right-click, save as?

    Thank you!!
    Francisco

  • http://www.facebook.com/dopama Martin Almazan

    I’d be inmensely grateful if you could find a fix for this

  • http://twitter.com/grpatten Grant Patten

    Ryan, thanks but it’s not working. I keep trying to post the code that I’m using for it… but the moderator won’t approve my message. Could I email you the code instead?

  • Jean Paul

    Thanks Ryan. Do you know if this works for tracking clicks on outbound links with the new Universal Analytics script? If so, how does one do this?

  • http://twitter.com/sonofray Michael

    Thanks! I found a bad reference to my script in a nested HTML. BAM! Beautiful!

  • Cade

    Thanks, however this does not appear to work with Google Tag Manager?

  • Harmeet Gabha

    I have this sort of tracking working on my site but I want to display the number of events count on the page. For example I have a download button on a page which is tracked in Analytics but on my same blog page, i want to display a counter to visitors that this item has been downloaded X number of time. Is there such options using Analytics to retrieve the value, in addition to push the event counter?

    Any help would be appreciated.

  • http://paolomargari.it/ Paolo Margari

    confirm, it doesn’t work, either placing in the head or as a custom html tag. i want to try changing it using datalayer instead of the ‘old’ ga method… but yes, the concept behind such solution is great and it’s really useful as for gtm there’s nothing made (yet)

  • http://paolomargari.it/ Paolo Margari

    update: it works also on GTM, with a little adaption…

  • Mia Gelles

    Thank you so much for this post. I’ve been looking for a solution for months and this works perfectly!

  • Cade

    Could you please share your adaption to get it to work?

  • Gael

    Hi Joe,
    Thanks a lot for this great post. I have one question to be sure it could help me.
    I would like to be able to know each month :
    – Number of downloaded of “myfile.exe”
    AND
    – Number of downloaded of “myfile.exe” from websiteA
    – Number of downloaded of “myfile.exe” from websiteB
    – Number of downloaded of “myfile.exe” from websiteC
    Etc.

    I dont have access to websiteA, websiteB, websiteC etc. They all pointed directly to mysite/myfile.exe. So, in that case, would it be possible to get those informations using your code ? (or would I ‘just’ have the total number of downloaded ? )
    Thank you

  • Mike Betts

    Yes, please share this adaptation with us.

  • Paul Lear

    Hi Martin,

    Feel free to paste a snippet of what your HTML code looks like with the extra slash rendered. It might be as simple as adjusting the RegEx you need to use in the tracking code.

    Paul

  • Paul Lear

    Great question, and possibly a difficult road to travel to get to what you want. Google does make available a data export API to extract data from your analytics account. This would require some pretty heavy lifting to make it happen if you were starting from scratch. I would first look into available plugins for your CMS system to see if any cool download counting widgets already exist before investing too much time.

  • Paul Lear

    Great suggestion!

  • Paul Lear

    Hi David,

    Currently your site uses a local reference to jQuery 1.6. You should replace the library with a 1.7 or greater version, however it also sounds like there may be additional issues if the inline version didn’t work either. It may be related to another javascript conflict if it continues to not track properly for you.

  • Paul Lear

    I took a quick look at your site and it may be that you’ve moved on from this approach as I didn’t see the tracking code in place, but when you are ready to try again, be sure to include the jQuery library reference in your section before the call to the tracking code.

    e.g.

  • Paul Lear

    Hi Gael,

    Unfortunately the code we developed and other similar solutions only track upon the “click” event, and if that event happens on a website you don’t control, we lose the ability to track it.

    As it sounds today, you won’t even get a count of file downloads as Google does not track file downloads, they track clicks.

    Knowing this, the right solution might be to wrap each download file in an individual page, where on that page you can include a redirect trigger script to essentially wait a few seconds (long enough to trigger the standard GA code to log your page view) then redirect the browser to the actual download file. This only counts the downloads from your site so you will need to contact the web masters who are referencing the files directly.

    Hope this helps.

    Paul

  • Paul Lear

    Nice solution! Sounds like the key change was to ensure the consistent extraction of the isThisDomain variable. Thanks for the follow-up!

  • Paul Lear

    Hi James,

    I took a quick look and didn’t see the inline test links in your footer, though I found your post on PyMOL B-factor visualisation immensely fun. Try adding this in your footer:

    %lt;a onclick=”var that=this;_gaq.push(['_trackEvent','Download','PyMOL',this.href]);setTimeout(function(){location.href=that.href;},400);return false;” href=”http://web.expasy.org/cgi-bin/protscale/protscale.pl”%gt;PyMOL with the blue_white_red palette%lt;/a%gt;

  • James Tolchard

    Hi Ryan,

    Sorry – I forgot to update my post. I fixed my problem. It turns out I was being stupid and trying to test the links in my site myself – whilst having “DoNotTrackMe” enabled in chrome. Turning this off instantly fixed my problem.

    Thanks for your reply though!

  • Peter Crackenberg

    Yep, I think all I ended up doing was adding one line and slightly changing how isThisDomain got calculated.

    Other than that, it’s worked great for us, thanks for the awesome tool!

  • Mike Betts

    I would like to get the adaptation made as well…

  • Tracey

    Thanks so much for this code! But, I have a question. I manage a large intranet site and use events for tracking things that happen on the site that I would like to know about like clicks on certain buttons, certain menu items, etc. I was using the inline code for tracking links to PDF documents but using the trackPageview instead of events. Is there any reason you track with events instead of just pageviews? Thanks!

  • http://www.blastam.com/ Olaf Calderon

    @disqus_kCZ2kwh4wn:disqus I took a look at your site but did not find this script implemented. I imagine you must have removed at some point in time. However, I did notice that you have the Universal Analytics snippet. Please be aware that the download tracking script as outlined above is for use with Classic Google Analytics. Universal Analytics no longer uses the _gaq object and has a different syntax so you would need to replace all of the _gaq.push calls with the appropriate UA method calls.

  • http://www.blastam.com/ Olaf Calderon

    @disqus_lxftEOH6aB:disqus Since Universal Analytics uses a different syntax, you would need to replace all of the _gaq.push methods with the appropriate UA syntax. Aside from that, nothing else would need to be changed. The script only contains one call to the _gaq so it’s fairly simple, however, if you’re using any inline calls, you’ll need to replace those as well.

  • http://www.blastam.com/ Olaf Calderon

    @disqus_7WXkQViOqq:disqus There is no problem implementing this via Google Tag manager. The only issue is that if you’re using the Google Analytics Tag template, you’ll have to namespace the tracker in both the GA tag and within the script for it to work properly. This assuming that there is no problem with jQuery and/or that jQuery is version 1.7 or higher as well.

  • http://www.blastam.com/ Olaf Calderon

    @disqus_0XEtDdvna6:disqus unfortunately not that we know of. Once the user clicks the right mouse button, it’s up to the operating system to capture what happens next. One thing you should think about though, is that not many people would right click on a .pdf document with an intent other than saving the doc so it’s a fairly good guess to say that right clicks are also downloads. I would think the 3 most “alternate” options with a right click would be Open ( including open in new tab/window ), Save and Print. Although you cannot print directly from the link, some users may not know this and try it. You could perform a print check. Look up onbeforeprint to know if the print dialog box has been activated – this would help cancel out that option from the right clicks. An Open or Save of the document is essentially the same thing since they’re most likely to save the document after opening it.

    I hope that helps!

  • Mangara

    The script as is breaks middle-click behaviour for external links in Chrome. Instead of opening in a new tab, they get a time-out and open in the original window.

    A possible fix is checking for the left mouse button before setting the time-out:
    if (event.which == 1 && (…)) { /* Set time-out */ }

  • http://www.blastam.com/ Olaf Calderon

    Hello Mangara, you do bring up a good point. I went and dished out an old microsoft mouse with that middle button but I wasn’t able to reproduce the problem you’re describing, do you happen to have a link I could take a look at?

  • Mangara

    Hi Olaf, I compressed my site to the smallest example that still exhibited the behaviour:

    http://cg.scs.carleton.ca/~sander/misc/GAClickTest/test.html

    Using the current version of Chrome (v 28) on Windows 7, when I click the link with the scroll-wheel or when I Ctrl-click it, it opens in the same tab instead of a new tab. Right-click and open in a new tab does work as expected. Both Firefox and IE work correctly (i.e. do not have the bug). It seems to be linked to https://bugs.webkit.org/show_bug.cgi?id=22382 and https://code.google.com/p/chromium/issues/detail?id=255

  • http://www.blastam.com/ Olaf Calderon

    Thank you Mangara, I was able to take a look. Something odd happens however. When I go to your link and I middle click, the link opens on the same tab, so then I downloaded the page Source and opened the file from my desktop. I middle clicked and the link opened on a new tab. Could you try that yourself? open it locally or at least with a non-aliased url?

  • Mangara

    Did you download the ga.js file as well? It includes the tracking code. I first noticed the behaviour when building a site locally, so I can confirm that it still happens.

  • Will Belden

    Would LOVE to see an update to this, based on the Google UNIVERSAL analytics. Because with the syntax like this (provided by Google for UA):
    ga(‘create’, ‘UA-327146……
    ga(‘send’, ‘pageview’);

    The code above doesn’t work. Clearly I’m not smart enough to rewrite the “push” line at the end of this suggested code to what it SHOULD be. Bear in mind that “_gaq” isn’t a valid variable anymore, it’s just “ga”.

    Would love to see an update, guys, awesome work!

  • Olivier

    Thanks a lot, it works well !
    I just have problems with double clicks. When a user double clicks on a link, the click event is fired twice (so two events sent to Google Analytics) and the document is downloaded twice. Any solution for that ?

  • http://mdj.us Matt

    Try replacing the _gaq.push line with this;


    ga('send', 'event', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, elEv.non_i);

    that *should* add send the events with the new Universal Analytics.

  • Will Belden

    Seems to be working! No errors at least. Haven’t actually checked the Analytics site for it.

  • http://mdj.us Matt

    coolio, I did it on my own site this afternoon and just checked GA, there’s two different events logged so it works on mine. thus far.

    hope it works for u 2, take care.

  • Harmeet Gabha

    Thanks Paul, I’m now using WP Download Monitor to track the count. Also modified one of its templates to push data into Google Analytics dynamically.

  • Angela Lindsey

    Can this be done via Google Tag Manager too?

  • Eric Erlebacher

    Hey Olaf, thanks for the help. I actually tried replacing _gaq.push with ga(‘send’… (the analytics.js syntax), and I get the following error in the debug console:

    Command ignored. Unknown target: undefined

    This could also be confounded by the fact that we are running both async and UA on our site. I noticed when attempting the original version, the UA-ID was XXXXX-X

    Thanks again!

  • Dany Giguere

    Hi Ryan. Ignore the message I sent you on Monday. I figured it out. Thanks for sharing !!!

  • Ben McCarty

    Thank you SO much for this incredible post, Ryan. I’ve run into an issue when implementing this via Google Tag Manager and I was hoping you (or another reader) would have a suggestion.

    When I debug the Events using GA Debug for Chrome, I see the Events firing but with an Account ID of UA-XXXXX-X (see attached screenshot). Any ideas why this might be happening and how to fix?

    Thanks in advance!

  • drgs100

    Thanks for this, been trying it out and seems to be working. No need to test if it will balls up the manual Event Tracking currently running…

  • Altabear

    Very nice post! We have a bunch of sites with contact forms. When someone fills one it, they’re redirected temporarily to a central asp page which determines where they came from, what they’re doing, grabs the lead information and then redirects back to the site they came from and it’s “thanks” page.

    We are in control of the sites and the one central asp page that they all connect to.

    Is there a preferred way to use your code for that? They are never actually downloading a file of any kind.

    Thanks in Advance

  • emarbee

    Hi James
    I have exactly the same problem. Could you please give me advice where and what I need to change in the script?
    Thanks very much

  • http://www.blastam.com/ Olaf Calderon

    Hello Eric,

    There’s no problem with running Classic and UA at the same time, you just need to make sure you have the snippets for both and that the settings are correct so that Classic ‘_set’s the account and UA ‘create’s the account.

    If you post a link I could take a quick look for you -

  • http://www.blastam.com/ Olaf Calderon

    Hello Grant,

    On the left side menu, go to Behavior > Events > Overview or Top Events. Here you’ll see a list of Categories coming in from the tracking code. If you implemented the code exactly as it appears above, you’ll see a category called “download”, click on that and you’ll see a new list with file extensions. Click on “pdf” to get a list of the pdf document links.

  • http://www.blastam.com/ Olaf Calderon

    Hello Tracey,

    You bring up a great point. Sometimes you want to track an action and report based on how many times it was performed. Additionally, you want to be able to pass it additional values so that you can identify the type of action taken. For this reason you want to use Events since you have 3 variable to populate, Category, Action, label. For example, with tracking links to PDF docs, you can set a Category to “download”, Action to “pdf” and label to the actual link of the pdf. Likewise other file types would fall under different Actions and would also carry the link. However, this doesn’t treat the “PDF” view as a pageview and will not allow for you to see it as a goal path or part of funnel. Perhaps your goal is the “PDF” download and you want a funnel leading up to it, in this case you MUST do it using a virtual pageview and you can pass the url of the file as the “page” value.

    So as you can see, it all depends on what you’re trying to do.

  • http://www.blastam.com/ Olaf Calderon

    Hello Simon,

    Thank you for bringing this point up.

    While the TOS states that you will not collect any PII, the email address that is being tracked with the script is the email address that is posted on the website. The fact that it’s published by the website owner disqualifies it as PII.

    If you had a form box where the user inputs their email address and submits, as is with most subscription widgets, then you need to make sure you don’t capture that and send it to GA since in that case the email address IS considered PII.

  • http://www.blastam.com/ Olaf Calderon

    Hello Altabear,

    There are two ways to do this. You can track the form submittal and pass the information to Google just prior to the DOM being dumped, or you can place some code on that central asp page that would pass the data to Google just before the redirect. It all depends on how you want to do it, the first scenario may require a delay, the second will require generating a response for the http request –

    A third way is to set a cookie an modify the values as the user clicks on the items you want to track. When the user submits, the latest version of the cookie will remain. Then you set code on the next page that checks for the cookie, parses it, fires tracking code as necessary prior to the initial pageview for the new page and then expires the cookie. This method has come in handy when forms have server side validation which makes it difficult sometimes to differentiate a form page from a submitted form page, from an error page after form submittal.

  • http://www.blastam.com/ Olaf Calderon

    Hello Olivier,

    I had this problem once, what I did is I have the click event increment a variable that is initially set to zero and then check the value prior to firing the GA event. This allows for the first click to trigger but then the second will have incremented the value and be cancelled out.

  • http://www.blastam.com/ Olaf Calderon

    Hello Angela,

    Yes, this can be done via GTM. However, the implementation can get a bit tricky unless you’re doing all of your GA code via a customHTML tag in GTM.

    As of late, they’ve come out with click events in GTM and this can be handled equally as easy within GTM. However, the current setup would require you to setup a separate event tag for each of the event types you’re trying to track and each would need a rule/macro to go with it.

    I’ve developed a method of having a single event with macro based dynamic values that is fired from within a customHTML tag and allow you to fire multiple events, pass custom values and all with one Event tag. I’ll be posting a blog on that soon, email me if you want a peek. :)

  • http://www.blastam.com/ Olaf Calderon

    Thanks for helping out Matt!

    We’ve fallen a little behind on some replies, it’s hard to keep up but yeah, if you follow the UA syntax, you just need to pass the values into the new syntax in the same way. I’ll see what I can do about updating this post to include a UA version -

  • http://www.blastam.com/ Olaf Calderon

    Hello Ben,

    Please make sure that the GA tag you’re using is for Classic and not Universal Analytics otherwise this event will not track properly. Aside from that, I’ve had instances where GTM namespaces the tracker so that when you fire a _trackEvent, it actually has no account associated with it. The namespace may be t0._trackEvent and thus you’d need to use that namespace throught out your code. The best way to ensure that your trackers are all synched is to namespace it yourself via the template UI, you can pas a “a” value to the name and then ensure all your GA code within customHTML tags use that namespace as such:

    _gaq.push(['a.trackPageview'])
    or
    _gaq.push([‘a.trackEvent’, ‘cat’, ‘act’, ‘lbl’ …

  • http://gioseo.com/ Zhan Yun

    Awesome! It takes only 10 seconds to setup and works perfectly, thanks a lot!!

  • nsherif

    would this hurt SEO in anyway? would the robot be still able to read and list links in my page?

  • Manish

    Waste of time to post here, did not recieve any response from here even my question was not posted by admin very annoying!! will never advice anyone to visit here.. Google forums is best place to get your query answer.. Bingo to Google

  • http://www.blastam.com Joe Christopher

    @disqus_jmO8gms41s:disqus As you can see, there are currently over 107 comments on this blog post. Just about all of them have a response from us. I don’t see a question from you on this thread… I do agree with you that Google help forums is the best place to ask general questions about GA. If you have a question about this free script we’ve provided, please let us know and we’ll do our best to answer it or point you in the right direction.

  • Bunnyslippers

    Works beautifully. Thank you!

  • Brett Burridge

    Thanks that looks a really awesome piece of code!

  • Art Thompson, Jr.

    Thanks for updating this, Ryan. Quick question, does this work as-is with the newer Universal Analytics embed code?

  • Mike

    How long should it take for event data to start showing up in google analytics after the above code has been added?

  • Art Thompson, Jr.

    Anyone? Anyone?

  • Josh C

    I have been using this for a bit, but noticed it breaks my lightbox pop-up script. Basically it loads youtube videos, pictures and audio files in a pop-up on the page when the link has rel=”lightbox” associated with it. Any ideas on how to add this exception?

  • Mat Leduc

    What can I input if I only want mail to and telephone tracking??

    Thank you very much for your help ; )

  • http://www.ptiwebtech.com/ Mahendra Dagdi

    Excellent post! Thanks

  • Kevin Harter

    Thank you so much for sharing this! I have implemented the code but don’t see any Events in my GA account. When I click on a download, it should show as an Event in the Real-Time Events, right?

  • Samantha

    Really interesting post. Do you have any idea if this can be used in a webpart on a Sharepoint website? I am familiar with adding jquery to a sharepoint html webpart so I may give this a go. Just curious if anyone has experience with this. Thank you.

  • Nick S

    Hi James,
    I’m having the same problem. I manage a site with .org.uk suffix and external links to other sites with .org.uk suffixes are not recorded as external links. Just wondered if you could share your code for the isThisDomain variable that takes account of domains that have more than one suffix.
    Thanks,
    Nick

  • Sarah

    Hi all, I came across this and recently used parts of the JQuery code to use on a project. I would recommend that instead of using a regex to detect external urls to use the filter method in JQuery:

    var anchor = $(‘a’);

    var external = anchor.filter(function() {
    return this.hostname && this.hostname !== location.hostname;
    });

    This is a much more reliable method. Using regex properly to cover all URL scenarios is a bit more difficult and as many of you in the comments found, there were errors.

    Thanks!

  • Ron Wright

    I implemented this code about a year ago – good stuff. After switching to Universal code, I noticed it doesn’t work anymore. Does this code work for anyone else using Universal Analytics code?

  • L_X_L

    This is excellent script that tracks everything, works out of the box! Google’s pushing the Tag Manager now – should we be switching to the Tag Manager and removing this script?

  • Crimson Diva

    Thanks! Do I add this code below my standard Google universal tracking code?

  • Div & Conquer

    Hi, I’ve been using this code a while but took it off back in March. At almost the exact same time my website traffic seemed to fall. It seems the code was attaching itself to internal links. Could it be possible GA has been counting them as extra traffic?



Goal Driven Online Marketing & Analytics
Copyright © 1999-2013 Blast Analytics & Marketing