ngx-admin/src/app/@core/utils/analytics.service.ts

33 lines
759 B
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Location } from '@angular/common';
2018-05-10 23:48:39 +03:00
import { filter } from 'rxjs/operators';
2017-09-20 13:55:34 +03:00
declare const ga: any;
@Injectable()
export class AnalyticsService {
private enabled: boolean;
2017-09-20 13:55:34 +03:00
constructor(private location: Location, private router: Router) {
this.enabled = false;
2017-09-20 13:55:34 +03:00
}
trackPageViews() {
if (this.enabled) {
2018-05-10 23:48:39 +03:00
this.router.events.pipe(
filter((event) => event instanceof NavigationEnd),
)
2017-09-20 13:55:34 +03:00
.subscribe(() => {
ga('send', {hitType: 'pageview', page: this.location.path()});
2017-09-20 13:55:34 +03:00
});
}
}
trackEvent(eventName: string) {
if (this.enabled) {
ga('send', 'event', eventName);
}
}
2017-09-20 13:55:34 +03:00
}