mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
|
|
*/
|
|
|
|
// tslint:disable: only-arrow-functions
|
|
|
|
import { Injectable } from '@angular/core';
|
|
import { NavigationEnd, Router } from '@angular/router';
|
|
import { filter } from 'rxjs/operators';
|
|
import { UIOptions } from './../configurations';
|
|
import { Types } from './../utils/types';
|
|
import { ResourceLoaderService } from './resource-loader.service';
|
|
|
|
export const AnalyticsServiceFactory = (uiOptions: UIOptions, router: Router, resourceLoader: ResourceLoaderService) => {
|
|
return new AnalyticsService(uiOptions, router, resourceLoader);
|
|
};
|
|
|
|
@Injectable()
|
|
export class AnalyticsService {
|
|
private readonly gtag: any;
|
|
private readonly analyticsId: string;
|
|
|
|
constructor(
|
|
private readonly uiOptions?: UIOptions,
|
|
private readonly router?: Router,
|
|
private readonly resourceLoader?: ResourceLoaderService
|
|
) {
|
|
window['dataLayer'] = window['dataLayer'] || [];
|
|
|
|
this.gtag = function () {
|
|
window['dataLayer'].push(arguments);
|
|
};
|
|
|
|
if (this.uiOptions) {
|
|
this.analyticsId = this.uiOptions.get('google.analyticsId');
|
|
}
|
|
|
|
this.configureGtag();
|
|
}
|
|
|
|
public trackEvent(category: string, action: string, label?: string, value?: number) {
|
|
this.gtag('event', 'user-action', {
|
|
event_category: category,
|
|
event_action: action,
|
|
event_label: label,
|
|
value: value
|
|
});
|
|
}
|
|
|
|
private configureGtag() {
|
|
if (this.analyticsId && this.router && this.resourceLoader && window.location.hostname !== 'localhost') {
|
|
this.gtag('config', this.analyticsId, { anonymize_ip: true });
|
|
|
|
this.router.events.pipe(
|
|
filter(event => Types.is(event, NavigationEnd)))
|
|
.subscribe(() => {
|
|
this.gtag('config', this.analyticsId, { page_path: window.location.pathname, anonymize_ip: true });
|
|
});
|
|
|
|
this.loadScript();
|
|
}
|
|
}
|
|
|
|
private loadScript() {
|
|
if (document.cookie.indexOf('ga-disable') < 0 && this.resourceLoader) {
|
|
this.resourceLoader.loadScript(`https://www.googletagmanager.com/gtag/js?id=${this.analyticsId}`);
|
|
}
|
|
}
|
|
}
|