Skip to content

Commit

Permalink
Fixed #13626 - Add gtag for production environment
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincakiroglu committed Sep 21, 2023
1 parent 4b40014 commit 78911ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ global['navigator'] = win.navigator;
global['CSS'] = null;
// global['XMLHttpRequest'] = require('xmlhttprequest').XMLHttpRequest;
global['Prism'] = null;
// global google tag manager
global['gtag'] = () => {};

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
Expand Down
37 changes: 36 additions & 1 deletion src/app/showcase/layout/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import { Component, Inject, OnDestroy, OnInit, PLATFORM_ID, Renderer2 } from '@angular/core';
import { Subscription } from 'rxjs';
import { NavigationEnd, Router } from '@angular/router';
import { AppConfigService } from '../service/appconfigservice';
import Announcement from '../data/news.json';
declare let gtag: Function;

@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit, OnDestroy {
constructor(@Inject(DOCUMENT) private document: Document, @Inject(PLATFORM_ID) private platformId: any, private renderer: Renderer2, private configService: AppConfigService) {}
constructor(@Inject(DOCUMENT) private document: Document, @Inject(PLATFORM_ID) private platformId: any, private renderer: Renderer2, private configService: AppConfigService, private router: Router) {
if(isPlatformBrowser(platformId) && window && process.env.NODE_ENV === 'production'){
this.injectScripts();
}
this.handleRouteEvents();
}

public subscription: Subscription;

Expand Down Expand Up @@ -45,6 +52,34 @@ export class AppComponent implements OnInit, OnDestroy {
}
}

injectScripts() {
const script = this.renderer.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.googletagmanager.com/gtag/js?id=G-W297P962XH';
this.renderer.appendChild(this.document.body, script);

const scriptBody = this.renderer.createElement('script');
scriptBody.type = 'text/javascript';
scriptBody.text = `
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-W297P962XH');
`;
this.renderer.appendChild(this.document.body, scriptBody);
}

handleRouteEvents() {
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
gtag('event', 'page_view', {
page_path: event.urlAfterRedirects
});
}
});
}

ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
Expand Down

1 comment on commit 78911ac

@vercel
Copy link

@vercel vercel bot commented on 78911ac Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.