Blog

Squiz Matrix Tracking file downloads with Google Analytics and jQuery

Mon, May. 30, 2011

Since most Squiz Matrix files are served by apache, it is not as easy to log those downloads with Google Analytics. So, I wrote a little jQuery to bind click events to any links that look like files. Just make sure to add the Google tracking code above where you place this code:

$(function () {
    // Documents
    $("a[href*='.pdf'], a[href*='.doc'], a[href*='.xls'], a[href*='.ppt']").click(function () {
    	_gaq.push(['_trackPageview', '/doc/' + $(this).attr('href')]);
	});
	// Video   
    $("a[href*='.mov']").click(function () {
        _gaq.push(['_trackPageview', '/video/' + $(this).attr('href')]);
    });
    // File
    $("a[href*='.zip']").click(function () {
        _gaq.push(['_trackPageview', '/file/' + $(this).attr('href')]);
    });
    // Audio
    $("a[href*='.mp3']").click(function () {
        _gaq.push(['_trackPageview', '/audio/' + $(this).attr('href')]);
    });
});//end

Now, since we have added the file type to the data we send to Google, we just go to Google Analytics, click "Content", then "Top Content", and enter the file type to search for, such as /doc or /audio and it should find all the Matrix files that were downloaded, and their stats.

Nicholas Hubbard
Owner

Add Comment