ngx-admin/src/app/@core/utils/analytics.service.ts
2019-10-08 16:15:15 +03:00

37 lines
1 KiB
TypeScript

import { Inject, Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Location } from '@angular/common';
import { filter } from 'rxjs/operators';
import { NB_WINDOW } from '@nebular/theme';
@Injectable()
export class AnalyticsService {
private enabled = false;
constructor(@Inject(NB_WINDOW) private window,
private location: Location,
private router: Router) {
this.enabled = this.window.location.href.indexOf('akveo.com') >= 0;
}
trackPageViews() {
if (this.enabled) {
this.router.events.pipe(
filter((event) => event instanceof NavigationEnd),
)
.subscribe(() => {
this.gtmPushToDataLayer({event: 'pageView' , path: this.location.path()});
});
}
}
trackEvent(eventName: string, eventVal: string = '') {
if (this.enabled) {
this.gtmPushToDataLayer({ event: eventName, eventValue: eventVal });
}
}
private gtmPushToDataLayer(params) {
this.window.dataLayer.push(params);
}
}