analytics.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 = ((window as any).ga =
  14. (window as any).ga ||
  15. //tslint:disable-next-line:only-arrow-functions
  16. function() {
  17. (ga.q = ga.q || []).push(arguments);
  18. });
  19. ga.l = +new Date();
  20. ga('create', (config as any).googleAnalyticsId, 'auto');
  21. ga('set', 'anonymizeIp', true);
  22. return ga;
  23. }
  24. init() {
  25. this.$rootScope.$on('$viewContentLoaded', () => {
  26. const track = { page: this.$location.url() };
  27. const ga = (window as any).ga || this.gaInit();
  28. ga('set', track);
  29. ga('send', 'pageview');
  30. });
  31. }
  32. }
  33. /** @ngInject */
  34. function startAnalytics(googleAnalyticsSrv) {
  35. if ((config as any).googleAnalyticsId) {
  36. googleAnalyticsSrv.init();
  37. }
  38. }
  39. coreModule.service('googleAnalyticsSrv', Analytics).run(startAnalytics);