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

Prebid 8: support sizeMapping for video; extract it into a module #9893

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 46 additions & 32 deletions src/sizeMapping.js → modules/sizeMapping.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { config } from './config.js';
import {logWarn, logInfo, isPlainObject, deepAccess, deepClone, getWindowTop} from './utils.js';
import {includes} from './polyfill.js';
import {BANNER} from './mediaTypes.js';
import {config} from '../src/config.js';
import {deepAccess, deepClone, deepSetValue, getWindowTop, logInfo, logWarn} from '../src/utils.js';
import {includes} from '../src/polyfill.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {setupAdUnitMediaTypes} from '../src/adapterManager.js';

let installed = false;
let sizeConfig = [];

/**
Expand All @@ -22,6 +24,10 @@ let sizeConfig = [];
*/
export function setSizeConfig(config) {
sizeConfig = config;
if (!installed) {
setupAdUnitMediaTypes.before((next, adUnit, labels) => next(processAdUnitsForLabels(adUnit, labels), labels));
installed = true;
}
}
config.getConfig('sizeConfig', config => setSizeConfig(config.sizeConfig));

Expand Down Expand Up @@ -52,6 +58,13 @@ export function sizeSupported(size, configs = sizeConfig) {
return !!maps.sizesSupported[size];
}

const SIZE_PROPS = {
[BANNER]: 'banner.sizes'
}
if (FEATURES.VIDEO) {
SIZE_PROPS[VIDEO] = 'video.playerSize'
}

/**
* Resolves the unique set of the union of all sizes and labels that are active from a SizeConfig.mediaQuery match
* @param {Array<string>} labels Labels specified on adUnit or bidder
Expand All @@ -62,33 +75,39 @@ export function sizeSupported(size, configs = sizeConfig) {
* @param {Array<SizeConfig>} configs
* @returns {{labels: Array<string>, sizes: Array<Array<number>>}}
*/
export function resolveStatus({labels = [], labelAll = false, activeLabels = []} = {}, mediaTypes, sizes, configs = sizeConfig) {
export function resolveStatus({labels = [], labelAll = false, activeLabels = []} = {}, mediaTypes, configs = sizeConfig) {
let maps = evaluateSizeConfig(configs);

if (!isPlainObject(mediaTypes)) {
// add support for deprecated adUnit.sizes by creating correct banner mediaTypes if they don't already exist
if (sizes) {
mediaTypes = {
banner: {
sizes
let filtered = false;
let hasSize = false;
const filterResults = {before: {}, after: {}};

if (maps.shouldFilter) {
Object.entries(SIZE_PROPS).forEach(([mediaType, sizeProp]) => {
const oldSizes = deepAccess(mediaTypes, sizeProp);
if (oldSizes) {
if (!filtered) {
mediaTypes = deepClone(mediaTypes);
filtered = true;
}
};
} else {
mediaTypes = {};
}
}

let oldSizes = deepAccess(mediaTypes, 'banner.sizes');
if (maps.shouldFilter && oldSizes) {
mediaTypes = deepClone(mediaTypes);
mediaTypes.banner.sizes = oldSizes.filter(size => maps.sizesSupported[size]);
const newSizes = oldSizes.filter(size => maps.sizesSupported[size]);
deepSetValue(mediaTypes, sizeProp, newSizes);
hasSize = hasSize || newSizes.length > 0;
if (oldSizes.length !== newSizes.length) {
filterResults.before[mediaType] = oldSizes;
filterResults.after[mediaType] = newSizes
}
}
})
} else {
hasSize = Object.values(SIZE_PROPS).find(prop => deepAccess(mediaTypes, prop)?.length) != null
}

let results = {
active: (
!mediaTypes.hasOwnProperty(BANNER)
!Object.keys(SIZE_PROPS).find(mediaType => mediaTypes.hasOwnProperty(mediaType))
) || (
deepAccess(mediaTypes, 'banner.sizes.length') > 0 && (
hasSize && (
labels.length === 0 || (
(!labelAll && (
labels.some(label => maps.labels[label]) ||
Expand All @@ -105,13 +124,9 @@ export function resolveStatus({labels = [], labelAll = false, activeLabels = []}
mediaTypes
};

if (oldSizes && oldSizes.length !== mediaTypes.banner.sizes.length) {
results.filterResults = {
before: oldSizes,
after: mediaTypes.banner.sizes
};
if (Object.keys(filterResults.before).length > 0) {
results.filterResults = filterResults;
}

return results;
}

Expand Down Expand Up @@ -162,14 +177,13 @@ export function processAdUnitsForLabels(adUnits, activeLabels) {
} = resolveStatus(
getLabels(adUnit, activeLabels),
adUnit.mediaTypes,
adUnit.sizes
);

if (!active) {
logInfo(`Size mapping disabled adUnit "${adUnit.code}"`);
} else {
if (filterResults) {
logInfo(`Size mapping filtered adUnit "${adUnit.code}" banner sizes from `, filterResults.before, 'to ', filterResults.after);
logInfo(`Size mapping filtered adUnit "${adUnit.code}" sizes from `, filterResults.before, 'to ', filterResults.after);
}

adUnit.mediaTypes = mediaTypes;
Expand All @@ -185,7 +199,7 @@ export function processAdUnitsForLabels(adUnits, activeLabels) {
logInfo(`Size mapping deactivated adUnit "${adUnit.code}" bidder "${bid.bidder}"`);
} else {
if (filterResults) {
logInfo(`Size mapping filtered adUnit "${adUnit.code}" bidder "${bid.bidder}" banner sizes from `, filterResults.before, 'to ', filterResults.after);
logInfo(`Size mapping filtered adUnit "${adUnit.code}" bidder "${bid.bidder}" sizes from `, filterResults.before, 'to ', filterResults.after);
bid.mediaTypes = mediaTypes;
}
bids.push(bid);
Expand Down
10 changes: 7 additions & 3 deletions src/adapterManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getUniqueIdentifierStr,
getUserConfiguredParams,
groupBy,
isArray,
isArray, isPlainObject,
isValidMediaTypes,
logError,
logInfo,
Expand All @@ -22,7 +22,6 @@ import {
shuffle,
timestamp,
} from './utils.js';
import {processAdUnitsForLabels} from './sizeMapping.js';
import {decorateAdUnitsWithNativeParams, nativeAdapters} from './native.js';
import {newBidder} from './adapters/bidderFactory.js';
import {ajaxBuilder} from './ajax.js';
Expand Down Expand Up @@ -193,7 +192,7 @@ export let coppaDataHandler = {
* they should be exposed under `adUnit.bids[].mediaTypes`.
*/
export const setupAdUnitMediaTypes = hook('sync', (adUnits, labels) => {
return processAdUnitsForLabels(adUnits, labels);
return adUnits;
}, 'setupAdUnitMediaTypes')

/**
Expand Down Expand Up @@ -237,6 +236,11 @@ adapterManager.makeBidRequests = hook('sync', function (adUnits, auctionStart, a
if (FEATURES.NATIVE) {
decorateAdUnitsWithNativeParams(adUnits);
}
adUnits.forEach(au => {
if (!isPlainObject(au.mediaTypes)) {
au.mediaTypes = {};
}
})
adUnits = setupAdUnitMediaTypes(adUnits, labels);

let {[PARTITIONS.CLIENT]: clientBidders, [PARTITIONS.SERVER]: serverBidders} = partitionBidders(adUnits, _s2sConfigs);
Expand Down
Loading