analytics.ts 966 B

123456789101112131415161718192021222324252627282930313233343536
  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. }
  8. gaInit() {
  9. $.getScript('https://www.google-analytics.com/analytics.js'); // jQuery shortcut
  10. var ga = (<any>window).ga = (<any>window).ga || function () { (ga.q = ga.q || []).push(arguments); }; ga.l = +new Date;
  11. ga('create', (<any>config).googleAnalyticsId, 'auto');
  12. return ga;
  13. }
  14. init() {
  15. this.$rootScope.$on('$viewContentLoaded', () => {
  16. var track = { page: this.$location.url() };
  17. var ga = (<any>window).ga || this.gaInit();
  18. ga('set', track);
  19. ga('send', 'pageview');
  20. });
  21. }
  22. }
  23. /** @ngInject */
  24. function startAnalytics(googleAnalyticsSrv) {
  25. if ((<any>config).googleAnalyticsId) {
  26. googleAnalyticsSrv.init();
  27. }
  28. }
  29. coreModule.service('googleAnalyticsSrv', Analytics).run(startAnalytics);