Skip to content

Commit

Permalink
Merge 041c996 into 2316ce8
Browse files Browse the repository at this point in the history
  • Loading branch information
emma-imber authored Nov 25, 2024
2 parents 2316ce8 + 041c996 commit b7edfea
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 90 deletions.
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,10 +103,10 @@
"webpack-merge": "^6.0.1"
},
"dependencies": {
"@guardian/prebid.js": "8.52.0-8",
"@octokit/core": "^6.1.2",
"fastdom": "^1.0.11",
"lodash-es": "^4.17.21",
"prebid.js": "guardian/prebid.js#6354bd62cdc350a34c77427f2e207c464b4cac51",
"process": "^0.11.10",
"tslib": "^2.6.2",
"web-vitals": "^4.2.1"
Expand Down
171 changes: 87 additions & 84 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" */
`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('prebid.js/build/dist/all/prebid');
};

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

0 comments on commit b7edfea

Please sign in to comment.