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