Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 6747c77

Browse files
chore: send analytics data on all navigation ends to both material and ng ids. (#401)
1 parent f1e612c commit 6747c77

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

src/app/app-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
import {HttpClientModule} from '@angular/common/http';
3636
import {ServiceWorkerModule} from '@angular/service-worker';
3737
import {environment} from '../environments/environment';
38+
import {GaService} from './shared/ga/ga';
3839

3940
@NgModule({
4041
imports: [
@@ -66,6 +67,7 @@ import {environment} from '../environments/environment';
6667
providers: [
6768
ComponentPageTitle,
6869
DocumentationItems,
70+
GaService,
6971
GuideItems,
7072
StyleManager,
7173
ThemeStorage,

src/app/material-docs-app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Component, ViewEncapsulation} from '@angular/core';
22
import {Router, NavigationEnd} from '@angular/router';
33
import {filter} from 'rxjs/operators/filter';
44

5+
import {GaService} from './shared/ga/ga';
56

67
@Component({
78
selector: 'material-docs-app',
@@ -11,7 +12,7 @@ import {filter} from 'rxjs/operators/filter';
1112
})
1213
export class MaterialDocsApp {
1314

14-
constructor(router: Router) {
15+
constructor(router: Router, ga: GaService) {
1516
let previousRoute = router.routerState.snapshot.url;
1617

1718
router.events
@@ -24,6 +25,7 @@ export class MaterialDocsApp {
2425
}
2526

2627
previousRoute = data.urlAfterRedirects;
28+
ga.locationChanged(data.urlAfterRedirects);
2729
});
2830
}
2931
}

src/app/shared/ga/ga.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {Injectable} from '@angular/core';
2+
3+
import {environment} from '../../../environments/environment';
4+
5+
@Injectable()
6+
/**
7+
* Google Analytics Service - captures app behaviors and sends them to Google Analytics (GA).
8+
* Presupposes that GA script has been loaded from a script on the host web page.
9+
* Associates data with a GA "property" from the environment (`gaId`).
10+
*/
11+
export class GaService {
12+
13+
private previousUrl: string;
14+
15+
constructor() {
16+
this.ga('create', environment['matGaId'] , 'auto', 'mat');
17+
this.ga('create', environment['ngGaId'] , 'auto', 'ng');
18+
}
19+
20+
locationChanged(url: string) {
21+
this.sendPage(url);
22+
}
23+
24+
sendPage(url: string) {
25+
// Won't re-send if the url hasn't changed.
26+
if (url === this.previousUrl || !environment.production) {
27+
return;
28+
}
29+
this.previousUrl = url;
30+
this.ga('set', 'page', '/' + url);
31+
this.ga('mat.send', 'pageview');
32+
this.ga('ng.send', 'pageview');
33+
}
34+
35+
ga(...args: any[]) {
36+
const gaFn = (window as any)['ga'];
37+
if (gaFn) {
38+
gaFn(...args);
39+
}
40+
}
41+
}

src/environments/environment.prod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const environment = {
2-
production: true
2+
matGaId: 'UA-8594346-24', // Production id
3+
ngGaId: 'UA-8594346-15', // Production id
4+
production: true,
35
};

src/environments/environment.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
// The list of which env maps to which file can be found in `angular-cli.json`.
55

66
export const environment = {
7-
production: false
7+
matGaId: '', // No development id for Material
8+
ngGaId: 'UA-8594346-26', // Development id
9+
production: false,
810
};

src/index.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@
2424
<body class="docs-app-background">
2525
<material-docs-app></material-docs-app>
2626
<script>
27-
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
28-
ga('create', 'UA-8594346-24', 'auto');
29-
ga('send', 'pageview');
27+
// Note this is a customised version of the GA tracking snippet
28+
// See the comments below for more info
29+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
30+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
31+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;
32+
~i.name.indexOf('NG_DEFER_BOOTSTRAP')|| // only load library if not running e2e tests
33+
m.parentNode.insertBefore(a,m)
34+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
3035
</script>
3136
<script>
3237
// This hides the address bar on mobile browsers
3338
// https://developers.google.com/web/fundamentals/native-hardware/fullscreen/
3439
window.addEventListener('load',function() { window.scrollTo(0, 1); });
3540
</script>
36-
<script async src='https://www.google-analytics.com/analytics.js'></script>
3741
<script src="https://ajax.googleapis.com/ajax/libs/hammerjs/2.0.8/hammer.min.js"></script>
3842
<script src="https://cdnjs.cloudflare.com/ajax/libs/web-animations/2.2.5/web-animations.min.js"></script>
3943
</body>

0 commit comments

Comments
 (0)