Skip to content

Commit

Permalink
Send consent to Ophan
Browse files Browse the repository at this point in the history
  • Loading branch information
akinsola-guardian committed Dec 19, 2024
1 parent 79f9fd3 commit 72ba889
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
52 changes: 50 additions & 2 deletions support-frontend/assets/helpers/page/analyticsAndConsent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ----- Imports ----- //

import { cmp, getCookie } from '@guardian/libs';
import type { ConsentState} from '@guardian/libs';
import { cmp, getCookie, onConsent } from '@guardian/libs';
import ophan from 'ophan';
import type { Participations } from 'helpers/abTests/abtest';
import type { IsoCountry } from 'helpers/internationalisation/country';
Expand Down Expand Up @@ -57,6 +58,53 @@ function consentInitialisation(country: IsoCountry): void {
}
}

function sendConsentToOphan(): void {
onConsent().then((consentState) => {
return ophan.record(getOphanConsentDetails(consentState));
}).catch((error) => {
console.error(error);
});

const getOphanConsentDetails = (consentState: ConsentState): {
consentJurisdiction: 'TCF' | 'USNAT' | 'AUS' | 'OTHER';
consentUUID: string;
consent: string;
} => {
if (consentState.tcfv2) {
return {
consentJurisdiction: 'TCF',
consentUUID: getCookie({ name: 'consentUUID' }) ?? '',
consent: consentState.tcfv2.tcString,
};
}
if (consentState.usnat) {
// Users who interacted with the CCPA banner before the migration to usnat will still have a ccpaUUID cookie. The usnatUUID cookie is set when the USNAT banner is interacted with. We need to check both cookies to ensure we have the correct consentUUID.
const consentUUID =
getCookie({ name: 'usnatUUID' }) ??
getCookie({ name: 'ccpaUUID' });
return {
consentJurisdiction: 'USNAT',
consentUUID: consentUUID ?? '',
consent: consentState.usnat.doNotSell ? 'false' : 'true',
};
}
if (consentState.aus) {
return {
consentJurisdiction: 'AUS',
consentUUID: getCookie({ name: 'ccpaUUID' }) ?? '',
consent: consentState.aus.personalisedAdvertising
? 'true'
: 'false',
};
}
return {
consentJurisdiction: 'OTHER',
consentUUID: '',
consent: '',
};
};
}

// ----- Helpers ----- //

function shouldInitCmp(): boolean {
Expand All @@ -70,4 +118,4 @@ function shouldInitCmp(): boolean {

// ----- Exports ----- //

export { analyticsInitialisation, consentInitialisation };
export { analyticsInitialisation, consentInitialisation, sendConsentToOphan };
2 changes: 2 additions & 0 deletions support-frontend/assets/helpers/page/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { CountryGroupId } from 'helpers/internationalisation/countryGroup';
import {
analyticsInitialisation,
consentInitialisation,
sendConsentToOphan,
} from 'helpers/page/analyticsAndConsent';
import { getReferrerAcquisitionData } from 'helpers/tracking/acquisitions';
import { getSettings } from '../globalsAndSwitches/globals';
Expand All @@ -33,6 +34,7 @@ function setUpTrackingAndConsents(): void {
};
const acquisitionData = getReferrerAcquisitionData();
void consentInitialisation(countryId);
sendConsentToOphan();
analyticsInitialisation(participationsWithAmountsTest, acquisitionData);
}

Expand Down

0 comments on commit 72ba889

Please sign in to comment.