Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use region specific prebid bundles #1661

Merged
merged 15 commits into from
Dec 17, 2024
5 changes: 5 additions & 0 deletions .changeset/fluffy-games-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': minor
---

Use region specific bundles for Prebid
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"webpack-merge": "^6.0.1"
},
"dependencies": {
"@guardian/prebid.js": "8.52.0-8",
"@guardian/prebid.js": "8.52.0-9",
"@octokit/core": "^6.1.2",
"fastdom": "^1.0.11",
"lodash-es": "^4.17.21",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/experiments/ab-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ABTest } from '@guardian/ab-core';
import { gpidPrebidAdUnits } from './tests/gpid-prebid';
import { mpuWhenNoEpic } from './tests/mpu-when-no-epic';
import { optOutFrequencyCap } from './tests/opt-out-frequency-cap';
import { regionSpecificPrebid } from './tests/region-specific-prebid';

/**
* You only need to add tests to this file if the code you are testing is here in
Expand All @@ -13,4 +14,5 @@ export const concurrentTests: ABTest[] = [
mpuWhenNoEpic,
optOutFrequencyCap,
gpidPrebidAdUnits,
regionSpecificPrebid,
];
29 changes: 29 additions & 0 deletions src/experiments/tests/region-specific-prebid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ABTest } from '@guardian/ab-core';

export const regionSpecificPrebid: ABTest = {
id: 'RegionSpecificPrebid',
author: '@commercial-dev',
start: '2024-11-25',
expiry: '2024-12-23',
audience: 0 / 100,
audienceOffset: 0 / 100,
audienceCriteria: '',
successMeasure: '',
description:
'Test if splitting the Prebid bundle by region improves performance.',
variants: [
{
id: 'control',
test: (): void => {
/* no-op */
},
},
{
id: 'variant',
test: (): void => {
/* no-op */
},
},
],
canRun: () => true,
};
5 changes: 4 additions & 1 deletion src/init/consented/prepare-prebid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jest.mock('utils/geo-utils', () => ({
jest.mock('experiments/ab', () => ({
isInABTestSynchronous: jest.fn().mockReturnValue(false),
isInVariantSynchronous: jest.fn().mockReturnValue(false),
isUserInVariant: jest.fn().mockReturnValue(false),
}));

jest.mock('lib/commercial-features', () => ({
Expand Down Expand Up @@ -203,7 +204,6 @@ describe('init', () => {
commercialFeatures.adFree = false;
mockOnConsent(tcfv2WithConsent);
mockGetConsentFor(true);
(isInCanada as jest.Mock).mockReturnValueOnce(false);

await setupPrebid();
expect(prebid.initialise).toBeCalled();
Expand Down Expand Up @@ -295,6 +295,7 @@ describe('init', () => {
commercialFeatures.adFree = false;
mockOnConsent(tcfv2WithConsent);
mockGetConsentFor(true);

await setupPrebid();
expect(prebid.initialise).toBeCalled();
});
Expand Down Expand Up @@ -330,6 +331,7 @@ describe('init', () => {
commercialFeatures.adFree = false;
mockOnConsent(usnatWithConsent);
mockGetConsentFor(true);

await setupPrebid();
expect(prebid.initialise).toBeCalled();
});
Expand Down Expand Up @@ -365,6 +367,7 @@ describe('init', () => {
commercialFeatures.adFree = false;
mockOnConsent(ausWithConsent);
mockGetConsentFor(true);

await setupPrebid();
expect(prebid.initialise).toBeCalled();
});
Expand Down
22 changes: 19 additions & 3 deletions src/init/consented/prepare-prebid.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { ConsentFramework } from '@guardian/libs';
import { getConsentFor, log, onConsent } from '@guardian/libs';
import { once } from 'lodash-es';
import { isUserInVariant } from '../../experiments/ab';
import { regionSpecificPrebid } from '../../experiments/tests/region-specific-prebid';
import { commercialFeatures } from '../../lib/commercial-features';
import { isGoogleProxy } from '../../lib/detect/detect-google-proxy';
import { prebid } from '../../lib/header-bidding/prebid/prebid';
import { shouldIncludeOnlyA9 } from '../../lib/header-bidding/utils';
import { isInCanada } from '../../utils/geo-utils';
import { isInAuOrNz, isInCanada, isInUk, isInUsa } from '../../utils/geo-utils';

const shouldLoadPrebid = () =>
!isGoogleProxy() &&
Expand All @@ -16,11 +18,25 @@ const shouldLoadPrebid = () =>
!shouldIncludeOnlyA9 &&
!isInCanada();

const prebidVersion = () => {
if (isUserInVariant(regionSpecificPrebid, 'variant')) {
if (isInUk()) {
return 'uk';
} else if (isInAuOrNz()) {
return 'aus-nz';
} else if (isInUsa()) {
return 'us';
}
return 'row';
}
return 'all';
};

const loadPrebid = async (framework: ConsentFramework): Promise<void> => {
if (shouldLoadPrebid()) {
await import(
// @ts-expect-error -- there’s no types for Prebid.js
/* webpackChunkName: "Prebid.js" */ '@guardian/prebid.js/build/dist/prebid'
/* webpackChunkName: "Prebid.js" */
`@guardian/prebid.js/build/dist/${prebidVersion()}/prebid`
);
prebid.initialise(window, framework);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/header-bidding/prebid/prebid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const resetPrebid = () => {
// @ts-expect-error -- there’s no types for this
delete window.pbjsChunk;
jest.resetModules();
jest.requireActual('@guardian/prebid.js/build/dist/prebid');
jest.requireActual('@guardian/prebid.js/build/dist/all/prebid');
};

describe('initialise', () => {
Expand Down
Loading