Skip to content

Commit

Permalink
Size Mapping V2 supports 'Identical Ad Units' (prebid#5062)
Browse files Browse the repository at this point in the history
* add support for identical ad units and improve log messages

* slight change in log message presentation

* fix typo

* document return value type
  • Loading branch information
Fawke authored Apr 7, 2020
1 parent 977f546 commit 9de6609
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 45 deletions.
15 changes: 8 additions & 7 deletions allowedModules.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

const sharedWhiteList = [
"core-js/library/fn/array/find", // no ie11
"core-js/library/fn/array/includes", // no ie11
"core-js/library/fn/set", // ie11 supports Set but not Set#values
"core-js/library/fn/string/includes", // no ie11
"core-js/library/fn/number/is-integer", // no ie11,
"core-js/library/fn/array/from" // no ie11
'core-js/library/fn/array/find', // no ie11
'core-js/library/fn/array/includes', // no ie11
'core-js/library/fn/set', // ie11 supports Set but not Set#values
'core-js/library/fn/string/includes', // no ie11
'core-js/library/fn/number/is-integer', // no ie11,
'core-js/library/fn/array/from' // no ie11
];

module.exports = {
Expand All @@ -21,6 +21,7 @@ module.exports = {
'fun-hooks/no-eval',
'just-clone',
'dlv',
'dset'
'dset',
'deep-equal'
]
};
41 changes: 23 additions & 18 deletions modules/sizeMappingV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,21 @@ export function isLabelActivated(bidOrAdUnit, activeLabels, adUnitCode) {
let labelOperator;
const labelsFound = Object.keys(bidOrAdUnit).filter(prop => prop === 'labelAny' || prop === 'labelAll');
if (labelsFound && labelsFound.length > 1) {
utils.logWarn(`SizeMappingV2:: ${(bidOrAdUnit.code)
? (`Ad Unit: ${bidOrAdUnit.code} has multiple label operators. Using the first declared operator: ${labelsFound[0]}`)
: (`Bidder: ${bidOrAdUnit.bidder} in Ad Unit: ${adUnitCode} has multiple label operators. Using the first declared operator: ${labelsFound[0]}`)}`);
utils.logWarn(`Size Mapping V2:: ${(bidOrAdUnit.code)
? (`Ad Unit: ${bidOrAdUnit.code} => Ad unit has multiple label operators. Using the first declared operator: ${labelsFound[0]}`)
: (`Ad Unit: ${adUnitCode}, Bidder: ${bidOrAdUnit.bidder} => Bidder has multiple label operators. Using the first declared operator: ${labelsFound[0]}`)}`);
}
labelOperator = labelsFound[0];

if (labelOperator === 'labelAll' && Array.isArray(bidOrAdUnit[labelOperator])) {
if (bidOrAdUnit.labelAll.length === 0) {
utils.logWarn(`SizeMappingV2:: Ad Unit: ${bidOrAdUnit.code} has declared property labelAll with an empty array. Ad Unit is still enabled!`);
utils.logWarn(`Size Mapping V2:: Ad Unit: ${bidOrAdUnit.code} => Ad unit has declared property 'labelAll' with an empty array. Ad Unit is still enabled!`);
return true;
}
return bidOrAdUnit.labelAll.every(label => includes(activeLabels, label));
} else if (labelOperator === 'labelAny' && Array.isArray(bidOrAdUnit[labelOperator])) {
if (bidOrAdUnit.labelAny.length === 0) {
utils.logWarn(`SizeMappingV2:: Ad Unit: ${bidOrAdUnit.code} has declared property labelAny with an empty array. Ad Unit is still enabled!`);
utils.logWarn(`Size Mapping V2:: Ad Unit: ${bidOrAdUnit.code} => Ad unit has declared property 'labelAny' with an empty array. Ad Unit is still enabled!`);
return true;
}
return bidOrAdUnit.labelAny.some(label => includes(activeLabels, label));
Expand Down Expand Up @@ -430,7 +430,11 @@ export function getRelevantMediaTypesForBidder(sizeConfig, activeViewport) {
// sets sizeMappingInternalStore for a given auctionId with relevant adUnit information returned from the call to 'getFilteredMediaTypes' function
// returns adUnit details object.
export function getAdUnitDetail(auctionId, adUnit) {
const adUnitDetail = sizeMappingInternalStore.getAuctionDetail(auctionId).adUnits.filter(adUnitDetail => adUnitDetail.adUnitCode === adUnit.code);
// fetch all adUnits for an auction from the sizeMappingInternalStore
const adUnitsForAuction = sizeMappingInternalStore.getAuctionDetail(auctionId).adUnits;

// check if the adUnit exists already in the sizeMappingInterStore (check for equivalence of 'code' && 'mediaTypes' properties)
const adUnitDetail = adUnitsForAuction.filter(adUnitDetail => adUnitDetail.adUnitCode === adUnit.code && utils.deepEqual(adUnitDetail.mediaTypes, adUnit.mediaTypes));

if (adUnitDetail.length > 0) {
return adUnitDetail[0];
Expand All @@ -445,16 +449,17 @@ export function getAdUnitDetail(auctionId, adUnit) {
transformedMediaTypes
};

// set adUnitDetail in sizeMappingInternalStore against the correct 'auctionId'.
sizeMappingInternalStore.setAuctionDetail(auctionId, adUnitDetail);

// 'filteredMediaTypes' are the mediaTypes that got removed/filtered-out from adUnit.mediaTypes after sizeConfig filtration.
const filteredMediaTypes = Object.keys(mediaTypes).filter(mt => Object.keys(transformedMediaTypes).indexOf(mt) === -1)
const filteredMediaTypes = Object.keys(mediaTypes).filter(mt => Object.keys(transformedMediaTypes).indexOf(mt) === -1);

utils.logInfo(`SizeMappingV2:: AdUnit: ${adUnit.code} - Active size buckets after filtration: `, sizeBucketToSizeMap);
utils.logInfo(`Size Mapping V2:: Ad Unit: ${adUnit.code} => Active size buckets after filtration: `, sizeBucketToSizeMap);
if (filteredMediaTypes.length > 0) {
utils.logInfo(`SizeMappingV2:: AdUnit: ${adUnit.code} - mediaTypes that got filtered out: ${filteredMediaTypes}`);
utils.logInfo(`Size Mapping V2:: Ad Unit: ${adUnit.code} => Media types that got filtered out: ${filteredMediaTypes}`);
}

// set adUnitDetail in sizeMappingInternalStore against the correct 'auctionId'.
sizeMappingInternalStore.setAuctionDetail(auctionId, adUnitDetail);
return adUnitDetail;
}
}
Expand All @@ -468,7 +473,7 @@ export function getBids({ bidderCode, auctionId, bidderRequestId, adUnits, label
// check if adUnit has any active media types remaining, if not drop the adUnit from auction,
// else proceed to evaluate the bids object.
if (Object.keys(transformedMediaTypes).length === 0) {
utils.logInfo(`SizeMappingV2:: Ad Unit: ${adUnit.code} is disabled since there are no active media types after sizeConfig filtration.`);
utils.logInfo(`Size Mapping V2:: Ad Unit: ${adUnit.code} => Ad unit disabled since there are no active media types after sizeConfig filtration.`);
return result;
}
result
Expand All @@ -488,7 +493,7 @@ export function getBids({ bidderCode, auctionId, bidderRequestId, adUnits, label
if (bid.sizeConfig) {
const relevantMediaTypes = internal.getRelevantMediaTypesForBidder(bid.sizeConfig, activeViewport);
if (relevantMediaTypes.length === 0) {
utils.logError(`SizeMappingV2:: AdUnit: ${adUnit.code}, Bidder: ${bidderCode} - sizeConfig is not configured properly. This bidder won't be eligible for sizeConfig checks and will remail active.`);
utils.logError(`Size Mapping V2:: Ad Unit: ${adUnit.code}, Bidder: ${bidderCode} => 'sizeConfig' is not configured properly. This bidder won't be eligible for sizeConfig checks and will remail active.`);
bid = Object.assign({}, bid);
} else if (relevantMediaTypes[0] !== 'none') {
const bidderMediaTypes = Object
Expand All @@ -502,11 +507,11 @@ export function getBids({ bidderCode, auctionId, bidderRequestId, adUnits, label
if (Object.keys(bidderMediaTypes).length > 0) {
bid = Object.assign({}, bid, { mediaTypes: bidderMediaTypes });
} else {
utils.logInfo(`SizeMappingV2:: AdUnit: ${adUnit.code}, Bidder: ${bid.bidder} - 'relevantMediaTypes' for this bidder does not match with any of the active mediaTypes at the Ad Unit level. This bidder is disabled.`);
utils.logInfo(`Size Mapping V2:: Ad Unit: ${adUnit.code}, Bidder: ${bid.bidder} => 'relevantMediaTypes' does not match with any of the active mediaTypes at the Ad Unit level. This bidder is disabled.`);
return bids;
}
} else {
utils.logInfo(`SizeMappingV2:: AdUnit: ${adUnit.code}, Bidder: ${bid.bidder} - 'relevantMediaTypes' is set to 'none' in sizeConfig for current viewport size. This bidder is disabled.`);
utils.logInfo(`Size Mapping V2:: Ad Unit: ${adUnit.code}, Bidder: ${bid.bidder} => 'relevantMediaTypes' is set to 'none' in sizeConfig for current viewport size. This bidder is disabled.`);
return bids;
}
}
Expand All @@ -525,15 +530,15 @@ export function getBids({ bidderCode, auctionId, bidderRequestId, adUnits, label
}));
return bids;
} else {
utils.logInfo(`SizeMappingV2:: AdUnit: ${adUnit.code}, Bidder: ${bid.bidder} - Label check for this bidder has failed. This bidder is disabled.`);
utils.logInfo(`Size Mapping V2:: Ad Unit: ${adUnit.code}, Bidder: ${bid.bidder} => Label check for this bidder has failed. This bidder is disabled.`);
return bids;
}
}, []));
} else {
utils.logWarn(`SizeMappingV2:: Ad Unit: ${adUnit.code} has declared invalid mediaTypes or has not declared a mediaTypes property`);
utils.logWarn(`Size Mapping V2:: Ad Unit: ${adUnit.code} => Ad unit has declared invalid 'mediaTypes' or has not declared a 'mediaTypes' property`);
}
} else {
utils.logInfo(`SizeMappingV2:: Ad Unit: ${adUnit.code} is disabled due to failing label check.`);
utils.logInfo(`Size Mapping V2:: Ad Unit: ${adUnit.code} => Ad unit is disabled due to failing label check.`);
return result;
}
return result;
Expand Down
14 changes: 13 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
import { config } from './config.js';
import clone from 'just-clone';
import deepequal from 'deep-equal';
import find from 'core-js/library/fn/array/find.js';
import includes from 'core-js/library/fn/array/includes.js';

Expand Down Expand Up @@ -36,7 +37,8 @@ export const internal = {
logError,
logWarn,
logMessage,
logInfo
logInfo,
deepEqual
};

var uniqueRef = {};
Expand Down Expand Up @@ -1242,3 +1244,13 @@ export function compareOn(property) {
return 0;
}
}

/**
* This function compares two objects for checking their equivalence.
* @param {Object} obj1
* @param {Object} obj2
* @returns {boolean}
*/
export function deepEqual(obj1, obj2) {
return deepequal(obj1, obj2);
}
Loading

0 comments on commit 9de6609

Please sign in to comment.