analytics.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import $ from 'jquery';
  2. import coreModule from 'app/core/core_module';
  3. import config from 'app/core/config';
  4. export class Analytics {
  5. /** @ngInject */
  6. constructor(private $rootScope, private $location) {}
  7. gaInit() {
  8. $.ajax({
  9. url: 'https://www.google-analytics.com/analytics.js',
  10. dataType: 'script',
  11. cache: true,
  12. });
  13. const ga = ((<any>window).ga =
  14. (<any>window).ga ||
  15. function() {
  16. (ga.q = ga.q || []).push(arguments);
  17. });
  18. ga.l = +new Date();
  19. ga('create', (<any>config).googleAnalyticsId, 'auto');
  20. ga('set', 'anonymizeIp', true);
  21. return ga;
  22. }
  23. init() {
  24. this.$rootScope.$on('$viewContentLoaded', () => {
  25. const track = { page: this.$location.url() };
  26. const ga = (<any>window).ga || this.gaInit();
  27. ga('set', track);
  28. ga('send', 'pageview');
  29. });
  30. }
  31. }
  32. /** @ngInject */
  33. function startAnalytics(googleAnalyticsSrv) {
  34. if ((<any>config).googleAnalyticsId) {
  35. googleAnalyticsSrv.init();
  36. }
  37. }
  38. coreModule.service('googleAnalyticsSrv', Analytics).run(startAnalytics);