analytics.service.ts 832 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import { Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter, tap } from 'rxjs/operators';
import { GoogleAnalyticsService } from './google-analytics.service';

@Injectable({ providedIn: 'root' })
export class AnalyticsService {
  constructor(private router: Router, private google: GoogleAnalyticsService) {
    this.router.events
      .pipe(
        filter(event => event instanceof NavigationEnd),
        tap(event => this.google.pageView((event as NavigationEnd).urlAfterRedirects))
      )
      .subscribe(event => {});
  }

  public recordEvent(action: string, category?: string, label?: string): void {
    this.google.recordEvent(action, category, label);
  }

  public recordError(exceptionMsg: string): void {
    this.google.recordException(exceptionMsg);
  }
}