Track File Downloads in Google Analytics Using MooTools

By  on  

A while back I wrote an article detailing how you can use forced downloads via PHP to track downloads. What if you could get that same data into Google Analytics? Using some simple MooTools and advanced Google Analytics tactics, now you can.

The HTML

	
	<a href="/media/video.wmv" class="download">Download This Video!</a>
	<a href="/media/document.pdf" class="download">Download This PDF!</a>
	<a href="/media/document.doc" class="download">Download This Document!</a>
	<a href="/media/archive.zip" class="download">Download This Zip File!</a>
	

Any download I want tracked, I add the "download" CSS class to.

The MooTools 1.2

window.addEvent('load', function() {
	if(pageTracker) {
		$$('.download').addEvent('click',function() {
			pageTracker._trackPageview('/downloads/' + this.get('href').replace('http://',''));
		});
	}
});

Just like tracking AJAX clicks and outbound links, we direct Google Analytics to record the event.

Now you get download counts and it gets lumped in with the rest of your Google Analytics stats.

Recent Features

  • By
    How to Create a RetroPie on Raspberry Pi &#8211; Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    I&#8217;m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Google Extension Effect with CSS or jQuery or MooTools JavaScript

    Both of the two great browser vendors, Google and Mozilla, have Extensions pages that utilize simple but classy animation effects to enhance the page. One of the extensions used by Google is a basic margin-top animation to switch between two panes: a graphic pane...

  • By
    HTML5&#8217;s window.postMessage API

    One of the little known HTML5 APIs is the window.postMessage API.  window.postMessage allows for sending data messages between two windows/frames across domains.  Essentially window.postMessage acts as cross-domain AJAX without the server shims. Let's take a look at how window.postMessage works and how you...

Discussion

  1. Wow, didn’t know that this was possible, this is a great trick! Thanks!

  2. Thanks man! Even i’m not a big fan of Moo, i will surely use this with jquery :P
    Thanks again ;)

  3. A while back, I wrote a jQuery plugin that automatically adds tracking for external links, downloads, and mailto’s:

    http://devblog.jasonhuck.com/2007/11/19/google-analytics-integration-with-jquery/

    Rather than adding specific classes for each of these items, I based the tracking on the characteristics of the URL.

  4. Thanks man.. I don not know the MooTool can do this.

  5. Thanks David :) everyday i learn different things from you. Thanks lot.

  6. Recently, I extended Google Analytics to add download tracking for Google’s new Asynchronous Google Analytics Model. I call the extension Entourage.js:

    http://techoctave.com/c7/posts/58-entourage-js-automatic-download-tracking-for-asynchronous-google-analytics

    One of my goals was a small footprint, so it’s narrowly scoped to track file downloads only – no mailto or external links. Also, it’s framework agnostic. I wanted developers to have the freedom to use the extension and still use whatever JavaScript framework they want.

    I love jQuery, but I’ve always had much respect for the MooTools and Prototype.js community too.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!