Skip to content

Commit 3d530e5

Browse files
JakeiiGHaberiscemms1George Haberis
authored
Add id5 prebid integration behind test (#1970)
Co-authored-by: GHaberis <george.haberis@guardian.co.uk> Co-authored-by: Charlotte Emms <43961396+cemms1@users.noreply.github.com> Co-authored-by: George Haberis <george_haberis@36869_mac.gnm.int>
1 parent f38a8ee commit 3d530e5

5 files changed

Lines changed: 114 additions & 14 deletions

File tree

src/experiments/ab-tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ABTest } from '@guardian/ab-core';
2+
import { prebidId5 } from './tests/prebid-id5';
23

34
/**
45
* You only need to add tests to this file if the code you are testing is here in
@@ -7,4 +8,5 @@ import type { ABTest } from '@guardian/ab-core';
78
*/
89
export const concurrentTests: ABTest[] = [
910
// one test per line
11+
prebidId5,
1012
];
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { ABTest } from '@guardian/ab-core';
2+
3+
export const prebidId5: ABTest = {
4+
id: 'PrebidId5',
5+
author: '@commercial-dev',
6+
start: '2025-05-07',
7+
expiry: '2025-05-30',
8+
audience: 1 / 100,
9+
audienceOffset: 0 / 100,
10+
audienceCriteria: '',
11+
successMeasure: '',
12+
description: 'Test enabling the ID5 module in prebid.js',
13+
variants: [
14+
{
15+
id: 'control',
16+
test: (): void => {
17+
/* no-op */
18+
},
19+
},
20+
{
21+
id: 'variant',
22+
test: (): void => {
23+
/* no-op */
24+
},
25+
},
26+
],
27+
canRun: () => true,
28+
};

src/lib/header-bidding/prebid/pbjs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import 'prebid.js/modules/kargoBidAdapter';
1616
import 'prebid.js/modules/rubiconBidAdapter';
1717
import 'prebid.js/modules/ttdBidAdapter';
1818
import 'prebid.js/modules/multibid';
19+
import 'prebid.js/modules/id5IdSystem';
20+
import 'prebid.js/modules/userId';
1921

2022
// Guardian specific adapters that we have modified or created
2123
import './modules/appnexusBidAdapter';

src/lib/header-bidding/prebid/prebid.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type ConsentState } from '@guardian/libs';
2+
import { getConsentFor } from '@guardian/libs';
23
import { isUserInVariant } from '../../../experiments/ab';
34
import { pubmatic } from '../../__vendor/pubmatic';
45
import { getAdvertById as getAdvertById_ } from '../../dfp/get-advert-by-id';
@@ -36,6 +37,16 @@ jest.mock('experiments/ab', () => ({
3637
isUserInVariant: jest.fn(),
3738
}));
3839

