From c8e9bc0eeb7616b3844adede41fb92b7ac8d5cf8 Mon Sep 17 00:00:00 2001 From: Yvan Date: Sat, 30 Sep 2017 20:22:35 +0200 Subject: [PATCH 1/2] Added standard bidCpmAdjustment --- src/bidmanager.js | 93 +++++++++++++++++++----------------- test/spec/bidmanager_spec.js | 29 +++++++++-- 2 files changed, 75 insertions(+), 47 deletions(-) diff --git a/src/bidmanager.js b/src/bidmanager.js index 800a0fe9579..40022724194 100644 --- a/src/bidmanager.js +++ b/src/bidmanager.js @@ -431,10 +431,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 (code && $$PREBID_GLOBAL$$.bidderSettings) { + if ($$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] && $$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); } @@ -454,48 +460,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]; } diff --git a/test/spec/bidmanager_spec.js b/test/spec/bidmanager_spec.js index fd5f4fa2b31..2eb780a2d8d 100644 --- a/test/spec/bidmanager_spec.js +++ b/test/spec/bidmanager_spec.js @@ -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: { @@ -207,6 +207,9 @@ describe('bidmanager.js', function () { }, }, standard: { + bidCpmAdjustment: function (bidCpm) { + return 200; + }, adserverTargeting: [ { key: 'hb_bidder', @@ -229,10 +232,28 @@ describe('bidmanager.js', function () { } }; + }); - var expected = { 'hb_bidder': bidderCode, 'hb_adid': adId, 'hb_pb': 10.0 }; - var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid); - assert.deepEqual(response, expected); + it('Standard bidCpmAdjustment should change 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 () { From 2c12600fd3d69d264cbf0a079088b52cbc8f89f8 Mon Sep 17 00:00:00 2001 From: Yvan Date: Sat, 30 Sep 2017 20:52:51 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Fix=20missing=20=C2=ABtypeof=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bidmanager.js | 6 +++--- test/spec/bidmanager_spec.js | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/bidmanager.js b/src/bidmanager.js index 40022724194..8dcf70fec62 100644 --- a/src/bidmanager.js +++ b/src/bidmanager.js @@ -432,10 +432,10 @@ function adjustBids(bid) { var code = bid.bidderCode; var bidPriceAdjusted = bid.cpm; let bidCpmAdjustment; - if (code && $$PREBID_GLOBAL$$.bidderSettings) { - if ($$PREBID_GLOBAL$$.bidderSettings[code] && typeof $$PREBID_GLOBAL$$.bidderSettings[code].bidCpmAdjustment === 'function') { + 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] && $$PREBID_GLOBAL$$.bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD].bidCpmAdjustment === 'function') { + } 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) { diff --git a/test/spec/bidmanager_spec.js b/test/spec/bidmanager_spec.js index 2eb780a2d8d..d925c7590b0 100644 --- a/test/spec/bidmanager_spec.js +++ b/test/spec/bidmanager_spec.js @@ -232,10 +232,13 @@ describe('bidmanager.js', function () { } }; - }); - it('Standard bidCpmAdjustment should change the bid of any bidder', function () { + var expected = { 'hb_bidder': bidderCode, 'hb_adid': adId, 'hb_pb': 10.0 }; + var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid); + assert.deepEqual(response, expected); + }); + it('Standard bidCpmAdjustment changes the bid of any bidder', function () { const bid = Object.assign({}, bidfactory.createBid(2), fixtures.getBidResponses()[5]