https://github.com/angular/angular
Raw File
Tip revision: 2fa61e8ee11bbbe4e9bafa9dce907e91aa2ab095 authored by George Kalpakas on 04 February 2019, 14:45:45 UTC
build(docs-infra): use pinned dependencies when possible in `ng-packages-installer` (#28510)
Tip revision: 2fa61e8
ga.service.ts
import { Inject, Injectable } from '@angular/core';

import { environment } from '../../environments/environment';
import { WindowToken } from 'app/shared/window';

@Injectable()
/**
 * Google Analytics Service - captures app behaviors and sends them to Google Analytics (GA).
 * Presupposes that GA script has been loaded from a script on the host web page.
 * Associates data with a GA "property" from the environment (`gaId`).
 */
export class GaService {

  private previousUrl: string;

  constructor(@Inject(WindowToken) private window: Window) {
    this.ga('create', environment['gaId'] , 'auto');
  }

  locationChanged(url: string) {
    this.sendPage(url);
  }

  sendPage(url: string) {
    // Won't re-send if the url hasn't changed.
    if (url === this.previousUrl) { return; }
    this.previousUrl = url;
    this.ga('set', 'page', '/' + url);
    this.ga('send', 'pageview');
  }

  ga(...args: any[]) {
    const gaFn = (this.window as any)['ga'];
    if (gaFn) {
      gaFn(...args);
    }
  }
}
back to top