Skip to content

Commit

Permalink
Add google analytics feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kamarmack committed Dec 12, 2023
1 parent 2253084 commit 700b481
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/config/enableGoogleAnalytics.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions src/config/nodeEnv.ts
Original file line number Diff line number Diff line change
@@ -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';
38 changes: 22 additions & 16 deletions src/lib/googleAnalytics.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,8 +11,9 @@ declare global {
}

const gtagConfig = (config: Record<string, string | number>) => {
if (!isDomAvailable()) return;
window?.gtag?.('config', FIREBASE_CONFIG.measurementId, config);
if (ENABLE_GOOGLE_ANALYTICS) {
window?.gtag?.('config', FIREBASE_CONFIG.measurementId, config);
}
};

export type GoogleAnalyticsAction =
Expand Down Expand Up @@ -164,11 +165,12 @@ const gtagEvent = (
action: GoogleAnalyticsAction,
params: Record<string, string | number | GoogleAnalyticsItem[]>,
) => {
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 = {
Expand All @@ -179,30 +181,34 @@ 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<string, string | number>) => {
if (!isDomAvailable()) return;
window?.gtag?.('set', params);
if (ENABLE_GOOGLE_ANALYTICS) {
window?.gtag?.('set', params);
}
};

const gtagSetTargetScope = (
target: string,
params: Record<string, string | number>,
) => {
if (!isDomAvailable()) return;
window?.gtag?.('set', target, params);
if (ENABLE_GOOGLE_ANALYTICS) {
window?.gtag?.('set', target, params);
}
};

const gtagGet = (
target: string,
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 = {
Expand Down

0 comments on commit 700b481

Please sign in to comment.