grafana/public/app/core/services/analytics.ts
Steven Vachon f48169633c
TSLint → ESLint (#21006)
* Alphabetized tslint and tsconfig files

* Optimized tsconfig files

* Optimized editorconfig & prettier config files

… to reduce redundancy

* Switched to @grafana/tsconfig

… and:
* de-duped options
* removed options with default values

* Fixed nasty issue with types for nested slate-react

* Replaced TSLint with ESLint

* TSLint disables → ESLint disables

… also JSHint removals, which haven’t had an affect since it was replaced with TSLint.

* Compliances for ESLint, Prettier and TypeScript

* Updated lockfile
2020-02-08 02:40:04 +01:00

46 lines
1.2 KiB
TypeScript

import $ from 'jquery';
import coreModule from 'app/core/core_module';
import config from 'app/core/config';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
export class Analytics {
/** @ngInject */
constructor(private $rootScope: GrafanaRootScope, private $location: any) {}
gaInit() {
$.ajax({
url: 'https://www.google-analytics.com/analytics.js',
dataType: 'script',
cache: true,
});
const ga = ((window as any).ga =
(window as any).ga ||
// this had the equivalent of `eslint-disable-next-line prefer-arrow/prefer-arrow-functions`
function() {
(ga.q = ga.q || []).push(arguments);
});
ga.l = +new Date();
ga('create', (config as any).googleAnalyticsId, 'auto');
ga('set', 'anonymizeIp', true);
return ga;
}
init() {
this.$rootScope.$on('$viewContentLoaded', () => {
const track = { page: this.$location.url() };
const ga = (window as any).ga || this.gaInit();
ga('set', track);
ga('send', 'pageview');
});
}
}
/** @ngInject */
function startAnalytics(googleAnalyticsSrv: Analytics) {
if ((config as any).googleAnalyticsId) {
googleAnalyticsSrv.init();
}
}
coreModule.service('googleAnalyticsSrv', Analytics).run(startAnalytics);