Skip to content

Instantly share code, notes, and snippets.

@adamcbrewer
Forked from stugoo/event-tracker-modernizr.js
Last active December 14, 2015 01:09
Show Gist options
  • Save adamcbrewer/5004273 to your computer and use it in GitHub Desktop.
Save adamcbrewer/5004273 to your computer and use it in GitHub Desktop.
JS: GA tracker with support for Modernizr tests
/**
* A Google Analytics event tracking proxy.
*
* This lobal allows us to do tests for the instantiation of _gaq
* and also allows us easier debugging in a test environment.
*
* Hat-tip to @stugoo for most the self-invoked function features!
*
*/
window.track = function (args) {
args = args || {};
// defaults
var category = args.category || '',
action = args.action || '',
label = args.label || '',
value = args.value || null;
if (_gaq) {
_gaq.push(['_trackEvent', category, action, label, value]);
}
};
(function (Modernizr) {
// set what you want to track here
var trackpasses = true,
trackfails = true,
/**
* Staging - check on our requirements of what to track
*/
trackFeature = function (results) {
if (trackpasses) {
SDM.track({ category: 'modernizr', action: 'passes', label: results.passed });
}
if (trackfails) {
SDM.track({ category: 'modernizr', action: 'fails', label: results.failed });
}
},
/**
* Run through all the Modernizr tests
*/
log = function() {
var tests = { passed: [], failed: [] };
// avoid functions, arrays and other objects
for (test in Modernizr) {
if (typeof Modernizr[test] === 'function' || typeof Modernizr[test] === 'array' || typeof Modernizr[test] === 'object') continue;
if (Modernizr[test]) {
tests.passed.push(test);
} else {
tests.failed.push(test);
}
}
// commas will break the GA _trackEvent method
tests.passed = tests.passed.join('|');
tests.failed = tests.failed.join('|');
trackFeature(tests);
};
if (trackpasses || trackfails) return log();
})(Modernizr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment