analytics.ts 994 B

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