diff --git a/libs/@guardian/libs/src/consent-management-platform/mergeUserConsent.ts b/libs/@guardian/libs/src/consent-management-platform/mergeUserConsent.ts new file mode 100644 index 000000000..921b70fb7 --- /dev/null +++ b/libs/@guardian/libs/src/consent-management-platform/mergeUserConsent.ts @@ -0,0 +1,27 @@ +import { getCookie } from '../cookies/getCookie'; +import { PROPERTY_ID_MAIN, PROPERTY_ID_SUPPORT } from './lib/sourcepointConfig'; +import type { SPUserConsent } from './types/tcfv2'; + +// https://sourcepoint-public-api.readme.io/reference/post_consent-v3-siteid-tcstring + +export const mergeUserConsent = () => { + const consentUUID = getCookie({ name: 'consentUUID' }); + const url = `https://cdn.privacy-mgmt.com/consent/tcfv2/consent/v3/${PROPERTY_ID_SUPPORT}/tcstring?consentUUID=${consentUUID}`; + const spUserConsentString = localStorage.getItem( + `_sp_user_consent_${PROPERTY_ID_MAIN}`, + ); + const userConsent = JSON.parse(spUserConsentString ?? '{}') as SPUserConsent; + + fetch(url, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + euconsent: userConsent.gdpr?.euconsent, + }), + }).catch((error) => { + console.error('Error:', error); + }); +}; diff --git a/libs/@guardian/libs/src/consent-management-platform/sourcepoint.ts b/libs/@guardian/libs/src/consent-management-platform/sourcepoint.ts index 65556cd4b..eb550f0e1 100644 --- a/libs/@guardian/libs/src/consent-management-platform/sourcepoint.ts +++ b/libs/@guardian/libs/src/consent-management-platform/sourcepoint.ts @@ -11,6 +11,7 @@ import { PROPERTY_ID_SUPPORT, SourcePointChoiceTypes, } from './lib/sourcepointConfig'; +import { mergeUserConsent } from './mergeUserConsent'; import { invokeCallbacks } from './onConsentChange'; import { stub } from './stub'; import type { ConsentFramework } from './types'; @@ -95,6 +96,10 @@ export const init = ( const isFeatureFlagEnabled = window.location.search.includes('CMP_COP'); const isMainSite = window.location.search.includes('CMP_MAIN'); + if (!isMainSite) { + mergeUserConsent(); + } + log('cmp', `framework: ${framework}`); log('cmp', `frameworkMessageType: ${frameworkMessageType}`); diff --git a/libs/@guardian/libs/src/consent-management-platform/types/tcfv2/index.ts b/libs/@guardian/libs/src/consent-management-platform/types/tcfv2/index.ts index efb8ed173..a1d94d45e 100644 --- a/libs/@guardian/libs/src/consent-management-platform/types/tcfv2/index.ts +++ b/libs/@guardian/libs/src/consent-management-platform/types/tcfv2/index.ts @@ -26,3 +26,9 @@ export type TCPingStatusCode = | 'visible' | 'hidden' | 'disabled'; + +export interface SPUserConsent { + gdpr?: { + euconsent?: string; + }; +}