From 07c4a1923cf788d194196989899c64512d17a941 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Tue, 8 Oct 2024 13:32:30 -0400 Subject: [PATCH] Update analytics to not depend on debug mode --- .env | 2 ++ src/services/analytics.ts | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.env b/.env index bc384a02..94c96aee 100644 --- a/.env +++ b/.env @@ -8,6 +8,8 @@ NEXT_PUBLIC_RELEASE_VERSION_URL=https://raw.githubusercontent.com/onc-healthit/s NEXT_PUBLIC_RELEASE_DATE_URL=https://raw.githubusercontent.com/onc-healthit/site-content/master/site-ui-4/release-date.md # TODO: Allow for these to be dynamic per environment file and delete from here. +# For now though, these are STATIC as this file is injected in the build, +# and NEXT_PUBLIC env vars are not overridden by other env file definitions # See: https://phase.dev/blog/nextjs-public-runtime-variables/ NEXT_PUBLIC_IS_DEBUG_MODE=false NEXT_PUBLIC_IS_EVENT_TRACKING=true diff --git a/src/services/analytics.ts b/src/services/analytics.ts index 7209fb49..cd7f4893 100644 --- a/src/services/analytics.ts +++ b/src/services/analytics.ts @@ -1,3 +1,5 @@ +'use client' +// Note: Explicitly defining 'use client' as this funciton uses 'window', which is part of the browser /* eventType: Describes the type of action being tracked, Button Click Form Submission, Dropdown Selection, Link Click, Sub Menu Anchor Link Click, etc. @@ -5,15 +7,14 @@ eventCategory: Groups events together, such as by a specific tool name or naviga eventLabel: Provides specific details for the event such as what the action and outcome is after the event is fired */ const eventTrack = (eventType: string, eventCategory: string, eventLabel: string) => { - if (typeof window.gtag === 'function') { - if (process.env.NEXT_PUBLIC_IS_DEBUG_MODE === 'true') { + if (process.env.NEXT_PUBLIC_IS_EVENT_TRACKING === 'true') { + if (typeof window.gtag === 'function') { console.info({ event: eventType, eventCategory: eventCategory, eventLabel: eventLabel, }) - } - if (process.env.NEXT_PUBLIC_IS_EVENT_TRACKING === 'true') { + window.gtag('event', eventType, { event_category: eventCategory, event_label: eventLabel,