Skip to content

Instantly share code, notes, and snippets.

@robert-blankenship
Last active September 9, 2015 17:24
Show Gist options
  • Save robert-blankenship/97cc7603a2442aa43135 to your computer and use it in GitHub Desktop.
Save robert-blankenship/97cc7603a2442aa43135 to your computer and use it in GitHub Desktop.
ShopPad google analytics
(function () {
function getScriptURL() {
var scripts = document.getElementsByTagName('script');
return scripts[scripts.length - 1].src;
}
function getParameterByName(name, url) {
var parser = document.createElement('a');
parser.href = url;
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(parser.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
var googleAnalyticsId = getParameterByName('id', getScriptURL());
ga('create', googleAnalyticsId, 'auto');
ga('set', 'referrer', document.referrer);
ga('set', 'campaignMedium', getParameterByName('utm_medium', document.referrer));
ga('set', 'campaignSource', getParameterByName('utm_source', document.referrer));
ga('set', 'campaignName', getParameterByName('utm_campaign', document.referrer));
sendPage();
function sendPage(search) {
ga('send', 'pageview', {
'page': location.hash
});
};
function when(expression, callback) {
var interval = window.setInterval(function () {
if (eval(expression)) {
window.clearInterval(interval);
callback();
}
}, 100);
return function() {
window.clearInterval(interval);
};
}
when('typeof angular !== "undefined"', function () {
angular.element(document.body).scope().$on('$routeChangeStart', sendPage);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment