Skip to content

Commit

Permalink
include switches in shouldInclude checks and use in prebid config
Browse files Browse the repository at this point in the history
  • Loading branch information
cemms1 committed Feb 24, 2025
1 parent 9d0a158 commit 36ee5cf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 56 deletions.
17 changes: 14 additions & 3 deletions src/lib/header-bidding/prebid/bid-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ const containsLeaderboardOrBillboard =
const containsMobileSticky = containsMobileSticky_ as jest.Mock;
const containsMpu = containsMpu_ as jest.Mock;
const containsMpuOrDmpu = containsMpuOrDmpu_ as jest.Mock;

// Non TCF bidders
const shouldIncludeTrustX = shouldIncludeTrustX_ as jest.Mock;
const shouldIncludeTripleLift = shouldIncludeTripleLift_ as jest.Mock;
// TCF bidders
const shouldIncludeAppNexus = shouldIncludeAppNexus_ as jest.Mock;
const shouldIncludeOpenx = shouldIncludeOpenx_ as jest.Mock;
const shouldIncludeTrustX = shouldIncludeTrustX_ as jest.Mock;
const shouldIncludeXaxis = shouldIncludeXaxis_ as jest.Mock;
const shouldIncludeTripleLift = shouldIncludeTripleLift_ as jest.Mock;
const shouldIncludeCriteo = shouldIncludeCriteo_ as jest.Mock;
const shouldIncludeAdYouLike = shouldIncludeAdYouLike_ as jest.Mock;
const shouldIncludeIndexExchange = shouldIncludeIndexExchange_ as jest.Mock;
Expand Down Expand Up @@ -454,7 +457,15 @@ describe('triplelift adapter', () => {
});

test('should include triplelift adapter if condition is true ', () => {
expect(getBidders()).toMatchObject(['ix', 'triplelift']);
shouldIncludeIndexExchange.mockReturnValue(true);
shouldIncludeCriteo.mockReturnValue(true);
shouldIncludeAdYouLike.mockReturnValue(true);
expect(getBidders()).toEqual([
'ix',
'criteo',
'triplelift',
'adyoulike',
]);
});

test('should return correct triplelift adapter params for leaderboard, with requests from US or Canada', () => {
Expand Down
22 changes: 8 additions & 14 deletions src/lib/header-bidding/prebid/bid-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ import {
shouldIncludeKargo,
shouldIncludeMagnite,
shouldIncludeOpenx,
shouldIncludeOzone,
shouldIncludePubmatic,
shouldIncludeTheTradeDesk,
shouldIncludeTripleLift,
shouldIncludeTrustX,
shouldIncludeXaxis,
shouldUseOzoneAdaptor,
stripDfpAdPrefixFrom,
stripMobileSuffix,
} from '../utils';
Expand Down Expand Up @@ -234,7 +234,7 @@ const appNexusBidder: (pageTargeting: PageTargeting) => PrebidBidder = (
),
});

const openxClientSideBidder: (pageTargeting: PageTargeting) => PrebidBidder = (
const openxBidder: (pageTargeting: PageTargeting) => PrebidBidder = (
pageTargeting: PageTargeting,
) => ({
name: 'oxd',
Expand Down Expand Up @@ -299,7 +299,7 @@ const getOzonePlacementId = (sizes: HeaderBiddingSize[]) => {
return '0420420500';
};

const ozoneClientSideBidder: (pageTargeting: PageTargeting) => PrebidBidder = (
const ozoneBidder: (pageTargeting: PageTargeting) => PrebidBidder = (
pageTargeting: PageTargeting,
) => ({
name: 'ozone',
Expand Down Expand Up @@ -648,21 +648,15 @@ const currentBidders = (
): PrebidBidder[] => {
const biddersToCheck: Array<[boolean, PrebidBidder]> = [
[shouldIncludeCriteo(consentState), criteoBidder(slotSizes)],
[shouldIncludeTrustX(), trustXBidder],
[shouldIncludeTripleLift(), tripleLiftBidder],
[shouldIncludeTrustX(), trustXBidder], // Non-TCF (no consentState check)
[shouldIncludeTripleLift(), tripleLiftBidder], // Non-TCF (no consentState check)
[shouldIncludeAppNexus(consentState), appNexusBidder(pageTargeting)],
[shouldIncludeXaxis(consentState), xaxisBidder],
[shouldIncludePubmatic(consentState), pubmaticBidder(slotSizes)],
[shouldIncludeAdYouLike(consentState), adYouLikeBidder],
[
shouldUseOzoneAdaptor(consentState),
ozoneClientSideBidder(pageTargeting),
],
[
shouldIncludeOpenx(consentState),
openxClientSideBidder(pageTargeting),
],
[shouldIncludeKargo(), kargoBidder],
[shouldIncludeOzone(consentState), ozoneBidder(pageTargeting)],
[shouldIncludeOpenx(consentState), openxBidder(pageTargeting)],
[shouldIncludeKargo(), kargoBidder], // Non-TCF (no consentState check)
[shouldIncludeMagnite(consentState), magniteBidder],
[shouldIncludeTheTradeDesk(consentState), theTradeDeskBidder(gpid)],
];
Expand Down
50 changes: 23 additions & 27 deletions src/lib/header-bidding/prebid/prebid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ConsentState } from '@guardian/libs';
import { getConsentFor, isString, log, onConsent } from '@guardian/libs';
import { isString, log, onConsent } from '@guardian/libs';
import { flatten } from 'lodash-es';
import type { Advert } from '../../../define/Advert';
import { getParticipations, isUserInVariant } from '../../../experiments/ab';
Expand All @@ -21,7 +21,16 @@ import type {
SlotFlatMap,
} from '../prebid-types';
import { getHeaderBiddingAdSlots } from '../slot-config';
import { stripDfpAdPrefixFrom } from '../utils';
import {
shouldIncludeCriteo,
shouldIncludeIndexExchange,
shouldIncludeKargo,
shouldIncludeMagnite,
shouldIncludeOzone,
shouldIncludePermutive,
shouldIncludeXaxis,
stripDfpAdPrefixFrom,
} from '../utils';
import { bids } from './bid-config';
import type { PrebidPriceGranularity } from './price-config';
import {
Expand Down Expand Up @@ -275,6 +284,10 @@ declare global {
}

const shouldEnableAnalytics = (): boolean => {
if (!window.guardian.config.switches.prebidAnalytics) {
return false;
}

const analyticsSampleRate = 10 / 100;
const isInSample = Math.random() < analyticsSampleRate;

Expand Down Expand Up @@ -392,12 +405,7 @@ const initialise = (window: Window, consentState: ConsentState): void => {
pbjsConfig.consentManagement = consentManagement();
}

if (
window.guardian.config.switches.permutive &&
// this switch specifically controls whether or not the Permutive Audience Connector can run with Prebid
window.guardian.config.switches.prebidPermutiveAudience &&
getConsentFor('permutive', consentState)
) {
if (shouldIncludePermutive(consentState)) {
pbjsConfig.realTimeData = {
dataProviders: [
{
Expand All @@ -419,10 +427,7 @@ const initialise = (window: Window, consentState: ConsentState): void => {
};
}

if (
window.guardian.config.switches.prebidCriteo &&
getConsentFor('criteo', consentState)
) {
if (shouldIncludeCriteo(consentState)) {
window.pbjs.bidderSettings.criteo = {
storageAllowed: true,
};
Expand All @@ -437,10 +442,7 @@ const initialise = (window: Window, consentState: ConsentState): void => {
});
}

if (
window.guardian.config.switches.prebidOzone &&
getConsentFor('ozone', consentState)
) {
if (shouldIncludeOzone(consentState)) {
// Use a custom price granularity, which is based upon the size of the slot being auctioned
window.pbjs.setBidderConfig({
bidders: ['ozone'],
Expand All @@ -462,10 +464,7 @@ const initialise = (window: Window, consentState: ConsentState): void => {
});
}

if (
window.guardian.config.switches.prebidIndexExchange &&
getConsentFor('indexExchange', consentState)
) {
if (shouldIncludeIndexExchange(consentState)) {
window.pbjs.setBidderConfig({
bidders: ['ix'],
config: {
Expand All @@ -486,10 +485,7 @@ const initialise = (window: Window, consentState: ConsentState): void => {
});
}

if (
window.guardian.config.switches.prebidAnalytics &&
shouldEnableAnalytics()
) {
if (shouldEnableAnalytics()) {
window.pbjs.enableAnalytics([
{
provider: 'gu',
Expand All @@ -503,7 +499,7 @@ const initialise = (window: Window, consentState: ConsentState): void => {
]);
}

if (window.guardian.config.switches.prebidXaxis) {
if (shouldIncludeXaxis(consentState)) {
window.pbjs.bidderSettings.xhb = {
adserverTargeting: [
{
Expand All @@ -520,13 +516,13 @@ const initialise = (window: Window, consentState: ConsentState): void => {
};
}

if (window.guardian.config.switches.prebidKargo) {
if (shouldIncludeKargo()) {
window.pbjs.bidderSettings.kargo = {
storageAllowed: true,
};
}

if (window.guardian.config.switches.prebidMagnite) {
if (shouldIncludeMagnite(consentState)) {
window.pbjs.bidderSettings.magnite = {
storageAllowed: true,
};
Expand Down
47 changes: 35 additions & 12 deletions src/lib/header-bidding/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,23 @@ export const getRandomIntInclusive = (
};

export const shouldIncludeOpenx = (consentState: ConsentState): boolean =>
getConsentFor('openX', consentState) && !isInUsOrCa();
!!window.guardian.config.switches.prebidOpenx &&
getConsentFor('openX', consentState) &&
!isInUsOrCa();

export const shouldIncludeTrustX = (): boolean => isInUsOrCa();
export const shouldIncludeTrustX = (): boolean =>
!!window.guardian.config.switches.prebidTrustx && isInUsOrCa();

export const shouldIncludeTripleLift = (): boolean =>
isInUsOrCa() || isInAuOrNz();
!!window.guardian.config.switches.prebidTriplelift &&
(isInUsOrCa() || isInAuOrNz());

// TODO: Check is we want regional restrictions on where we load the ozoneBidAdapter
export const shouldUseOzoneAdaptor = (consentState: ConsentState): boolean =>
export const shouldIncludeOzone = (consentState: ConsentState): boolean =>
!!window.guardian.config.switches.prebidOzone &&
getConsentFor('ozone', consentState) &&
!isInCanada() &&
!isInAuOrNz() &&
(window.guardian.config.switches.prebidOzone ?? false);
!isInAuOrNz();

export const shouldIncludeAppNexus = (consentState: ConsentState): boolean =>
isInAuOrNz() ||
Expand All @@ -179,27 +183,46 @@ export const shouldIncludeAppNexus = (consentState: ConsentState): boolean =>
!!pbTestNameMap().and;

export const shouldIncludeXaxis = (consentState: ConsentState): boolean =>
getConsentFor('xandr', consentState) && isInUk();
!!window.guardian.config.switches.prebidXaxis &&
getConsentFor('xandr', consentState) &&
isInUk();

export const shouldIncludeKargo = (): boolean => isInUsa();
export const shouldIncludeKargo = (): boolean =>
!!window.guardian.config.switches.prebidKargo && isInUsa();

export const shouldIncludeMagnite = (consentState: ConsentState): boolean =>
getConsentFor('magnite', consentState) &&
!!window.guardian.config.switches.prebidMagnite;
!!window.guardian.config.switches.prebidMagnite &&
getConsentFor('magnite', consentState);

export const shouldIncludeCriteo = (consentState: ConsentState): boolean =>
!!window.guardian.config.switches.prebidCriteo &&
getConsentFor('criteo', consentState);

export const shouldIncludePubmatic = (consentState: ConsentState): boolean =>
!!window.guardian.config.switches.prebidPubmatic &&
getConsentFor('pubmatic', consentState);

export const shouldIncludeAdYouLike = (consentState: ConsentState): boolean =>
!!window.guardian.config.switches.prebidAdYouLike &&
getConsentFor('adYouLike', consentState);

export const shouldIncludeTheTradeDesk = (
consentState: ConsentState,
): boolean => getConsentFor('theTradeDesk', consentState);
): boolean =>
!!window.guardian.config.switches.prebidTheTradeDesk &&
getConsentFor('theTradeDesk', consentState);

export const shouldIncludeIndexExchange = (
consentState: ConsentState,
): boolean => getConsentFor('indexExchange', consentState);
): boolean =>
!!window.guardian.config.switches.prebidIndexExchange &&
getConsentFor('indexExchange', consentState);

export const shouldIncludePermutive = (consentState: ConsentState): boolean =>
!!window.guardian.config.switches.permutive &&
/** this switch specifically controls whether or not the Permutive Audience Connector can run with Prebid */
!!window.guardian.config.switches.prebidPermutiveAudience &&
getConsentFor('permutive', consentState);

export const shouldIncludeMobileSticky = once(
(): boolean =>
Expand Down

0 comments on commit 36ee5cf

Please sign in to comment.