-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
updated sizeMapping to use sizeConfig and support labels #1772
Changes from 2 commits
455466a
408958f
fc323b9
2b6fcb5
79382ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** @module adaptermanger */ | ||
|
||
import { flatten, getBidderCodes, getDefinedParams, shuffle } from './utils'; | ||
import { mapSizes } from './sizeMapping'; | ||
import { resolveStatus } from './sizeMapping'; | ||
import { processNativeAdUnitParams, nativeAdapters } from './native'; | ||
import { newBidder } from './adapters/bidderFactory'; | ||
import { ajaxBuilder } from 'src/ajax'; | ||
|
@@ -19,53 +19,61 @@ let _s2sConfig = config.getConfig('s2sConfig'); | |
|
||
var _analyticsRegistry = {}; | ||
|
||
function getBids({bidderCode, auctionId, bidderRequestId, adUnits}) { | ||
return adUnits.map(adUnit => { | ||
return adUnit.bids.filter(bid => bid.bidder === bidderCode) | ||
.map(bid => { | ||
let sizes = adUnit.sizes; | ||
if (adUnit.sizeMapping) { | ||
let sizeMapping = mapSizes(adUnit); | ||
if (sizeMapping === '') { | ||
return ''; | ||
} | ||
sizes = sizeMapping; | ||
} | ||
function getBids({bidderCode, auctionId, bidderRequestId, adUnits, labels}) { | ||
function getLabels(obj, requestLabels) { | ||
if (obj.labelAll) { | ||
return {labelAll: true, labels: obj.labelAll, requestLabels}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in my experience, creating objects on the fly like this is better done more formally, at least in a function were you can use @param and also reduces chances to code to a single place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated some names and added a typedef to help. I tried to use a constructor function but my IDEs linter was not having it, claiming a type mismatch; so left as factory function. |
||
} | ||
return {labelAll: false, labels: obj.labelAny, requestLabels}; | ||
} | ||
|
||
if (adUnit.mediaTypes) { | ||
if (utils.isValidMediaTypes(adUnit.mediaTypes)) { | ||
bid = Object.assign({}, bid, { mediaTypes: adUnit.mediaTypes }); | ||
} else { | ||
utils.logError( | ||
`mediaTypes is not correctly configured for adunit ${adUnit.code}` | ||
); | ||
return adUnits.reduce((result, adUnit) => { | ||
let {active, sizes: filteredAdUnitSizes} = resolveStatus(getLabels(adUnit, labels), adUnit.sizes); | ||
|
||
if (active) { | ||
result.push(adUnit.bids.filter(bid => bid.bidder === bidderCode) | ||
.reduce((bids, bid) => { | ||
if (adUnit.mediaTypes) { | ||
if (utils.isValidMediaTypes(adUnit.mediaTypes)) { | ||
bid = Object.assign({}, bid, {mediaTypes: adUnit.mediaTypes}); | ||
} else { | ||
utils.logError( | ||
`mediaTypes is not correctly configured for adunit ${adUnit.code}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
const nativeParams = | ||
adUnit.nativeParams || utils.deepAccess(adUnit, 'mediaTypes.native'); | ||
if (nativeParams) { | ||
bid = Object.assign({}, bid, { | ||
nativeParams: processNativeAdUnitParams(nativeParams), | ||
}); | ||
} | ||
const nativeParams = | ||
adUnit.nativeParams || utils.deepAccess(adUnit, 'mediaTypes.native'); | ||
if (nativeParams) { | ||
bid = Object.assign({}, bid, { | ||
nativeParams: processNativeAdUnitParams(nativeParams), | ||
}); | ||
} | ||
|
||
bid = Object.assign({}, bid, getDefinedParams(adUnit, [ | ||
'mediaType', | ||
'renderer' | ||
])); | ||
|
||
return Object.assign({}, bid, { | ||
adUnitCode: adUnit.code, | ||
transactionId: adUnit.transactionId, | ||
sizes: sizes, | ||
bidId: bid.bid_id || utils.getUniqueIdentifierStr(), | ||
bidderRequestId, | ||
auctionId | ||
}); | ||
} | ||
bid = Object.assign({}, bid, getDefinedParams(adUnit, [ | ||
'mediaType', | ||
'renderer' | ||
])); | ||
|
||
let {active, sizes} = resolveStatus(getLabels(bid, labels), filteredAdUnitSizes); | ||
|
||
if (active) { | ||
bids.push(Object.assign({}, bid, { | ||
adUnitCode: adUnit.code, | ||
transactionId: adUnit.transactionId, | ||
sizes: sizes, | ||
bidId: bid.bid_id || utils.getUniqueIdentifierStr(), | ||
bidderRequestId, | ||
auctionId | ||
})); | ||
} | ||
return bids; | ||
}, []) | ||
); | ||
}).reduce(flatten, []).filter(val => val !== ''); | ||
} | ||
return result; | ||
}, []).reduce(flatten, []).filter(val => val !== ''); | ||
} | ||
|
||
function getAdUnitCopyForPrebidServer(adUnits) { | ||
|
@@ -94,7 +102,7 @@ function getAdUnitCopyForPrebidServer(adUnits) { | |
return adUnitsCopy; | ||
} | ||
|
||
exports.makeBidRequests = function(adUnits, auctionStart, auctionId, cbTimeout) { | ||
exports.makeBidRequests = function(adUnits, auctionStart, auctionId, cbTimeout, labels) { | ||
let bidRequests = []; | ||
let bidderCodes = getBidderCodes(adUnits); | ||
if (config.getConfig('bidderSequence') === RANDOM) { | ||
|
@@ -134,7 +142,7 @@ exports.makeBidRequests = function(adUnits, auctionStart, auctionId, cbTimeout) | |
auctionId, | ||
bidderRequestId, | ||
tid, | ||
bids: getBids({bidderCode, auctionId, bidderRequestId, 'adUnits': adUnitsS2SCopy}), | ||
bids: getBids({bidderCode, auctionId, bidderRequestId, 'adUnits': adUnitsS2SCopy, labels}), | ||
auctionStart: auctionStart, | ||
timeout: _s2sConfig.timeout, | ||
src: CONSTANTS.S2S.SRC | ||
|
@@ -165,7 +173,7 @@ exports.makeBidRequests = function(adUnits, auctionStart, auctionId, cbTimeout) | |
bidderCode, | ||
auctionId, | ||
bidderRequestId, | ||
bids: getBids({bidderCode, auctionId, bidderRequestId, adUnits}), | ||
bids: getBids({bidderCode, auctionId, bidderRequestId, adUnits, labels}), | ||
auctionStart: auctionStart, | ||
timeout: cbTimeout | ||
}; | ||
|
@@ -174,7 +182,7 @@ exports.makeBidRequests = function(adUnits, auctionStart, auctionId, cbTimeout) | |
} | ||
}); | ||
return bidRequests; | ||
} | ||
}; | ||
|
||
exports.callBids = (adUnits, bidRequests, addBidResponse, doneCb) => { | ||
let serverBidRequests = bidRequests.filter(bidRequest => { | ||
|
@@ -194,22 +202,26 @@ exports.callBids = (adUnits, bidRequests, addBidResponse, doneCb) => { | |
} | ||
} | ||
} | ||
let ajax = ajaxBuilder(bidRequests[0].timeout); | ||
bidRequests.forEach(bidRequest => { | ||
bidRequest.start = new Date().getTime(); | ||
// TODO : Do we check for bid in pool from here and skip calling adapter again ? | ||
const adapter = _bidderRegistry[bidRequest.bidderCode]; | ||
if (adapter) { | ||
utils.logMessage(`CALLING BIDDER ======= ${bidRequest.bidderCode}`); | ||
events.emit(CONSTANTS.EVENTS.BID_REQUESTED, bidRequest); | ||
bidRequest.doneCbCallCount = 0; | ||
let done = doneCb(bidRequest.bidderRequestId); | ||
adapter.callBids(bidRequest, addBidResponse, done, ajax); | ||
} else { | ||
utils.logError(`Adapter trying to be called which does not exist: ${bidRequest.bidderCode} adaptermanager.callBids`); | ||
} | ||
}); | ||
} | ||
if (bidRequests.length) { | ||
let ajax = ajaxBuilder(bidRequests[0].timeout); | ||
bidRequests.forEach(bidRequest => { | ||
bidRequest.start = new Date().getTime(); | ||
// TODO : Do we check for bid in pool from here and skip calling adapter again ? | ||
const adapter = _bidderRegistry[bidRequest.bidderCode]; | ||
if (adapter) { | ||
utils.logMessage(`CALLING BIDDER ======= ${bidRequest.bidderCode}`); | ||
events.emit(CONSTANTS.EVENTS.BID_REQUESTED, bidRequest); | ||
bidRequest.doneCbCallCount = 0; | ||
let done = doneCb(bidRequest.bidderRequestId); | ||
adapter.callBids(bidRequest, addBidResponse, done, ajax); | ||
} else { | ||
utils.logError(`Adapter trying to be called which does not exist: ${bidRequest.bidderCode} adaptermanager.callBids`); | ||
} | ||
}); | ||
} else { | ||
utils.logWarn('callBids executed with no bidRequests'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a hint in the log here that it could be because labels didn't match / didn't exist? Or even better if there was an easy code point to detect that condition (I don't see one). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the hint |
||
} | ||
}; | ||
|
||
function transformHeightWidth(adUnit) { | ||
let sizesObj = []; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,84 @@ | ||
import { config } from 'src/config'; | ||
|
||
let sizeConfig = []; | ||
|
||
/** | ||
* @module sizeMapping | ||
* @typedef {object} SizeConfig | ||
* | ||
* @property {string} [mediaQuery] A CSS media query string that will to be interpreted by window.matchMedia. If the | ||
* media query matches then the this config will be active and sizesSupported will filter bid and adUnit sizes. If | ||
* this property is not present then this SizeConfig will only be active if triggered manually by a call to | ||
* pbjs.setConfig({labels:['label']) specifying one of the labels present on this SizeConfig. | ||
* @property {Array<Array>} sizesSupported The sizes to be accepted if this SizeConfig is enabled. | ||
* @property {Array<string>} labels The active labels to match this SizeConfig to an adUnits and/or bidders. | ||
*/ | ||
import * as utils from './utils'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 for dropping all this code! |
||
let _win; | ||
|
||
function mapSizes(adUnit) { | ||
if (!isSizeMappingValid(adUnit.sizeMapping)) { | ||
return adUnit.sizes; | ||
} | ||
const width = getScreenWidth(); | ||
if (!width) { | ||
// size not detected - get largest value set for desktop | ||
const mapping = adUnit.sizeMapping.reduce((prev, curr) => { | ||
return prev.minWidth < curr.minWidth ? curr : prev; | ||
}); | ||
if (mapping.sizes && mapping.sizes.length) { | ||
return mapping.sizes; | ||
} | ||
return adUnit.sizes; | ||
} | ||
let sizes = ''; | ||
const mapping = adUnit.sizeMapping.find(sizeMapping => { | ||
return width >= sizeMapping.minWidth; | ||
}); | ||
if (mapping && mapping.sizes && mapping.sizes.length) { | ||
sizes = mapping.sizes; | ||
utils.logMessage(`AdUnit : ${adUnit.code} resized based on device width to : ${sizes}`); | ||
} else { | ||
utils.logMessage(`AdUnit : ${adUnit.code} not mapped to any sizes for device width. This request will be suppressed.`); | ||
} | ||
return sizes; | ||
} | ||
|
||
function isSizeMappingValid(sizeMapping) { | ||
if (utils.isArray(sizeMapping) && sizeMapping.length > 0) { | ||
return true; | ||
} | ||
utils.logInfo('No size mapping defined'); | ||
return false; | ||
/** | ||
* | ||
* @param {Array<SizeConfig>} config | ||
*/ | ||
export function setSizeConfig(config) { | ||
sizeConfig = config; | ||
} | ||
config.getConfig('sizeConfig', config => setSizeConfig(config.sizeConfig)); | ||
|
||
function getScreenWidth(win) { | ||
var w = win || _win || window; | ||
var d = w.document; | ||
/** | ||
* 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 | ||
* @param {boolean} labelAll if true, all labels must match to be enabled | ||
* @param {Array<string>} requestLabels Labels passed in through requestBids | ||
* @param {Array<Array<number>>} sizes Sizes specified on adUnit | ||
* @param {Array<SizeConfig>} configs | ||
* @returns {{labels: Array<string>, sizes: Array<Array<number>>}} | ||
*/ | ||
export function resolveStatus({labels = [], labelAll = false, requestLabels = []} = {}, sizes = [], configs = sizeConfig) { | ||
let maps = evaluateSizeConfig(configs); | ||
|
||
if (w.innerWidth) { | ||
return w.innerWidth; | ||
} else if (d.body.clientWidth) { | ||
return d.body.clientWidth; | ||
} else if (d.documentElement.clientWidth) { | ||
return d.documentElement.clientWidth; | ||
let filteredSizes; | ||
if (maps.shouldFilter) { | ||
filteredSizes = sizes.filter(size => maps.sizesSupported[size]); | ||
} else { | ||
filteredSizes = sizes; | ||
} | ||
return 0; | ||
} | ||
|
||
function setWindow(win) { | ||
_win = win; | ||
return { | ||
active: filteredSizes.length > 0 && ( | ||
labels.length === 0 || ( | ||
(!labelAll && ( | ||
labels.some(label => maps.labels[label]) || | ||
labels.some(label => requestLabels.includes(label)) | ||
)) || | ||
(labelAll && ( | ||
labels.reduce((result, label) => !result ? result : ( | ||
maps.labels[label] || requestLabels.includes(label) | ||
), true) | ||
)) | ||
) | ||
), | ||
sizes: filteredSizes | ||
}; | ||
} | ||
|
||
export { mapSizes, getScreenWidth, setWindow }; | ||
function evaluateSizeConfig(configs) { | ||
return configs.reduce((results, config) => { | ||
if ( | ||
typeof config === 'object' && | ||
typeof config.mediaQuery === 'string' && | ||
matchMedia(config.mediaQuery).matches | ||
) { | ||
if (Array.isArray(config.sizesSupported)) { | ||
results.shouldFilter = true; | ||
} | ||
['labels', 'sizesSupported'].forEach( | ||
type => (config[type] || []).forEach( | ||
thing => results[type][thing] = true | ||
) | ||
); | ||
} | ||
return results; | ||
}, { | ||
labels: {}, | ||
sizesSupported: {}, | ||
shouldFilter: false | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
obj
could use a better namebidOrAdUnit
?