Skip to content

Commit

Permalink
Merge pull request #10897 from wellcomecollection/tidy-consent-helpers
Browse files Browse the repository at this point in the history
Cookies: pass marketing consent value to `serverData` and `gtag`
  • Loading branch information
rcantin-w authored May 31, 2024
2 parents 2d26808 + 3e52306 commit 12df1ad
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
5 changes: 4 additions & 1 deletion common/server-data/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export async function getServerData(): Promise<ServerData> {
popupDialog: emptyPopupDialog(),
collectionVenues: emptyPrismicQuery<CollectionVenuePrismicDocument>(),
},
hasAnalyticsConsent: false,
consentStatus: {
analytics: false,
marketing: false,
},
};
}
7 changes: 5 additions & 2 deletions common/server-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ export const getServerData = async (
toggles[enableToggle].value = true;
}

const hasAnalyticsConsent = getConsentState('analytics', context);
const consentStatus = {
analytics: getConsentState('analytics', context),
marketing: getConsentState('marketing', context),
};

const serverData = { toggles, prismic, hasAnalyticsConsent };
const serverData = { toggles, prismic, consentStatus };

return simplifyServerData(serverData);
};
14 changes: 11 additions & 3 deletions common/server-data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@ import {
export type ServerData = {
toggles: Toggles;
prismic: PrismicData;
hasAnalyticsConsent: boolean;
consentStatus: ConsentStatusProps;
};

export type SimplifiedServerData = {
toggles: Toggles;
prismic: SimplifiedPrismicData;
hasAnalyticsConsent: boolean;
consentStatus: ConsentStatusProps;
};

export type ConsentStatusProps = {
analytics: boolean;
marketing: boolean;
};

export const defaultServerData: SimplifiedServerData = {
toggles: {},
prismic: prismicDefaultValue,
hasAnalyticsConsent: false,
consentStatus: {
analytics: false,
marketing: false,
},
};

/**
Expand Down
19 changes: 13 additions & 6 deletions common/services/app/google-analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FunctionComponent } from 'react';
import { Toggles } from '@weco/toggles';
import { ConsentStatusProps } from '@weco/common/server-data/types';

export type GaDimensions = {
partOf: string[];
Expand All @@ -11,7 +12,7 @@ type Props = {
data: {
toggles?: Toggles;
};
hasAnalyticsConsent: boolean;
consentStatus: ConsentStatusProps;
};

// We send toggles as an event parameter to GA4 so we can determine the condition in which a particular event took place.
Expand Down Expand Up @@ -45,7 +46,7 @@ function createABToggleString(toggles: Toggles | undefined): string | null {

export const Ga4DataLayer: FunctionComponent<Props> = ({
data,
hasAnalyticsConsent,
consentStatus,
}) => {
const abTestsToggleString = createABToggleString(data.toggles);

Expand All @@ -61,11 +62,17 @@ export const Ga4DataLayer: FunctionComponent<Props> = ({
gtag('consent', 'default', {
'analytics_storage': ${
hasAnalyticsConsent ? '"granted"' : '"denied"'
consentStatus.analytics ? '"granted"' : '"denied"'
},
'ad_storage': ${
consentStatus.marketing ? '"granted"' : '"denied"'
},
'ad_user_data': ${
consentStatus.marketing ? '"granted"' : '"denied"'
},
'ad_personalization': ${
consentStatus.marketing ? '"granted"' : '"denied"'
},
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied'
});`
: ``
}
Expand Down
9 changes: 5 additions & 4 deletions common/views/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GoogleTagManager,
GaDimensions,
} from '@weco/common/services/app/google-analytics';
import { ConsentStatusProps } from '@weco/common/server-data/types';

// Don't attempt to destructure the process object
// https://github.com/vercel/next.js/pull/20869/files
Expand All @@ -38,7 +39,7 @@ export function renderSegmentSnippet() {
type DocumentInitialPropsWithTogglesAndGa = DocumentInitialProps & {
toggles: Toggles;
gaDimensions?: GaDimensions;
hasAnalyticsConsent: boolean;
consentStatus: ConsentStatusProps;
};
class WecoDoc extends Document<DocumentInitialPropsWithTogglesAndGa> {
static async getInitialProps(
Expand All @@ -62,7 +63,7 @@ class WecoDoc extends Document<DocumentInitialPropsWithTogglesAndGa> {
...initialProps,
toggles: pageProps.serverData?.toggles,
gaDimensions: pageProps.gaDimensions,
hasAnalyticsConsent: pageProps.serverData?.hasAnalyticsConsent,
consentStatus: pageProps.serverData?.consentStatus,
styles: (
<>
{initialProps.styles}
Expand All @@ -79,15 +80,15 @@ class WecoDoc extends Document<DocumentInitialPropsWithTogglesAndGa> {
const cookiesWork = this.props.toggles?.cookiesWork?.value;

const shouldRenderAnalytics =
!cookiesWork || (cookiesWork && this.props.hasAnalyticsConsent);
!cookiesWork || (cookiesWork && this.props.consentStatus.analytics);

return (
<Html lang="en">
<Head>
<>
{/* Adding toggles etc. to the datalayer so they are available to events in Google Tag Manager */}
<Ga4DataLayer
hasAnalyticsConsent={this.props.hasAnalyticsConsent}
consentStatus={this.props.consentStatus}
data={{ toggles: this.props.toggles }}
/>

Expand Down

0 comments on commit 12df1ad

Please sign in to comment.