Skip to content

Commit

Permalink
alerts-ui: integrate google analytics via gtag
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Jan 17, 2018
1 parent 8e27bb6 commit 9b4c6e8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
29 changes: 26 additions & 3 deletions alerts-ui/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

import 'rxjs/add/operator/distinctUntilChanged';

import { TranslateService } from '@ngx-translate/core';

import { environment } from '../environments/environment';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
export class AppComponent implements OnInit {
title = 'Crypto Coin Alerts';

constructor(translate: TranslateService) {
constructor(
translate: TranslateService,
private router: Router) {

translate.setDefaultLang('en');

// TODO: choose lang based on the user preferences
Expand All @@ -19,6 +28,20 @@ export class AppComponent {
translate.setTranslation('en', this.englishLang());
}

ngOnInit() {
// integrate google analytics via gtag - based on https://stackoverflow.com/a/47658214/3211175
this.router.events.distinctUntilChanged((previous: any, current: any) => {
// Subscribe to any `NavigationEnd` events where the url has changed
if (current instanceof NavigationEnd) {
return previous.url === current.url;
}

return true;
}).subscribe((x: any) => {
(<any>window).gtag('config', environment.gtag.id, { 'page_path': x.url });
});
}

englishLang(): Object {
return {
// default messages from angular
Expand Down
3 changes: 3 additions & 0 deletions alerts-ui/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ export const environment = {
},
recaptcha: {
siteKey: '6LewFT8UAAAAAIBai79H-KQH37pk83YRC5scVGdj'
},
gtag: {
id: 'UA-112495411-1'
}
};
4 changes: 4 additions & 0 deletions alerts-ui/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ export const environment = {
},
recaptcha: {
siteKey: '6Lesrz8UAAAAAEJT7iYUhWJfq04v8uTB-lFgFyOY'
},
gtag: {
// don't track events on dev
id: 'UA-XXXXXXXXX-X'
}
};
9 changes: 9 additions & 0 deletions alerts-ui/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-112495411-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
</script>

</head>
<body>
<app-root></app-root>
Expand Down

0 comments on commit 9b4c6e8

Please sign in to comment.