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

Update VIS.X bid adapter #3777

Merged
merged 1 commit into from
Apr 25, 2019
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
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