Skip to content

Instantly share code, notes, and snippets.

@davetayls
Created October 12, 2011 15:02
Show Gist options
  • Save davetayls/1281442 to your computer and use it in GitHub Desktop.
Save davetayls/1281442 to your computer and use it in GitHub Desktop.
Google tracking for static files
/*
* Google tracking for static files
*/
/*jslint browser: true, vars: true, white: true, forin: true, nomen: true */
/*global window, jQuery, _gaq */
(function ($) {
"use strict";
var init_tracking = function () {
var $body = $('body');
$body.delegate("a", "click", function (e) {
e = e || window.event;
var t = e.target || e.srcElement,
ext,
fileName,
filetypes = /(zip|ZIP|pdf|PDF|csv|CSV|doc*|DOC*|xls*|XLS*|ashx)+$/;
try {
if (t.nodeName === "A") {
fileName = /([^\/])+$/.exec(t.href)[0];
ext = /[^\.]+$/.exec(fileName.split('&')[0])[0];
if (filetypes.test(ext)) {
var eventName = 'Downloaded - ' + ext + ' Document';
trackEvent('Download', eventName, fileName, '');
setTimeout(function () {
window.location.href = t.href;
}, 100);
return false;
}
}
} catch (ex) {
debug.error('Error trying to track download', ex);
}
});
};
var init_youtube_tracking = function () {
try {
var ytTracker = new YoutubeTracker(/*opt_bucket*/false, function (category, action, label, value) {
trackEvent(category, action, label, value);
}, /*debug*/false);
} catch (e) { }
};
var trackEvent = function(eventName, action, label, value) {
if (typeof _gaq !== 'undefined') {
_gaq = _gaq || [];
debug.log('Tracking', eventName, action, label, value);
_gaq.push(['_trackEvent', '\'' + eventName + '\'', '\'' + action + '\'', '\'' + label + '\'', 0]);
}
};
$(document).ready(function () { init_tracking(); init_youtube_tracking(); });
} (jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment