docs(misc): fix issue with GA events during local development (#31376)

This PR fixes an issue when you serve nx-dev locally and navigate
between pages in the browser. You'll get an error that `gtag` is not
defined since we never loaded it. We now check that we're in production
environment before sending events.
This commit is contained in:
Jack Hsu 2025-05-28 09:33:26 -04:00 committed by GitHub
parent d74c39e9be
commit c8a6ffb6ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,6 +12,7 @@ export function sendPageViewEvent(data: {
path?: string; path?: string;
title?: string; title?: string;
}): void { }): void {
if (process.env.NODE_ENV !== 'production') return;
try { try {
gtag('config', data.gaId, { gtag('config', data.gaId, {
...(!!data.path && { page_path: data.path }), ...(!!data.path && { page_path: data.path }),
@ -29,6 +30,7 @@ export function sendCustomEvent(
value?: number, value?: number,
customObject?: Record<string, unknown> customObject?: Record<string, unknown>
): void { ): void {
if (process.env.NODE_ENV !== 'production') return;
try { try {
gtag('event', action, { gtag('event', action, {
event_category: category, event_category: category,