diff --git a/src/app/app-module.ts b/src/app/app-module.ts index 9560b519d..2586ab9b1 100644 --- a/src/app/app-module.ts +++ b/src/app/app-module.ts @@ -35,6 +35,7 @@ import { import {HttpClientModule} from '@angular/common/http'; import {ServiceWorkerModule} from '@angular/service-worker'; import {environment} from '../environments/environment'; +import {GaService} from './shared/ga/ga'; @NgModule({ imports: [ @@ -66,6 +67,7 @@ import {environment} from '../environments/environment'; providers: [ ComponentPageTitle, DocumentationItems, + GaService, GuideItems, StyleManager, ThemeStorage, diff --git a/src/app/material-docs-app.ts b/src/app/material-docs-app.ts index 803314edc..9b38c3e67 100644 --- a/src/app/material-docs-app.ts +++ b/src/app/material-docs-app.ts @@ -2,6 +2,7 @@ import {Component, ViewEncapsulation} from '@angular/core'; import {Router, NavigationEnd} from '@angular/router'; import {filter} from 'rxjs/operators/filter'; +import {GaService} from './shared/ga/ga'; @Component({ selector: 'material-docs-app', @@ -11,7 +12,7 @@ import {filter} from 'rxjs/operators/filter'; }) export class MaterialDocsApp { - constructor(router: Router) { + constructor(router: Router, ga: GaService) { let previousRoute = router.routerState.snapshot.url; router.events @@ -24,6 +25,7 @@ export class MaterialDocsApp { } previousRoute = data.urlAfterRedirects; + ga.locationChanged(data.urlAfterRedirects); }); } } diff --git a/src/app/shared/ga/ga.ts b/src/app/shared/ga/ga.ts new file mode 100644 index 000000000..8feea2cd5 --- /dev/null +++ b/src/app/shared/ga/ga.ts @@ -0,0 +1,41 @@ +import {Injectable} from '@angular/core'; + +import {environment} from '../../../environments/environment'; + +@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() { + this.ga('create', environment['matGaId'] , 'auto', 'mat'); + this.ga('create', environment['ngGaId'] , 'auto', 'ng'); + } + + locationChanged(url: string) { + this.sendPage(url); + } + + sendPage(url: string) { + // Won't re-send if the url hasn't changed. + if (url === this.previousUrl || !environment.production) { + return; + } + this.previousUrl = url; + this.ga('set', 'page', '/' + url); + this.ga('mat.send', 'pageview'); + this.ga('ng.send', 'pageview'); + } + + ga(...args: any[]) { + const gaFn = (window as any)['ga']; + if (gaFn) { + gaFn(...args); + } + } +} \ No newline at end of file diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index 3612073bc..7dee8d147 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -1,3 +1,5 @@ export const environment = { - production: true + matGaId: 'UA-8594346-24', // Production id + ngGaId: 'UA-8594346-15', // Production id + production: true, }; diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 00313f166..7adb8c883 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -4,5 +4,7 @@ // The list of which env maps to which file can be found in `angular-cli.json`. export const environment = { - production: false + matGaId: '', // No development id for Material + ngGaId: 'UA-8594346-26', // Development id + production: false, }; diff --git a/src/index.html b/src/index.html index f65bb507f..338314544 100644 --- a/src/index.html +++ b/src/index.html @@ -24,16 +24,20 @@