2017-12-20 05:33:33 -06:00
|
|
|
import $ from 'jquery';
|
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import config from 'app/core/config';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
|
2017-10-26 06:22:45 -05:00
|
|
|
|
|
|
|
export class Analytics {
|
|
|
|
/** @ngInject */
|
2019-10-14 03:27:47 -05:00
|
|
|
constructor(private $rootScope: GrafanaRootScope, private $location: any) {}
|
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-09-03 04:30:44 -05:00
|
|
|
const ga = ((window as any).ga =
|
|
|
|
(window as any).ga ||
|
2020-02-07 19:40:04 -06:00
|
|
|
// this had the equivalent of `eslint-disable-next-line prefer-arrow/prefer-arrow-functions`
|
2017-12-19 09:06:54 -06:00
|
|
|
function() {
|
|
|
|
(ga.q = ga.q || []).push(arguments);
|
|
|
|
});
|
|
|
|
ga.l = +new Date();
|
2018-09-03 04:30:44 -05:00
|
|
|
ga('create', (config as any).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', () => {
|
2020-09-08 08:38:04 -05:00
|
|
|
const track = { page: `${config.appSubUrl ?? ''}${this.$location.url()}` };
|
2018-09-03 04:30:44 -05:00
|
|
|
const ga = (window as any).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 */
|
2019-04-28 02:58:12 -05:00
|
|
|
function startAnalytics(googleAnalyticsSrv: Analytics) {
|
2018-09-03 04:30:44 -05:00
|
|
|
if ((config as any).googleAnalyticsId) {
|
2017-10-26 06:22:45 -05:00
|
|
|
googleAnalyticsSrv.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.service('googleAnalyticsSrv', Analytics).run(startAnalytics);
|