analytics.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. var 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. return ga;
  21. }
  22. init() {
  23. this.$rootScope.$on('$viewContentLoaded', () => {
  24. var track = { page: this.$location.url() };
  25. var ga = (<any>window).ga || this.gaInit();
  26. ga('set', track);
  27. ga('send', 'pageview');
  28. });
  29. }
  30. }
  31. /** @ngInject */
  32. function startAnalytics(googleAnalyticsSrv) {
  33. if ((<any>config).googleAnalyticsId) {
  34. googleAnalyticsSrv.init();
  35. }
  36. }
  37. coreModule.service('googleAnalyticsSrv', Analytics).run(startAnalytics);