Skip to content

Commit

Permalink
Update VIS.X bid adapter (#3777)
Browse files Browse the repository at this point in the history
* update VIS.X bid adapter to support identical uids in the parameters
* added wrapperType and wrapperVersion parameters in the ad request for VIS.X bid adapter
* added second iteration of the response processing to ignore requested sizes in VIS.X bid adapter
  • Loading branch information
mk0x9 authored and jsnellbaker committed Apr 25, 2019
1 parent 2741f95 commit f1aeb85
Show file tree
Hide file tree
Showing 2 changed files with 312 additions and 36 deletions.
76 changes: 64 additions & 12 deletions modules/visxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const spec = {
buildRequests: function(validBidRequests, bidderRequest) {
const auids = [];
const bidsMap = {};
const slotsMapByUid = {};
const sizeMap = {};
const bids = validBidRequests || [];
const currency =
config.getConfig(`currency.bidderCurrencyDefault.${BIDDER_CODE}`) ||
Expand All @@ -33,21 +35,46 @@ export const spec = {
let reqId;

bids.forEach(bid => {
if (!bidsMap[bid.params.uid]) {
bidsMap[bid.params.uid] = [bid];
auids.push(bid.params.uid);
reqId = bid.bidderRequestId;
const {params: {uid}, adUnitCode} = bid;
auids.push(uid);
const sizesId = utils.parseSizesInput(bid.sizes);

if (!slotsMapByUid[uid]) {
slotsMapByUid[uid] = {};
}
const slotsMap = slotsMapByUid[uid];
if (!slotsMap[adUnitCode]) {
slotsMap[adUnitCode] = {adUnitCode, bids: [bid], parents: []};
} else {
bidsMap[bid.params.uid].push(bid);
slotsMap[adUnitCode].bids.push(bid);
}
reqId = bid.bidderRequestId;
const slot = slotsMap[adUnitCode];

sizesId.forEach((sizeId) => {
sizeMap[sizeId] = true;
if (!bidsMap[uid]) {
bidsMap[uid] = {};
}

if (!bidsMap[uid][sizeId]) {
bidsMap[uid][sizeId] = [slot];
} else {
bidsMap[uid][sizeId].push(slot);
}
slot.parents.push({parent: bidsMap[uid], key: sizeId, uid});
});
});

const payload = {
u: utils.getTopWindowUrl(),
pt: 'net',
auids: auids.join(','),
sizes: utils.getKeys(sizeMap).join(','),
r: reqId,
cur: currency,
wrapperType: 'Prebid_js',
wrapperVersion: '$prebid.version$'
};

if (bidderRequest && bidderRequest.gdprConsent) {
Expand All @@ -69,6 +96,7 @@ export const spec = {
interpretResponse: function(serverResponse, bidRequest) {
serverResponse = serverResponse && serverResponse.body;
const bidResponses = [];
const bidsWithoutSizeMatching = [];
const bidsMap = bidRequest.bidsMap;
const currency = bidRequest.data.cur;

Expand All @@ -81,7 +109,10 @@ export const spec = {

if (!errorMessage && serverResponse.seatbid) {
serverResponse.seatbid.forEach(respItem => {
_addBidResponse(_getBidFromResponse(respItem), bidsMap, currency, bidResponses);
_addBidResponse(_getBidFromResponse(respItem), bidsMap, currency, bidResponses, bidsWithoutSizeMatching);
});
bidsWithoutSizeMatching.forEach(serverBid => {
_addBidResponse(serverBid, bidsMap, currency, bidResponses);
});
}
if (errorMessage) utils.logError(errorMessage);
Expand Down Expand Up @@ -117,17 +148,22 @@ function _getBidFromResponse(respItem) {
return respItem && respItem.bid && respItem.bid[0];
}

function _addBidResponse(serverBid, bidsMap, currency, bidResponses) {
function _addBidResponse(serverBid, bidsMap, currency, bidResponses, bidsWithoutSizeMatching) {
if (!serverBid) return;
let errorMessage;
if (!serverBid.auid) errorMessage = LOG_ERROR_MESS.noAuid + JSON.stringify(serverBid);
if (!serverBid.adm) errorMessage = LOG_ERROR_MESS.noAdm + JSON.stringify(serverBid);
else {
const awaitingBids = bidsMap[serverBid.auid];
if (awaitingBids) {
awaitingBids.forEach(bid => {
const bidResponse = {
const sizeId = bidsWithoutSizeMatching ? `${serverBid.w}x${serverBid.h}` : Object.keys(awaitingBids)[0];
if (awaitingBids[sizeId]) {
const slot = awaitingBids[sizeId][0];

const bid = slot.bids.shift();
bidResponses.push({
requestId: bid.bidId,
bidderCode: spec.code,
cpm: serverBid.price,
width: serverBid.w,
height: serverBid.h,
Expand All @@ -137,9 +173,25 @@ function _addBidResponse(serverBid, bidsMap, currency, bidResponses) {
ttl: TIME_TO_LIVE,
ad: serverBid.adm,
dealId: serverBid.dealid
};
bidResponses.push(bidResponse);
});
});

if (!slot.bids.length) {
slot.parents.forEach(({parent, key, uid}) => {
const index = parent[key].indexOf(slot);
if (index > -1) {
parent[key].splice(index, 1);
}
if (!parent[key].length) {
delete parent[key];
if (!utils.getKeys(parent).length) {
delete bidsMap[uid];
}
}
});
}
} else {
bidsWithoutSizeMatching && bidsWithoutSizeMatching.push(serverBid);
}
} else {
errorMessage = LOG_ERROR_MESS.noPlacementCode + serverBid.auid;
}
Expand Down
Loading

0 comments on commit f1aeb85

Please sign in to comment.