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