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

32 lines
749 B
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Location } from '@angular/common';
2017-09-20 13:55:34 +03:00
import { filter } from 'rxjs/operator/filter';
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) {
filter.call(this.router.events, (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
}