diff --git a/src/config/enableGoogleAnalytics.ts b/src/config/enableGoogleAnalytics.ts new file mode 100644 index 0000000..d1aa87f --- /dev/null +++ b/src/config/enableGoogleAnalytics.ts @@ -0,0 +1,8 @@ +import { isDomAvailable } from '../utils/isDomAvailable'; +import { FIREBASE_CONFIG } from './firebaseConfig'; +import { NODE_ENV } from './nodeEnv'; + +export const ENABLE_GOOGLE_ANALYTICS = + isDomAvailable() && + NODE_ENV === 'production' && + !!FIREBASE_CONFIG.measurementId; diff --git a/src/config/nodeEnv.ts b/src/config/nodeEnv.ts new file mode 100644 index 0000000..1e2ed01 --- /dev/null +++ b/src/config/nodeEnv.ts @@ -0,0 +1,6 @@ +export type NodeEnvironment = 'development' | 'production' | 'test'; + +export const NODE_ENV: 'development' | 'production' | 'test' = + process.env.JEST_WORKER_ID === undefined + ? (process.env.NODE_ENV as NodeEnvironment | undefined) || 'development' + : 'test'; diff --git a/src/lib/googleAnalytics.ts b/src/lib/googleAnalytics.ts index f66e4bc..69b0382 100644 --- a/src/lib/googleAnalytics.ts +++ b/src/lib/googleAnalytics.ts @@ -1,5 +1,5 @@ +import { ENABLE_GOOGLE_ANALYTICS } from '../config/enableGoogleAnalytics'; import { FIREBASE_CONFIG } from '../config/firebaseConfig'; -import { isDomAvailable } from '../utils/isDomAvailable'; declare global { interface Window { @@ -11,8 +11,9 @@ declare global { } const gtagConfig = (config: Record) => { - if (!isDomAvailable()) return; - window?.gtag?.('config', FIREBASE_CONFIG.measurementId, config); + if (ENABLE_GOOGLE_ANALYTICS) { + window?.gtag?.('config', FIREBASE_CONFIG.measurementId, config); + } }; export type GoogleAnalyticsAction = @@ -164,11 +165,12 @@ const gtagEvent = ( action: GoogleAnalyticsAction, params: Record, ) => { - if (!isDomAvailable()) return; - window?.gtag?.('event', action, { - ...params, - send_to: FIREBASE_CONFIG.measurementId, - }); + if (ENABLE_GOOGLE_ANALYTICS) { + window?.gtag?.('event', action, { + ...params, + send_to: FIREBASE_CONFIG.measurementId, + }); + } }; export type GtagConsentParams = { @@ -179,21 +181,24 @@ export type GtagConsentParams = { wait_for_update?: number; }; const gtagConsent = (params: GtagConsentParams) => { - if (!isDomAvailable()) return; - window?.gtag?.('consent', params); + if (ENABLE_GOOGLE_ANALYTICS) { + window?.gtag?.('consent', params); + } }; const gtagSetGlobalScope = (params: Record) => { - if (!isDomAvailable()) return; - window?.gtag?.('set', params); + if (ENABLE_GOOGLE_ANALYTICS) { + window?.gtag?.('set', params); + } }; const gtagSetTargetScope = ( target: string, params: Record, ) => { - if (!isDomAvailable()) return; - window?.gtag?.('set', target, params); + if (ENABLE_GOOGLE_ANALYTICS) { + window?.gtag?.('set', target, params); + } }; const gtagGet = ( @@ -201,8 +206,9 @@ const gtagGet = ( fieldName: string, callback: (field: string) => void, ) => { - if (!isDomAvailable()) return; - window?.gtag?.('get', target, fieldName, callback); + if (ENABLE_GOOGLE_ANALYTICS) { + window?.gtag?.('get', target, fieldName, callback); + } }; export const googleAnalytics = {