diff --git a/common/services/app/civic-uk/index.ts b/common/services/app/civic-uk.ts similarity index 51% rename from common/services/app/civic-uk/index.ts rename to common/services/app/civic-uk.ts index 40fd656374..986630c77a 100644 --- a/common/services/app/civic-uk/index.ts +++ b/common/services/app/civic-uk.ts @@ -15,12 +15,20 @@ export const getAnalyticsConsentState = ( const consentCookie = cookies.CookieControl; // Ensures this returns true for regular users - // that don't have the "Cookie works" toggle on/have defined the consent cookie - if (isCookiesWorkToggleOn && consentCookie !== undefined) { - const civicUKCookie: CivicUKCookie = JSON.parse( - decodeURIComponent(consentCookie) - ); - return civicUKCookie.optionalCookies?.analytics === 'accepted'; + // that don't have the "Cookie works" toggle on + if (isCookiesWorkToggleOn) { + // If the feature flag is ON and consent has been defined, + // return its value + if (consentCookie !== undefined) { + const civicUKCookie: CivicUKCookie = JSON.parse( + decodeURIComponent(consentCookie) + ); + + return civicUKCookie.optionalCookies?.analytics === 'accepted'; + } else { + // If the feature flag is ON but consent has yet to be defined + return false; + } } else { return true; } diff --git a/common/services/app/google-analytics.tsx b/common/services/app/google-analytics.tsx index 54f03e375b..e4971ec853 100644 --- a/common/services/app/google-analytics.tsx +++ b/common/services/app/google-analytics.tsx @@ -11,13 +11,14 @@ type Props = { data: { toggles?: Toggles; }; + hasAnalyticsConsent: boolean; }; // We send toggles as an event parameter to GA4 so we can determine the condition in which a particular event took place. // GA4 now limits event parameter values to 100 characters: https://support.google.com/analytics/answer/9267744?hl=en // So instead of sending the whole toggles JSON blob, we only look at the "test" typed toggles and send a concatenated string made of the toggles' name // , preceeded with a! if its value is false. -function createToggleString(toggles: Toggles | undefined): string | null { +function createABToggleString(toggles: Toggles | undefined): string | null { const testToggles = toggles ? Object.keys(toggles).reduce((acc, key) => { if (toggles?.[key].type === 'test') { @@ -42,18 +43,40 @@ function createToggleString(toggles: Toggles | undefined): string | null { : null; } -export const Ga4DataLayer: FunctionComponent = ({ data }) => { - const toggleString = createToggleString(data.toggles); +export const Ga4DataLayer: FunctionComponent = ({ + data, + hasAnalyticsConsent, +}) => { + const abTestsToggleString = createABToggleString(data.toggles); - return toggleString ? ( + return data.toggles?.cookiesWork?.value || abTestsToggleString ? (