forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core & priceFloors: pass bid request to
bidCpmAdjustment
; warn abou…
…t invalid `adUnit.floors` definitions (prebid#9441) * Core & priceFloors: pass `bidRequest` as third arg to `bidCpmAdjustment` * Floors: warn when adUnit.floors is not valid
- Loading branch information
1 parent
e16602a
commit 2b35182
Showing
4 changed files
with
90 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {auctionManager} from '../auctionManager.js'; | ||
import {bidderSettings} from '../bidderSettings.js'; | ||
import {logError} from '../utils.js'; | ||
|
||
export function adjustCpm(cpm, bidResponse, bidRequest, {index = auctionManager.index, bs = bidderSettings} = {}) { | ||
bidRequest = bidRequest || index.getBidRequest(bidResponse); | ||
const bidCpmAdjustment = bs.get(bidResponse?.bidderCode || bidRequest?.bidder, 'bidCpmAdjustment'); | ||
|
||
if (bidCpmAdjustment && typeof bidCpmAdjustment === 'function') { | ||
try { | ||
return bidCpmAdjustment(cpm, Object.assign({}, bidResponse), bidRequest); | ||
} catch (e) { | ||
logError('Error during bid adjustment', e); | ||
} | ||
} | ||
return cpm; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {adjustCpm} from '../../../../src/utils/cpm.js'; | ||
|
||
describe('adjustCpm', () => { | ||
const bidderCode = 'mockBidder'; | ||
let adjustmentFn, bs, index; | ||
beforeEach(() => { | ||
bs = { | ||
get: sinon.stub() | ||
} | ||
index = { | ||
getBidRequest: sinon.stub() | ||
} | ||
adjustmentFn = sinon.stub().callsFake((cpm) => cpm * 2); | ||
}) | ||
|
||
it('throws when neither bidRequest nor bidResponse are provided', () => { | ||
expect(() => adjustCpm(1)).to.throw(); | ||
}) | ||
|
||
it('always provides an object as bidResponse for the adjustment fn', () => { | ||
bs.get.callsFake(() => adjustmentFn); | ||
adjustCpm(1, null, {bidder: bidderCode}, {index, bs}); | ||
sinon.assert.calledWith(adjustmentFn, 1, {}); | ||
}); | ||
|
||
describe('when no bidRequest is provided', () => { | ||
Object.entries({ | ||
'unavailable': undefined, | ||
'found': {foo: 'bar'} | ||
}).forEach(([t, req]) => { | ||
describe(`and it is ${t} in the index`, () => { | ||
beforeEach(() => { | ||
bs.get.callsFake(() => adjustmentFn); | ||
index.getBidRequest.callsFake(() => req) | ||
}); | ||
|
||
it('provides it to the adjustment fn', () => { | ||
const bidResponse = {bidderCode}; | ||
adjustCpm(1, bidResponse, undefined, {index, bs}); | ||
sinon.assert.calledWith(index.getBidRequest, bidResponse); | ||
sinon.assert.calledWith(adjustmentFn, 1, bidResponse, req); | ||
}) | ||
}) | ||
}) | ||
}); | ||
|
||
Object.entries({ | ||
'bidResponse': [{bidderCode}], | ||
'bidRequest': [null, {bidder: bidderCode}], | ||
}).forEach(([t, [bidResp, bidReq]]) => { | ||
describe(`when passed ${t}`, () => { | ||
beforeEach(() => { | ||
bs.get.callsFake((bidder) => { if (bidder === bidderCode) return adjustmentFn }); | ||
}); | ||
it('retrieves the correct bidder code', () => { | ||
expect(adjustCpm(1, bidResp, bidReq, {bs, index})).to.eql(2); | ||
}); | ||
it('passes them to the adjustment fn', () => { | ||
adjustCpm(1, bidResp, bidReq, {bs, index}); | ||
sinon.assert.calledWith(adjustmentFn, 1, bidResp == null ? sinon.match.any : bidResp, bidReq); | ||
}); | ||
}); | ||
}) | ||
}); |