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

38 lines
1 KiB
TypeScript
Raw Normal View History

2019-07-15 18:52:01 +03:00
import { Inject, 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';
2019-07-15 18:52:01 +03:00
import { NB_WINDOW } from '@nebular/theme';
2017-09-20 13:55:34 +03:00
@Injectable()
export class AnalyticsService {
2019-07-15 18:52:01 +03:00
private enabled = false;
2017-09-20 13:55:34 +03:00
2019-07-15 18:52:01 +03:00
constructor(@Inject(NB_WINDOW) private window,
private location: Location,
private router: Router) {
this.enabled = this.window.location.href.indexOf('akveo.com') >= 0;
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(() => {
2019-07-15 18:52:01 +03:00
this.gtmPushToDataLayer({event: 'pageView' , path: this.location.path()});
2017-09-20 13:55:34 +03:00
});
}
}
2019-07-15 18:52:01 +03:00
trackEvent(eventName: string, eventVal: string = '') {
if (this.enabled) {
2019-07-15 18:52:01 +03:00
this.gtmPushToDataLayer({ event: eventName, eventValue: eventVal });
}
}
2019-07-15 18:52:01 +03:00
private gtmPushToDataLayer(params) {
this.window.dataLayer.push(params);
}
2017-09-20 13:55:34 +03:00
}