Tracking Downloads with Google Analytics »

2

Tags: jQuery

I use Google Analytics as my way of tracking traffic data for this website. Although this provides an excellent method of analyzing the statistics, I found that downloads links were not getting picked up through the standard way provided by Google.

The method below explains the steps that are needed to be followed in order for Google Analytics to track static links that do not need rendering in the browser. Links such as direct downloads to any file type.

Google Analytics and jQuery is needed to get this working properly and enable full tracking by Analytics.

Once these have been added to the website the following javascript snippet needs to be added, this is the code that will use Analytics to track when a user downloads a file from the site.

$(document).ready(function() {
    // find all the links with 'download' in the rel tag and assign click event
    $("a[rel^=download]").click(function() {
        
        var _rel = $(this).attr('rel').split('-');
        // use string to add to start to make it easily identifiable in analytics        
        var _filter = (_rel.length » 1) ? '/'+_rel[1]+'/' : '';
        // build link text to use for pagetracking
        var _linktext = _filter+$(this).attr('href');
        
        pageTracker._trackPageview(_linktext);
        
        return true;
    });
});

In each of the links that need to be tracked, an extra attribute needs to be added. The 'rel' attribute can be included with the anchor link to specify that it needs to be tracked by Analytics. The code below demonstrates this :

<a rel="download" href="%{LINK}%">%{HTML}%</a>

 

The rel attribute will enable google to track the download and display it in the page hit part of the report under '/downloads/'. If you require to be more specific and include the type of download - .zip/.jpeg/.pdf - then we can alter the rel tag to include the file type within Analytics

<a rel="download-zip" href="%{LINK}%">%{HTML}%</a>

 

By checking Google Analytics the number of downloads can be tracked and an idea of how popular the file is can be seen.

screenshot of Google Analytics with tracked downloads

2 responses to “Tracking Downloads with Google Analytics”

  1. Mark Aplet Says:
    Great Plugin. Fond only one small discrepancy. The plugin description as used in the plugin.xml reads "Just add a class of 'anchorSlide' to your links. That's It! jQuery does the rest!" I think this is leftover from one of my old plugins that you used as a template.
  2. jbuda Says:
    @Mark Sorry - just updated the plugin and removed the description left over from your plugin. Thanks for the pointer!
leave a reply