Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gpeterso
Created August 9, 2019 16:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gpeterso/880c414af0aa1c80975e4436d4508223 to your computer and use it in GitHub Desktop.
Save gpeterso/880c414af0aa1c80975e4436d4508223 to your computer and use it in GitHub Desktop.
Tracking Primo pageviews in Google Analytics
const gaTrackingId = '...';
class GoogleAnalytics {
constructor($rootScope, $location, $window) {
this.$rootScope = $rootScope;
this.$location = $location;
this.$window = $window;
this.loadAnalytics(this.$window);
}
loadAnalytics(window) {
(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,window.document,'script','https://www.google-analytics.com/analytics.js','ga');
window.ga('create', gaTrackingId, 'auto');
window.ga('set', 'anonymizeIp', true);
}
trackPageviews() {
this.$rootScope.$on('$locationChangeSuccess', (event, newUrl, oldUrl) => {
this.$window.ga('send', 'pageview', {location: newUrl});
});
}
trackEvent(category, action, label) {
this.$window.ga('send', 'event', category, action, label);
}
}
GoogleAnalytics.$inject = ['$rootScope', '$location', '$window'];
angular
.module('googleAnalytics', [])
.service('googleAnalytics', GoogleAnalytics)
.run(['googleAnalytics', (googleAnalytics) => googleAnalytics.trackPageviews()]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment