Skip to content

Commit

Permalink
Added the ability to send multiple bids in one ad request for mediafo…
Browse files Browse the repository at this point in the history
…rce bid adapter (#5834)

* Added the ability to send multiple bids in one ad request for mediaforce bid adapter

* Fixes after review for mediaforce bid adapter
  • Loading branch information
Niksok authored Oct 12, 2020
1 parent e39a812 commit ad41a19
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 31 deletions.
78 changes: 52 additions & 26 deletions modules/mediaforceBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,28 @@ export const spec = {
}

const referer = bidderRequest && bidderRequest.refererInfo ? encodeURIComponent(bidderRequest.refererInfo.referer) : '';
const auctionId = bidderRequest && bidderRequest.auctionId;
const timeout = bidderRequest && bidderRequest.timeout;
const dnt = utils.getDNT() ? 1 : 0;
let requests = [];
const requestsMap = {};
const requests = [];
let isTest = false;
validBidRequests.forEach(bid => {
isTest = isTest || bid.params.is_test;
let tagid = bid.params.placement_id;
let bidfloor = bid.params.bidfloor ? parseFloat(bid.params.bidfloor) : 0;
let imp = [];
let validImp = false;
let impObj = {
id: bid.bidId,
tagid: tagid,
secure: 1,
secure: window.location.protocol === 'https' ? 1 : 0,
bidfloor: bidfloor,
ext: {
mediaforce: {
transactionId: bid.transactionId
}
}

};
for (let mediaTypes in bid.mediaTypes) {
switch (mediaTypes) {
Expand All @@ -140,31 +150,47 @@ export const spec = {
default: return;
}
}
validImp && imp.push(impObj);

let request = {
id: bid.transactionId,
site: {
page: referer,
ref: referer,
id: bid.params.publisher_id,
publisher: {
id: bid.params.publisher_id
let request = requestsMap[bid.params.publisher_id];
if (!request) {
request = {
id: Math.round(Math.random() * 1e16).toString(16),
site: {
page: window.location.href,
ref: referer,
id: bid.params.publisher_id,
publisher: {
id: bid.params.publisher_id
},
},
},
device: {
ua: navigator.userAgent,
js: 1,
dnt: dnt,
language: getLanguage()
},
imp
};
requests.push({
method: 'POST',
url: bid.params.is_test ? TEST_ENDPOINT_URL : ENDPOINT_URL,
data: JSON.stringify(request)
});
device: {
ua: navigator.userAgent,
js: 1,
dnt: dnt,
language: getLanguage()
},
ext: {
mediaforce: {
hb_key: auctionId
}
},
tmax: timeout,
imp: []
};
requestsMap[bid.params.publisher_id] = request;
requests.push({
method: 'POST',
url: ENDPOINT_URL,
data: request
});
}
validImp && request.imp.push(impObj);
});
requests.forEach((req) => {
if (isTest) {
req.url = TEST_ENDPOINT_URL;
}
req.data = JSON.stringify(req.data);
});
return requests;
},
Expand Down
169 changes: 164 additions & 5 deletions test/spec/modules/mediaforceBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ describe('mediaforce bid adapter', function () {
transactionId: 'd45dd707-a418-42ec-b8a7-b70a6c6fab0b',
};

const multiBid = [
{
publisher_id: 'pub123',
placement_id: '202',
},
{
publisher_id: 'pub123',
placement_id: '203',
},
{
publisher_id: 'pub124',
placement_id: '202',
},
{
publisher_id: 'pub123',
placement_id: '203',
transactionId: '8df76688-1618-417a-87b1-60ad046841c9'
}
].map(({publisher_id, placement_id, transactionId}) => {
return {
bidder: 'mediaforce',
params: {publisher_id, placement_id},
mediaTypes: {
banner: {
sizes: [[300, 250], [600, 400]]
}
},
transactionId: transactionId || 'd45dd707-a418-42ec-b8a7-b70a6c6fab0b'
}
});

const refererInfo = {
referer: 'https://www.prebid.org',
reachedTop: true,
Expand All @@ -111,7 +142,9 @@ describe('mediaforce bid adapter', function () {

const requestUrl = `${baseUrl}/header_bid`;
const dnt = utils.getDNT() ? 1 : 0;
const secure = 1;
const secure = window.location.protocol === 'https' ? 1 : 0;
const pageUrl = window.location.href;
const timeout = 1500;

it('should return undefined if no validBidRequests passed', function () {
assert.equal(spec.buildRequests([]), undefined);
Expand All @@ -127,18 +160,29 @@ describe('mediaforce bid adapter', function () {
bid.params.bidfloor = 0.5;

let bidRequests = [bid];
let bidderRequest = {bids: bidRequests, refererInfo: refererInfo};
let bidderRequest = {
bids: bidRequests,
refererInfo: refererInfo,
timeout: timeout,
auctionId: '210a474e-88f0-4646-837f-4253b7cf14fb'
};

let [request] = spec.buildRequests(bidRequests, bidderRequest);

let data = JSON.parse(request.data);
assert.deepEqual(data, {
id: bid.transactionId,
id: data.id,
tmax: timeout,
ext: {
mediaforce: {
hb_key: bidderRequest.auctionId
}
},
site: {
id: bid.params.publisher_id,
publisher: {id: bid.params.publisher_id},
ref: encodeURIComponent(refererInfo.referer),
page: encodeURIComponent(refererInfo.referer),
page: pageUrl,
},
device: {
ua: navigator.userAgent,
Expand All @@ -150,6 +194,11 @@ describe('mediaforce bid adapter', function () {
tagid: bid.params.placement_id,
secure: secure,
bidfloor: bid.params.bidfloor,
ext: {
mediaforce: {
transactionId: bid.transactionId
}
},
banner: {w: 300, h: 250},
native: {
ver: '1.2',
Expand All @@ -170,7 +219,7 @@ describe('mediaforce bid adapter', function () {
assert.deepEqual(request, {
method: 'POST',
url: requestUrl,
data: '{"id":"d45dd707-a418-42ec-b8a7-b70a6c6fab0b","site":{"page":"https%3A%2F%2Fwww.prebid.org","ref":"https%3A%2F%2Fwww.prebid.org","id":"pub123","publisher":{"id":"pub123"}},"device":{"ua":"' + navigator.userAgent + '","js":1,"dnt":' + dnt + ',"language":"' + language + '"},"imp":[{"tagid":"202","secure":1,"bidfloor":0.5,"banner":{"w":300,"h":250},"native":{"ver":"1.2","request":{"assets":[{"required":1,"id":1,"title":{"len":800}},{"required":1,"id":3,"img":{"type":3,"w":300,"h":250}},{"required":1,"id":5,"data":{"type":1}}],"context":1,"plcmttype":1,"ver":"1.2"}}}]}',
data: '{"id":"' + data.id + '","site":{"page":"' + pageUrl + '","ref":"https%3A%2F%2Fwww.prebid.org","id":"pub123","publisher":{"id":"pub123"}},"device":{"ua":"' + navigator.userAgent + '","js":1,"dnt":' + dnt + ',"language":"' + language + '"},"ext":{"mediaforce":{"hb_key":"210a474e-88f0-4646-837f-4253b7cf14fb"}},"tmax":1500,"imp":[{"tagid":"202","secure":' + secure + ',"bidfloor":0.5,"ext":{"mediaforce":{"transactionId":"d45dd707-a418-42ec-b8a7-b70a6c6fab0b"}},"banner":{"w":300,"h":250},"native":{"ver":"1.2","request":{"assets":[{"required":1,"id":1,"title":{"len":800}},{"required":1,"id":3,"img":{"type":3,"w":300,"h":250}},{"required":1,"id":5,"data":{"type":1}}],"context":1,"plcmttype":1,"ver":"1.2"}}}]}',
});
});

Expand All @@ -186,6 +235,116 @@ describe('mediaforce bid adapter', function () {
let data = JSON.parse(request.data);
assert.deepEqual(data.imp[0].banner, {w: 300, h: 600, format: [{w: 300, h: 250}]});
});

it('should return proper requests for multiple imps', function () {
let bidderRequest = {
bids: multiBid,
refererInfo: refererInfo,
timeout: timeout,
auctionId: '210a474e-88f0-4646-837f-4253b7cf14fb'
};

let requests = spec.buildRequests(multiBid, bidderRequest);
assert.equal(requests.length, 2);
requests.forEach((req) => {
req.data = JSON.parse(req.data);
});

assert.deepEqual(requests, [
{
method: 'POST',
url: requestUrl,
data: {
id: requests[0].data.id,
tmax: timeout,
ext: {
mediaforce: {
hb_key: bidderRequest.auctionId
}
},
site: {
id: 'pub123',
publisher: {id: 'pub123'},
ref: encodeURIComponent(refererInfo.referer),
page: pageUrl,
},
device: {
ua: navigator.userAgent,
dnt: dnt,
js: 1,
language: language,
},
imp: [{
tagid: '202',
secure: secure,
bidfloor: 0,
ext: {
mediaforce: {
transactionId: 'd45dd707-a418-42ec-b8a7-b70a6c6fab0b'
}
},
banner: {w: 300, h: 250, format: [{w: 600, h: 400}]},
}, {
tagid: '203',
secure: secure,
bidfloor: 0,
ext: {
mediaforce: {
transactionId: 'd45dd707-a418-42ec-b8a7-b70a6c6fab0b'
}
},
banner: {w: 300, h: 250, format: [{w: 600, h: 400}]},
}, {
tagid: '203',
secure: secure,
bidfloor: 0,
ext: {
mediaforce: {
transactionId: '8df76688-1618-417a-87b1-60ad046841c9'
}
},
banner: {w: 300, h: 250, format: [{w: 600, h: 400}]},
}]
}
},
{
method: 'POST',
url: requestUrl,
data: {
id: requests[1].data.id,
tmax: timeout,
ext: {
mediaforce: {
hb_key: bidderRequest.auctionId
}
},
site: {
id: 'pub124',
publisher: {id: 'pub124'},
ref: encodeURIComponent(refererInfo.referer),
page: pageUrl,
},
device: {
ua: navigator.userAgent,
dnt: dnt,
js: 1,
language: language,
},
imp: [{
tagid: '202',
secure: secure,
bidfloor: 0,
ext: {
mediaforce: {
transactionId: 'd45dd707-a418-42ec-b8a7-b70a6c6fab0b'
}
},
banner: {w: 300, h: 250, format: [{w: 600, h: 400}]},
}]
}
}
]);
});
});

describe('interpretResponse() banner', function () {
Expand Down

0 comments on commit ad41a19

Please sign in to comment.