From 280edb4c4fff49d22454ab38fdf90e443e2a4f14 Mon Sep 17 00:00:00 2001 From: Robert Helmer Date: Tue, 13 Feb 2024 15:53:14 -0800 Subject: [PATCH] MNTOR-2957 - avoid loading client-side analytics on server (#4207) * MNTOR-2957 - avoid loading client-side analytics on server --- src/app/components/client/GoogleAnalyticsWorkaround.tsx | 5 +++++ src/app/hooks/useGlean.ts | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/app/components/client/GoogleAnalyticsWorkaround.tsx b/src/app/components/client/GoogleAnalyticsWorkaround.tsx index 3f1f405bb..aed11c0dc 100644 --- a/src/app/components/client/GoogleAnalyticsWorkaround.tsx +++ b/src/app/components/client/GoogleAnalyticsWorkaround.tsx @@ -91,6 +91,11 @@ export const sendGAEvent = (type: "event", eventName: string, args: object) => { return; } + if (typeof window === "undefined") { + console.warn("GA4 should only be used on the client"); + return; + } + if (currDataLayerName === undefined) { console.warn(`@next/third-parties: GA has not been initialized`); return; diff --git a/src/app/hooks/useGlean.ts b/src/app/hooks/useGlean.ts index 54a7c698a..87de6c76b 100644 --- a/src/app/hooks/useGlean.ts +++ b/src/app/hooks/useGlean.ts @@ -15,6 +15,11 @@ export const useGlean = () => { // Initialize Glean only on the first render of our custom hook. useEffect(() => { + if (typeof window === "undefined") { + console.warn("Glean should only be used on the client"); + return; + } + // Enable upload only if the user has not opted out of tracking. const uploadEnabled = navigator.doNotTrack !== "1" || @@ -54,6 +59,10 @@ export const useGlean = () => { event: keyof GleanMetricMap[EventModule], data: GleanMetricMap[EventModule][EventName], ) => { + if (typeof window === "undefined") { + console.warn("Glean should only be used on the client"); + return; + } const mod = (await import( `../../telemetry/generated/${eventModule}` )) as Record;