Skip to content

Commit

Permalink
feat: add google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosanif committed Jan 16, 2022
1 parent 68b4811 commit 6857211
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/app/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import { Component, ViewEncapsulation } from '@angular/core';
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs';

import { ConfigService, GoogleAnalyticsService } from '../core/services';

@Component({
selector: 'aa-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class AppComponent {}
export class AppComponent implements OnInit {
constructor(
private router: Router,
private configService: ConfigService,
private googleAnalyticsService: GoogleAnalyticsService
) {}

ngOnInit() {
if (this.configService.isProd()) {
this.setupGoogleAnalytics();
}
}

private setupGoogleAnalytics() {
this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(navigationEndEvent => {
this.googleAnalyticsService.sendPageView(
(navigationEndEvent as NavigationEnd).urlAfterRedirects
);
});
}
}
9 changes: 9 additions & 0 deletions src/app/core/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export class ConfigService {
return environment;
}

/**
* Indicates whether the apps is running in production mode
*
* @return {*} {boolean}
*/
isProd(): boolean {
return environment.production;
}

/**
* Returns app's version
*/
Expand Down
42 changes: 42 additions & 0 deletions src/app/core/services/google-analytics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Injectable } from '@angular/core';

declare const gtag: any;
const GOOGLE_ANALYTICS_ID = 'UA-217340656-1';

@Injectable({ providedIn: 'root' })
export class GoogleAnalyticsService {
protected gtag: any;

constructor() {
if (typeof gtag !== 'undefined') {
this.gtag = gtag;
}
}

sendEvent = (
eventName: string,
eventCategory: string,
eventAction: string,
eventLabel: string | null = null,
eventValue: number | null = null
) => {
if (!this.gtag) {
return;
}

this.gtag('event', eventName, {
eventCategory,
eventLabel,
eventAction,
eventValue,
});
};

sendPageView(url: string) {
if (!this.gtag) {
return;
}

this.gtag('config', GOOGLE_ANALYTICS_ID, { page_path: url });
}
}
1 change: 1 addition & 0 deletions src/app/core/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './config.service';
export * from './google-analytics.service';
export * from './local-storage.service';
export * from './token-storage.service';
14 changes: 14 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#5b0fff" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />

<!-- BEGIN: Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-217340656-1"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
</script>
<!-- END: Google Analytics -->
</head>
<body>
<aa-root></aa-root>
Expand Down

0 comments on commit 6857211

Please sign in to comment.