Skip to content

Commit

Permalink
Prebid Core: Configurable maxbid (prebid#11519)
Browse files Browse the repository at this point in the history
* 11252 Configurable maxbid

* lint fixes & add docs

* removing unnecessary logic

---------

Co-authored-by: mkomorski <[email protected]>
Co-authored-by: Marcin Komorski <[email protected]>
  • Loading branch information
3 people authored and zkosanovic committed May 28, 2024
1 parent 24a5750 commit 3e9c2d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import {auctionManager} from './auctionManager.js';
import {bidderSettings} from './bidderSettings.js';
import * as events from './events.js';
import adapterManager from './adapterManager.js';
import { EVENTS, GRANULARITY_OPTIONS, JSON_MAPPING, S2S, TARGETING_KEYS } from './constants.js';
import { EVENTS, GRANULARITY_OPTIONS, JSON_MAPPING, REJECTION_REASON, S2S, TARGETING_KEYS } from './constants.js';
import {defer, GreedyPromise} from './utils/promise.js';
import {useMetrics} from './utils/perfMetrics.js';
import {adjustCpm} from './utils/cpm.js';
Expand Down Expand Up @@ -421,7 +421,11 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
* @param {function(String): void} reject a function that, when called, rejects `bid` with the given reason.
*/
export const addBidResponse = hook('sync', function(adUnitCode, bid, reject) {
this.dispatch.call(null, adUnitCode, bid);
if (!isValidPrice(bid)) {
reject(REJECTION_REASON.PRICE_TOO_HIGH)
} else {
this.dispatch.call(null, adUnitCode, bid);
}
}, 'addBidResponse');

/**
Expand Down Expand Up @@ -974,3 +978,17 @@ function groupByPlacement(bidsByPlacement, bid) {
bidsByPlacement[bid.adUnitCode].bids.push(bid);
return bidsByPlacement;
}

/**
* isValidPrice is price validation function
* which checks if price from bid response
* is not higher than top limit set in config
* @type {Function}
* @param bid
* @returns {boolean}
*/
function isValidPrice(bid) {
const maxBidValue = config.getConfig('maxBid');
if (!maxBidValue || !bid.cpm) return true;
return maxBidValue >= Number(bid.cpm);
}
4 changes: 4 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const DEFAULT_DISABLE_AJAX_TIMEOUT = false;
const DEFAULT_BID_CACHE = false;
const DEFAULT_DEVICE_ACCESS = true;
const DEFAULT_MAX_NESTED_IFRAMES = 10;
const DEFAULT_MAXBID_VALUE = 5000

const DEFAULT_TIMEOUTBUFFER = 400;

Expand Down Expand Up @@ -160,6 +161,9 @@ export function newConfig() {

// default max nested iframes for referer detection
maxNestedIframes: DEFAULT_MAX_NESTED_IFRAMES,

// default max bid
maxBid: DEFAULT_MAXBID_VALUE
};

Object.defineProperties(newConfig,
Expand Down
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ export const REJECTION_REASON = {
FLOOR_NOT_MET: 'Bid does not meet price floor',
CANNOT_CONVERT_CURRENCY: 'Unable to convert currency',
DSA_REQUIRED: 'Bid does not provide required DSA transparency info',
DSA_MISMATCH: 'Bid indicates inappropriate DSA rendering method'
DSA_MISMATCH: 'Bid indicates inappropriate DSA rendering method',
PRICE_TOO_HIGH: 'Bid price exceeds maximum value'
};

export const PREBID_NATIVE_DATA_KEYS_TO_ORTB = {
Expand Down
13 changes: 13 additions & 0 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {expect} from 'chai';
import {deepClone} from '../../src/utils.js';
import { IMAGE as ortbNativeRequest } from 'src/native.js';
import {PrebidServer} from '../../modules/prebidServerBidAdapter/index.js';
import '../../modules/currency.js'
import { setConfig as setCurrencyConfig } from '../../modules/currency.js';
import { REJECTION_REASON } from '../../src/constants.js';

/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
Expand Down Expand Up @@ -1467,6 +1470,16 @@ describe('auctionmanager.js', function () {
config.getConfig.restore();
store.store.restore();
});

it('should reject bid for price higher than limit for the same currency', () => {
sinon.stub(auction, 'addBidRejected');
config.setConfig({
maxBid: 1
});

auction.callBids();
sinon.assert.calledWith(auction.addBidRejected, sinon.match({rejectionReason: REJECTION_REASON.PRICE_TOO_HIGH}));
})
});

describe('addBidRequests', function () {
Expand Down

0 comments on commit 3e9c2d9

Please sign in to comment.