Skip to content

Commit 83fd773

Browse files
authored
fix(analytics): fix ga setup (#268)
* fix(analytics): fix ga setup * fix(lint): fixed lint
1 parent 21d02ee commit 83fd773

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

apps/valor-software-site/src/app/app.component.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { filter, pairwise } from "rxjs/operators";
66
import { Subscription } from "rxjs";
77

88
const notFoundPageUrl = '/404';
9+
declare const gtag: any;
910

1011
@Component({
1112
selector: 'valor-software-site-base-root',
@@ -16,6 +17,7 @@ export class AppComponent implements OnDestroy, AfterViewInit {
1617
title = 'valor-software-site';
1718
scrollPosition = 0;
1819
$routeEvents?: Subscription;
20+
$gaRouteEvents?: Subscription;
1921
$routerEventNavigationEnd: Subscription;
2022
showFooterAndHeader = true;
2123

@@ -45,6 +47,7 @@ export class AppComponent implements OnDestroy, AfterViewInit {
4547
}
4648

4749
ngAfterViewInit() {
50+
this.handleGoogleAnalyticsTracking();
4851
setTimeout(() => {
4952
if (this.scrollPosition) {
5053
this.scrollToPosition(this.scrollPosition);
@@ -77,5 +80,22 @@ export class AppComponent implements OnDestroy, AfterViewInit {
7780
ngOnDestroy() {
7881
this.$routeEvents?.unsubscribe();
7982
this.$routerEventNavigationEnd?.unsubscribe();
83+
this.$gaRouteEvents?.unsubscribe();
8084
}
85+
86+
private handleGoogleAnalyticsTracking(): void {
87+
let configured = false;
88+
this.$gaRouteEvents = this.router.events.subscribe((event) => {
89+
if (event instanceof NavigationEnd) {
90+
if(!configured){
91+
gtag('config', 'UA-73071494-2', { send_page_view: false });
92+
configured = true;
93+
}
94+
gtag('event', 'page_view', {
95+
page_location: window.location.href,
96+
page_path: event.urlAfterRedirects,
97+
});
98+
}
99+
});
100+
}
81101
}

apps/valor-software-site/src/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
window.dataLayer = window.dataLayer || [];
2626
function gtag(){dataLayer.push(arguments);}
2727
gtag('js', new Date());
28-
29-
gtag('config', 'UA-73071494-2');
3028
</script>
3129
<script type="text/javascript">
3230
window.onload = function () {

0 commit comments

Comments
 (0)