40+
jest.mock('@guardian/libs', () => ({
41+
...jest.requireActual('@guardian/libs'),
42+
getConsentFor: jest.fn(),
43+
}));
44+
45+
const mockGetConsentForID5 = (hasConsent: boolean) =>
46+
(getConsentFor as jest.Mock).mockImplementation((vendor: string) =>
47+
vendor === 'id5' ? hasConsent : undefined,
48+
);
49+
3950
const mockConsentState = {
4051
tcfv2: {
4152
consents: { '': true },
@@ -72,6 +83,8 @@ describe('initialise', () => {
7283
jest.fn().mockReturnValue(true),
7384
);
7485
jest.mocked(isUserInVariant).mockReturnValue(false);
86+
87+
mockGetConsentForID5(true);
7588
prebid.initialise(window, mockConsentState);
7689

7790
expect(window.pbjs?.getConfig()).toEqual({
@@ -202,6 +215,39 @@ describe('initialise', () => {
202215
});
203216
});
204217

218+
test('should generate correct Prebid config when no consent for ID5', () => {
219+
jest.mocked(shouldIncludeBidder).mockReturnValue(
220+
jest.fn().mockReturnValue(true),
221+
);
222+
jest.mocked(isUserInVariant).mockReturnValue(false);
223+
224+
mockGetConsentForID5(false);
225+
prebid.initialise(window, mockConsentState);
226+
227+
expect(window.pbjs?.getConfig('userSync')).toEqual({
228+
syncDelay: 3000,
229+
syncEnabled: true,
230+
syncsPerBidder: 0,
231+
userIds: [
232+
{
233+
name: 'sharedId',
234+
storage: {
235+
type: 'cookie',
236+
name: '_pubcid',
237+
expires: 365,
238+
},
239+
},
240+
],
241+
auctionDelay: 500,
242+
filterSettings: {
243+
all: {
244+
bidders: '*',
245+
filter: 'include',
246+
},
247+
},
248+
});
249+
});
250+
205251
test('should generate correct Prebid config consent management in USNAT', () => {
206252
prebid.initialise(window, { ...mockConsentState, framework: 'usnat' });
207253
expect(window.pbjs?.getConfig('consentManagement')).toEqual({

src/lib/header-bidding/prebid/prebid.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { ConsentState } from '@guardian/libs';
2-
import { isString, log, onConsent } from '@guardian/libs';
2+
import { getConsentFor, isString, log, onConsent } from '@guardian/libs';
33
import { flatten } from 'lodash-es';
44
import type { PrebidPriceGranularity } from 'prebid.js/src/cpmBucketManager';
55
import type { Advert } from '../../../define/Advert';
6-
import { getParticipations } from '../../../experiments/ab';
6+
import { getParticipations, isUserInVariant } from '../../../experiments/ab';
7+
import { prebidId5 } from '../../../experiments/tests/prebid-id5';
78
import type { AdSize } from '../../../lib/ad-sizes';
89
import { createAdSize } from '../../../lib/ad-sizes';
910
import { PREBID_TIMEOUT } from '../../../lib/constants/prebid-timeout';
@@ -64,11 +65,12 @@ type ConsentManagement =
6465

6566
type UserId = {
6667
name: string;
67-
params?: Record<string, string>;
68+
params?: Record<string, string | number>;
6869
storage: {
69-
type: 'cookie';
70+
type: 'cookie' | 'html5';
7071
name: string;
7172
expires: number;
73+
refreshInSeconds?: number;
7274
};
7375
};
7476

@@ -347,6 +349,35 @@ const initialise = (window: Window, consentState: ConsentState): void => {
347349
}
348350
initialised = true;
349351

352+
const userIds: UserId[] = [
353+
{
354+
name: 'sharedId',
355+
storage: {
356+
type: 'cookie',
357+
name: '_pubcid',
358+
expires: 365,
359+
},
360+
},
361+
];
362+
363+
if (
364+
getConsentFor('id5', consentState) &&
365+
isUserInVariant(prebidId5, 'variant')
366+
) {
367+
userIds.push({
368+
name: 'id5Id',
369+
params: {
370+
partner: 182,
371+
},
372+
storage: {
373+
type: 'html5',
374+
name: 'id5id',
375+
expires: 90,
376+
refreshInSeconds: 7200,
377+
},
378+
});
379+
}
380+
350381
const userSync: UserSync = isSwitchedOn('prebidUserSync')
351382
? {
352383
syncsPerBidder: 0, // allow all syncs
@@ -356,16 +387,7 @@ const initialise = (window: Window, consentState: ConsentState): void => {
356387
filter: 'include',
357388
},
358389
},
359-
userIds: [
360-
{
361-
name: 'sharedId',
362-
storage: {
363-
type: 'cookie',
364-
name: '_pubcid',
365-
expires: 365,
366-
},
367-
},
368-
],
390+
userIds,
369391
}
370392
: { syncEnabled: false };
371393

0 commit comments

Comments
 (0)