Created
August 9, 2019 16:44
-
-
Save gpeterso/880c414af0aa1c80975e4436d4508223 to your computer and use it in GitHub Desktop.
Tracking Primo pageviews in Google Analytics
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
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