Skip to content

Commit

Permalink
Merge 93b2611 into f25fb1f
Browse files Browse the repository at this point in the history
  • Loading branch information
akinsola-guardian authored Jan 17, 2025
2 parents f25fb1f + 93b2611 commit 27384c4
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-trees-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/libs': minor
---

Test for Consent or Pay
74 changes: 70 additions & 4 deletions apps/github-pages/src/components/CmpTest.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import { cmp, onConsentChange, log } from '@guardian/libs';
import { onMount } from 'svelte';
let subscriber = window.location.search.includes('subscriber');
let isFeatureFlagEnabled = window.location.search.includes('CMP_COP');
let useNonAdvertisedList = window.location.search.includes('CMP_MAIN');
let isUserSignedIn = window.location.search.includes('CMP_SIGNED_IN');
// localStorage.setItem('subscribed', window.location.search.includes('subscribed'));
switch (window.location.hash) {
case '#tcfv2':
localStorage.setItem('framework', JSON.stringify('tcfv2'));
Expand Down Expand Up @@ -37,6 +44,13 @@
log('cmp', event);
}
let rejectAllFunc = () => {
cmp.rejectAll().then(() => {
logEvent({ title: 'rejectAll'});
// window.location.reload();
});
};
let clearPreferences = () => {
// clear local storage
// https://documentation.sourcepoint.com/web-implementation/general/cookies-and-local-storage#cmp-local-storage
Expand All @@ -60,6 +74,33 @@
clearPreferences();
};
const toggleQueryParams = (param) => {
let queryParams = new URLSearchParams(window.location.search);
queryParams.has(param) ? queryParams.delete(param) : queryParams.append(param, '');
window.location.search = queryParams.toString();
};
const toggleSubscriber = () => {
subscriber = !subscriber;
toggleQueryParams('subscriber');
// localStorage.setItem('subscriber', JSON.stringify(subscriber));
};
const toggleIsFeatureFlagEnabled = () => {
isFeatureFlagEnabled = !isFeatureFlagEnabled;
toggleQueryParams('CMP_COP');
};
const toggleIsUserSignedIn = () => {
isUserSignedIn = !isUserSignedIn;
toggleQueryParams('CMP_SIGNED_IN');
};
const toggleUseNonAdvertisedList = () => {
useNonAdvertisedList = !useNonAdvertisedList;
toggleQueryParams('CMP_MAIN');
};
$: consentState = {};
$: eventsList = [];
Expand Down Expand Up @@ -91,10 +132,7 @@
}
// do this loads to make sure that doesn't break things
cmp.init({ country });
cmp.init({ country });
cmp.init({ country });
cmp.init({ country });
cmp.init({ country, subscriber: subscriber ?? false, isUserSignedIn: isUserSignedIn ?? false, useNonAdvertisedList: useNonAdvertisedList ?? false });
});
</script>
Expand All @@ -104,6 +142,7 @@
>open privacy manager</button
>
<button on:click={clearPreferences}>clear preferences</button>
<button on:click={rejectAllFunc}>rejectAll</button>
<label class={framework == 'tcfv2' ? 'selected' : 'none'}>
<input
type="radio"
Expand Down Expand Up @@ -133,6 +172,33 @@
in Australia:
<strong>CCPA-like</strong>
</label>
<label class={useNonAdvertisedList ? 'selected' : 'none'}>
<input
type="checkbox"
on:change={toggleUseNonAdvertisedList}
checked={useNonAdvertisedList}
/>
<strong>useNonAdvertisedList?</strong>
</label>
<!-- <label class={subscriber ? 'selected' : 'none'}>
<input
type="checkbox"
on:change={toggleSubscriber}
checked={subscriber}
/>
<strong>Subscriber</strong>
</label> -->
<label class={isUserSignedIn ? 'selected' : 'none'}>
<input
type="checkbox"
on:change={toggleIsUserSignedIn}
checked={isUserSignedIn}
/>
<strong>isUserSignedIn?</strong>
</label>
</nav>
<div id="consent-state">
Expand Down
19 changes: 17 additions & 2 deletions libs/@guardian/libs/src/consent-management-platform/cmp.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CountryCode } from '../index.test';
import { getCurrentFramework } from './getCurrentFramework';
import { mark } from './lib/mark';
import {
Expand All @@ -16,9 +17,23 @@ import type {
WillShowPrivacyMessage,
} from './types';

const init = (framework: ConsentFramework, pubData?: PubData): void => {
const init = (
framework: ConsentFramework,
countryCode: CountryCode,
subscriber: boolean,
isUserSignedIn: boolean,
useNonAdvertisedList: boolean,
pubData?: PubData,
): void => {
mark('cmp-init');
initSourcepoint(framework, pubData);
initSourcepoint(
framework,
countryCode,
subscriber,
isUserSignedIn,
useNonAdvertisedList,
pubData,
);
};

const willShowPrivacyMessage: WillShowPrivacyMessage = () =>
Expand Down
23 changes: 12 additions & 11 deletions libs/@guardian/libs/src/consent-management-platform/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('cmp.init', () => {
it('does nothing if CMP is disabled', () => {
disable();

cmp.init({ country: 'GB' });
cmp.init({ country: 'US' });
cmp.init({ country: 'GB', subscriber: true });
cmp.init({ country: 'US', subscriber: true });

expect(CMP.init).not.toHaveBeenCalled();

Expand All @@ -33,33 +33,33 @@ describe('cmp.init', () => {

it('requires country to be set', () => {
expect(() => {
cmp.init({ pubData: {} });
cmp.init({ subscriber: true, pubData: {} });
}).toThrow('required');
});

it('initializes CMP when in the US', () => {
cmp.init({ country: 'US' });
cmp.init({ country: 'US', subscriber: true });
expect(CMP.init).toHaveBeenCalledTimes(1);
});

it('initializes CMP when in Australia', () => {
cmp.init({ country: 'AU' });
cmp.init({ country: 'AU', subscriber: true });
expect(CMP.init).toHaveBeenCalledTimes(1);
});

it('initializes TCF when neither in the US or Australia', () => {
cmp.init({ country: 'GB' });
cmp.init({ country: 'GB', subscriber: true });
expect(CMP.init).toHaveBeenCalledTimes(1);
});
});

// *************** START commercial.dcr.js hotfix ***************
describe('hotfix cmp.init', () => {
it('only initialises once per page', () => {
cmp.init({ country: 'GB' });
cmp.init({ country: 'GB' });
cmp.init({ country: 'GB' });
cmp.init({ country: 'GB' });
cmp.init({ country: 'GB', subscriber: true });
cmp.init({ country: 'GB', subscriber: true });
cmp.init({ country: 'GB', subscriber: true });
cmp.init({ country: 'GB', subscriber: true });
expect(CMP.init).toHaveBeenCalledTimes(1);
expect(window.guCmpHotFix.initialised).toBe(true);
});
Expand All @@ -68,7 +68,7 @@ describe('hotfix cmp.init', () => {
const consoleWarn = jest
.spyOn(global.console, 'warn')
.mockImplementation(() => undefined);
cmp.init({ country: 'GB' });
cmp.init({ country: 'GB', subscriber: true });
const currentVersion = window.guCmpHotFix.cmp?.version;
const mockedVersion = 'X.X.X-mock';

Expand Down Expand Up @@ -106,6 +106,7 @@ describe('hotfix cmp.init', () => {
willShowPrivacyMessage: () => new Promise(() => true),
willShowPrivacyMessageSync: () => true,
hasInitialised: () => true,
rejectAll: () => new Promise(() => undefined),
showPrivacyManager: () => {
console.warn('This is a dummy for showPrivacyManager');
},
Expand Down
23 changes: 21 additions & 2 deletions libs/@guardian/libs/src/consent-management-platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getConsentFor as clientGetConsentFor } from './getConsentFor';
import { getFramework } from './getFramework';
import { onConsent as clientOnConsent } from './onConsent';
import { onConsentChange as clientOnConsentChange } from './onConsentChange';
import { rejectAll as clientRejectAll } from './rejectAll';
import {
isServerSide,
cmp as serverCmp,
Expand Down Expand Up @@ -33,7 +34,13 @@ const initialised = new Promise((resolve) => {
resolveInitialised = resolve;
});

const init: InitCMP = ({ pubData, country }) => {
const init: InitCMP = ({
pubData,
country,
subscriber = false,
isUserSignedIn = false,
useNonAdvertisedList = false,
}) => {
if (isDisabled() || isServerSide) {
return;
}
Expand Down Expand Up @@ -61,7 +68,14 @@ const init: InitCMP = ({ pubData, country }) => {

const framework = getFramework(country);

UnifiedCMP.init(framework, pubData ?? {});
UnifiedCMP.init(
framework,
country,
subscriber,
isUserSignedIn,
useNonAdvertisedList,
pubData ?? {},
);

void UnifiedCMP.willShowPrivacyMessage().then((willShowValue) => {
_willShowPrivacyMessage = willShowValue;
Expand Down Expand Up @@ -92,6 +106,10 @@ const showPrivacyManager = () => {
void initialised.then(UnifiedCMP.showPrivacyManager);
};

const rejectAll = isServerSide
? clientRejectAll
: (window.guCmpHotFix.rejectAll ??= clientRejectAll);

export const cmp: CMP = isServerSide
? serverCmp
: (window.guCmpHotFix.cmp ??= {
Expand All @@ -100,6 +118,7 @@ export const cmp: CMP = isServerSide
willShowPrivacyMessageSync,
hasInitialised,
showPrivacyManager,
rejectAll,
version: version,

// special helper methods for disabling CMP
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { CountryCode } from '../index.test';

export const isConsentOrPay = (countryCode: CountryCode) => {
const consentOrPayCountries = ['GB'];

return consentOrPayCountries.includes(countryCode);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isObject } from '../../isObject/isObject';
import { isString } from '../../isString/isString';
import { storage } from '../../storage/storage';

export type Participations = Record<string, { variant: string }>;

const participationsKey = 'gu.ab.participations';
export const getParticipationsFromLocalStorage = (): Participations => {
const participations = storage.local.get(participationsKey);
return isParticipations(participations) ? participations : {};
};

const isParticipations = (
participations: unknown,
): participations is Participations => {
return (
isObject(participations) &&
Object.values(participations).every(
(participation) =>
isObject(participation) && isString(participation.variant),
)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { isGuardianDomain } from './domain';

export const ACCOUNT_ID = 1257;
export const PRIVACY_MANAGER_USNAT = 1068329;
export const PROPERTY_ID = 7417;
export const PROPERTY_ID_MAIN = 9398;
export const PROPERTY_ID_SUPPORT = 38161;
// export const PROPERTY_ID_MAIN = 7417;
export const PROPERTY_ID_AUSTRALIA = 13348;
export const PRIVACY_MANAGER_TCFV2 = 106842;
export const PRIVACY_MANAGER_AUSTRALIA = 1178486;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
};
22 changes: 22 additions & 0 deletions libs/@guardian/libs/src/consent-management-platform/rejectAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getCurrentFramework } from './getCurrentFramework';
import { invokeCallbacks } from './onConsentChange';
import { rejectAllForUser } from './tcfv2/api';

const rejectAll = (): Promise<void> =>
// Consider jurisdiction/framework and countries.
new Promise<void>((resolve, reject) => {
console.log('Rejecting all');
if (getCurrentFramework() === 'tcfv2') {
rejectAllForUser()
.then(() => {
invokeCallbacks();
resolve();
})
.catch(() => {
reject(new Error('Unable to reject all'));
});
} else {
reject(new Error('Framework not supported'));
}
});
export { rejectAll };
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const cmp: CMP = {
version: 'n/a',
willShowPrivacyMessage: serverSideWarnAndReturn(Promise.resolve(false)),
willShowPrivacyMessageSync: serverSideWarnAndReturn(false),
rejectAll: serverSideWarnAndReturn(Promise.resolve()),
};

export const onConsent = (): ReturnType<typeof OnConsent> => {
Expand Down
Loading

0 comments on commit 27384c4

Please sign in to comment.