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

Prebid Server Bid Adapter: adding support for getFloor #6273

Merged
merged 1 commit into from
Feb 18, 2021
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
17 changes: 17 additions & 0 deletions modules/prebidServerBidAdapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,23 @@ const OPEN_RTB_PROTOCOL = {
utils.deepSetValue(imp, 'ext.prebid.storedauctionresponse.id', storedAuctionResponseBid.storedAuctionResponse.toString());
}

const getFloorBid = find(firstBidRequest.bids, bid => bid.adUnitCode === adUnit.code && typeof bid.getFloor === 'function');

if (getFloorBid) {
let floorInfo;
try {
floorInfo = getFloorBid.getFloor({
currency: config.getConfig('currency.adServerCurrency') || DEFAULT_S2S_CURRENCY,
});
} catch (e) {
utils.logError('PBS: getFloor threw an error: ', e);
}
if (floorInfo && floorInfo.currency && !isNaN(parseFloat(floorInfo.floor))) {
imp.bidfloor = parseFloat(floorInfo.floor);
imp.bidfloorcur = floorInfo.currency
}
}

if (imp.banner || imp.video || imp.native) {
imps.push(imp);
}
Expand Down
95 changes: 95 additions & 0 deletions test/spec/modules/prebidServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,101 @@ describe('S2S Adapter', function () {
expect(requestBid.imp[0].ext.prebid.storedauctionresponse.id).to.equal('11111');
});

describe('price floors module', function () {
function runTest(expectedFloor, expectedCur) {
adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax);
const requestBid = JSON.parse(server.requests[requestCount].requestBody);
expect(requestBid.imp[0].bidfloor).to.equal(expectedFloor);
expect(requestBid.imp[0].bidfloorcur).to.equal(expectedCur);
requestCount += 1;
}

let getFloorResponse, requestCount;
beforeEach(function () {
getFloorResponse = {};
requestCount = 0;
});

it('should NOT pass bidfloor and bidfloorcur when getFloor not present or returns invalid response', function () {
const _config = {
s2sConfig: CONFIG,
};

config.setConfig(_config);

// if no get floor
runTest(undefined, undefined);

// if getFloor returns empty object
BID_REQUESTS[0].bids[0].getFloor = () => getFloorResponse;
sinon.spy(BID_REQUESTS[0].bids[0], 'getFloor');

runTest(undefined, undefined);
// make sure getFloor was called
expect(
BID_REQUESTS[0].bids[0].getFloor.calledWith({
currency: 'USD',
})
).to.be.true;

// if getFloor does not return number
getFloorResponse = {currency: 'EUR', floor: 'not a number'};
runTest(undefined, undefined);

// if getFloor does not return currency
getFloorResponse = {floor: 1.1};
runTest(undefined, undefined);
});

it('should correctly pass bidfloor and bidfloorcur', function () {
const _config = {
s2sConfig: CONFIG,
};

config.setConfig(_config);

BID_REQUESTS[0].bids[0].getFloor = () => getFloorResponse;
sinon.spy(BID_REQUESTS[0].bids[0], 'getFloor');

// returns USD and string floor
getFloorResponse = {currency: 'USD', floor: '1.23'};
runTest(1.23, 'USD');
// make sure getFloor was called
expect(
BID_REQUESTS[0].bids[0].getFloor.calledWith({
currency: 'USD',
})
).to.be.true;

// returns non USD and number floor
getFloorResponse = {currency: 'EUR', floor: 0.85};
runTest(0.85, 'EUR');
});

it('should correctly pass adServerCurrency when set to getFloor not default', function () {
config.setConfig({
s2sConfig: CONFIG,
currency: { adServerCurrency: 'JPY' },
});

// we have to start requestCount at 1 because a conversion rates fetch occurs when adServerCur is not USD!
requestCount = 1;

BID_REQUESTS[0].bids[0].getFloor = () => getFloorResponse;
sinon.spy(BID_REQUESTS[0].bids[0], 'getFloor');

// returns USD and string floor
getFloorResponse = {currency: 'JPY', floor: 97.2};
runTest(97.2, 'JPY');
// make sure getFloor was called with JPY
expect(
BID_REQUESTS[0].bids[0].getFloor.calledWith({
currency: 'JPY',
})
).to.be.true;
});
});

it('adds device.w and device.h even if the config lacks a device object', function () {
const _config = {
s2sConfig: CONFIG,
Expand Down