Created
March 5, 2015 08:51
-
-
Save fedotxxl/f688628ff67ae9356ed9 to your computer and use it in GitHub Desktop.
ga util functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
window._gaq = window._gaq || []; | |
var ga = wav.ga; | |
var eventsPercentage = wav.$config.data("ga-events-percentage"); | |
ga.getCurrentPageCategory = function() { | |
return "page_" + wav.getCurrentPage(); | |
}; | |
ga.isLoaded = function() { | |
//http://stackoverflow.com/questions/1954910/javascript-detect-if-google-analytics-is-loaded-yet | |
return window._gat && window._gat._getTracker; | |
}; | |
ga.trackPageView = function(url) { | |
ga.pageEvent("open", null, null, true); | |
if (url) _gaq.push(['_trackPageview', url]); | |
}; | |
ga.event = function(category, action, label, value, nonInteraction, force) { | |
wav.console.debug("ga event: ", arguments); | |
if (isProcessEvent(force)) { | |
_gaq.push(['_trackEvent', category, action, label, value, nonInteraction]); | |
} | |
}; | |
ga.pageEvent = function(action, label, value, nonInteraction, force) { | |
ga.event(ga.getCurrentPageCategory(), action, label, value, nonInteraction, force); | |
}; | |
ga.eventWithCallback = function(category, action, label, callback, force) { | |
wav.console.debug("ga event: ", arguments); | |
if (ga.isLoaded() && isProcessEvent(force)) { | |
//http://stackoverflow.com/questions/4086587/track-event-in-google-analytics-upon-clicking-form-submit | |
_gaq.push(['_set', 'hitCallback', function() { | |
_gaq.push(['_set', 'hitCallback', null]); | |
callback(); | |
}]); | |
_gaq.push(['_trackEvent', category, action, label]); | |
} else { | |
callback(); | |
} | |
}; | |
ga.eventAndProcessLink = function(e, target, category, action, label, force) { | |
if (ga.isLoaded() && isProcessEvent(force)) { | |
e.preventDefault(); | |
ga.eventWithCallback(category, action, label, function() { | |
window.location.href = (target || e.target).href; | |
}, true); | |
} else { | |
//do process link click | |
} | |
}; | |
var isProcessEvent = function(forced) { | |
return forced || eventsPercentage >= (Math.random()*100) | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment