Skip to content

Commit

Permalink
Fix for prebid#1628 (allowing standard bidCpmAdjustment) (prebid#1645)
Browse files Browse the repository at this point in the history
* Added standard bidCpmAdjustment

* Fix missing «typeof»
  • Loading branch information
dugwood authored and dluxemburg committed Jul 17, 2018
1 parent 01fca8c commit 8976758
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 44 deletions.
93 changes: 50 additions & 43 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,16 @@ events.on(CONSTANTS.EVENTS.BID_ADJUSTMENT, function (bid) {
function adjustBids(bid) {
var code = bid.bidderCode;
var bidPriceAdjusted = bid.cpm;
if (code && $$PREBID_GLOBAL$$.bidderSettings && $$PREBID_GLOBAL$$.bidderSettings[code]) {
if (typeof $$PREBID_GLOBAL$$.bidderSettings[code].bidCpmAdjustment === 'function') {
let bidCpmAdjustment;
if ($$PREBID_GLOBAL$$.bidderSettings) {
if (code && $$PREBID_GLOBAL$$.bidderSettings[code] && typeof $$PREBID_GLOBAL$$.bidderSettings[code].bidCpmAdjustment === 'function') {
bidCpmAdjustment = $$PREBID_GLOBAL$$.bidderSettings[code].bidCpmAdjustment;
} else if ($$PREBID_GLOBAL$$.bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD] && typeof $$PREBID_GLOBAL$$.bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD].bidCpmAdjustment === 'function') {
bidCpmAdjustment = $$PREBID_GLOBAL$$.bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD].bidCpmAdjustment;
}
if (bidCpmAdjustment) {
try {
bidPriceAdjusted = $$PREBID_GLOBAL$$.bidderSettings[code].bidCpmAdjustment.call(null, bid.cpm, Object.assign({}, bid));
bidPriceAdjusted = bidCpmAdjustment(bid.cpm, Object.assign({}, bid));
} catch (e) {
utils.logError('Error during bid adjustment', 'bidmanager.js', e);
}
Expand All @@ -449,48 +455,49 @@ function getStandardBidderSettings() {
let granularity = config.getConfig('priceGranularity');
let bidder_settings = $$PREBID_GLOBAL$$.bidderSettings;
if (!bidder_settings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD]) {
bidder_settings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD] = {
adserverTargeting: [
{
key: 'hb_bidder',
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: 'hb_adid',
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: 'hb_pb',
val: function (bidResponse) {
if (granularity === CONSTANTS.GRANULARITY_OPTIONS.AUTO) {
return bidResponse.pbAg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.DENSE) {
return bidResponse.pbDg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.LOW) {
return bidResponse.pbLg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.MEDIUM) {
return bidResponse.pbMg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.HIGH) {
return bidResponse.pbHg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.CUSTOM) {
return bidResponse.pbCg;
}
}
}, {
key: 'hb_size',
val: function (bidResponse) {
return bidResponse.size;
}
}, {
key: 'hb_deal',
val: function (bidResponse) {
return bidResponse.dealId;
bidder_settings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD] = {};
}
if (!bidder_settings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD][CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING]) {
bidder_settings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD][CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING] = [
{
key: 'hb_bidder',
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: 'hb_adid',
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: 'hb_pb',
val: function (bidResponse) {
if (granularity === CONSTANTS.GRANULARITY_OPTIONS.AUTO) {
return bidResponse.pbAg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.DENSE) {
return bidResponse.pbDg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.LOW) {
return bidResponse.pbLg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.MEDIUM) {
return bidResponse.pbMg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.HIGH) {
return bidResponse.pbHg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.CUSTOM) {
return bidResponse.pbCg;
}
}
]
};
}, {
key: 'hb_size',
val: function (bidResponse) {
return bidResponse.size;
}
}, {
key: 'hb_deal',
val: function (bidResponse) {
return bidResponse.dealId;
}
}
];
}
return bidder_settings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD];
}
Expand Down
26 changes: 25 additions & 1 deletion test/spec/bidmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('bidmanager.js', function () {
assert.deepEqual(response, expected);
});

it('Custom bidCpmAdjustment for one bidder and inherit standard', function () {
it('Custom bidCpmAdjustment for one bidder and inherit standard but doesn\'t use standard bidCpmAdjustment', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
appnexus: {
Expand All @@ -207,6 +207,9 @@ describe('bidmanager.js', function () {
},
},
standard: {
bidCpmAdjustment: function (bidCpm) {
return 200;
},
adserverTargeting: [
{
key: 'hb_bidder',
Expand Down Expand Up @@ -235,6 +238,27 @@ describe('bidmanager.js', function () {
assert.deepEqual(response, expected);
});

it('Standard bidCpmAdjustment changes the bid of any bidder', function () {
const bid = Object.assign({},
bidfactory.createBid(2),
fixtures.getBidResponses()[5]
);

assert.equal(bid.cpm, 0.5);

$$PREBID_GLOBAL$$.bidderSettings =
{
standard: {
bidCpmAdjustment: function (bidCpm) {
return bidCpm * 0.5;
}
}
};

bidmanager.adjustBids(bid)
assert.equal(bid.cpm, 0.25);
});

it('Custom bidCpmAdjustment AND custom configuration for one bidder and inherit standard settings', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
Expand Down

0 comments on commit 8976758

Please sign in to comment.