2017-12-20 12:33:33 +01:00
|
|
|
import $ from 'jquery';
|
|
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
|
import config from 'app/core/config';
|
2017-10-26 13:22:45 +02:00
|
|
|
|
|
|
|
|
export class Analytics {
|
|
|
|
|
/** @ngInject */
|
2017-12-19 16:06:54 +01:00
|
|
|
constructor(private $rootScope, private $location) {}
|
2017-10-26 13:22:45 +02:00
|
|
|
|
|
|
|
|
gaInit() {
|
2018-04-26 15:49:22 +02:00
|
|
|
$.ajax({
|
|
|
|
|
url: 'https://www.google-analytics.com/analytics.js',
|
|
|
|
|
dataType: 'script',
|
|
|
|
|
cache: true,
|
|
|
|
|
});
|
2018-09-03 11:30:44 +02:00
|
|
|
const ga = ((window as any).ga =
|
|
|
|
|
(window as any).ga ||
|
2018-09-05 15:28:30 +02:00
|
|
|
//tslint:disable-next-line:only-arrow-functions
|
2017-12-19 16:06:54 +01:00
|
|
|
function() {
|
|
|
|
|
(ga.q = ga.q || []).push(arguments);
|
|
|
|
|
});
|
|
|
|
|
ga.l = +new Date();
|
2018-09-03 11:30:44 +02:00
|
|
|
ga('create', (config as any).googleAnalyticsId, 'auto');
|
2018-05-08 04:48:27 -04:00
|
|
|
ga('set', 'anonymizeIp', true);
|
2017-10-26 13:22:45 +02:00
|
|
|
return ga;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init() {
|
2017-12-20 12:33:33 +01:00
|
|
|
this.$rootScope.$on('$viewContentLoaded', () => {
|
2018-11-19 18:21:37 +00:00
|
|
|
const track = { page: this.$location.url() };
|
2018-09-03 11:30:44 +02:00
|
|
|
const ga = (window as any).ga || this.gaInit();
|
2017-12-20 12:33:33 +01:00
|
|
|
ga('set', track);
|
|
|
|
|
ga('send', 'pageview');
|
2017-10-26 13:22:45 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @ngInject */
|
|
|
|
|
function startAnalytics(googleAnalyticsSrv) {
|
2018-09-03 11:30:44 +02:00
|
|
|
if ((config as any).googleAnalyticsId) {
|
2017-10-26 13:22:45 +02:00
|
|
|
googleAnalyticsSrv.init();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
coreModule.service('googleAnalyticsSrv', Analytics).run(startAnalytics);
|