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

Implementing #697 - Custom CPM bucket sizes and precision #742

Merged
merged 2 commits into from
Oct 25, 2016
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
83 changes: 9 additions & 74 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { uniques } from './utils';
import {getPriceBucketString} from './cpmBucketManager';

var CONSTANTS = require('./constants.json');
var utils = require('./utils.js');
Expand All @@ -8,11 +9,12 @@ var objectType_function = 'function';

var externalCallbacks = {byAdUnit: [], all: [], oneTime: null, timer: false};
var _granularity = CONSTANTS.GRANULARITY_OPTIONS.MEDIUM;
let _customPriceBucket;
var defaultBidderSettingsMap = {};

const _lgPriceCap = 5.00;
const _mgPriceCap = 20.00;
const _hgPriceCap = 20.00;
exports.setCustomPriceBucket = function(customConfig) {
_customPriceBucket = customConfig;
};

/**
* Returns a list of bidders that we haven't received a response yet
Expand Down Expand Up @@ -96,12 +98,13 @@ exports.addBidResponse = function (adUnitCode, bid) {
events.emit(CONSTANTS.EVENTS.BID_RESPONSE, bid);

//append price strings
const priceStringsObj = getPriceBucketString(bid.cpm, bid.height, bid.width);
const priceStringsObj = getPriceBucketString(bid.cpm, _customPriceBucket);
bid.pbLg = priceStringsObj.low;
bid.pbMg = priceStringsObj.med;
bid.pbHg = priceStringsObj.high;
bid.pbAg = priceStringsObj.auto;
bid.pbDg = priceStringsObj.dense;
bid.pbCg = priceStringsObj.custom;

//if there is any key value pairs to map do here
var keyValues = {};
Expand Down Expand Up @@ -370,6 +373,8 @@ function getStandardBidderSettings() {
return bidResponse.pbMg;
} else if (_granularity === CONSTANTS.GRANULARITY_OPTIONS.HIGH) {
return bidResponse.pbHg;
} else if (_granularity === CONSTANTS.GRANULARITY_OPTIONS.CUSTOM) {
return bidResponse.pbCg;
}
}
}, {
Expand All @@ -389,73 +394,3 @@ function getStandardBidderAdServerTargeting() {
}

exports.getStandardBidderAdServerTargeting = getStandardBidderAdServerTargeting;

function getPriceBucketString(cpm) {
var cpmFloat = 0;
var returnObj = {
low: '',
med: '',
high: '',
auto: '',
dense: ''
};
try {
cpmFloat = parseFloat(cpm);
if (cpmFloat) {
//round to closest .5
if (cpmFloat > _lgPriceCap) {
returnObj.low = _lgPriceCap.toFixed(2);
} else {
returnObj.low = (Math.floor(cpm * 2) / 2).toFixed(2);
}

//round to closest .1
if (cpmFloat > _mgPriceCap) {
returnObj.med = _mgPriceCap.toFixed(2);
} else {
returnObj.med = (Math.floor(cpm * 10) / 10).toFixed(2);
}

//round to closest .01
if (cpmFloat > _hgPriceCap) {
returnObj.high = _hgPriceCap.toFixed(2);
} else {
returnObj.high = (Math.floor(cpm * 100) / 100).toFixed(2);
}

// round auto default sliding scale
if (cpmFloat <= 5) {
// round to closest .05
returnObj.auto = (Math.floor(cpm * 20) / 20).toFixed(2);
} else if (cpmFloat <= 10) {
// round to closest .10
returnObj.auto = (Math.floor(cpm * 10) / 10).toFixed(2);
} else if (cpmFloat <= 20) {
// round to closest .50
returnObj.auto = (Math.floor(cpm * 2) / 2).toFixed(2);
} else {
// cap at 20.00
returnObj.auto = '20.00';
}

// dense mode
if (cpmFloat <= 3) {
// round to closest .01
returnObj.dense = (Math.floor(cpm * 100) / 100).toFixed(2);
} else if (cpmFloat <= 8) {
// round to closest .05
returnObj.dense = (Math.floor(cpm * 20) / 20).toFixed(2);
} else if (cpmFloat <= 20) {
// round to closest .50
returnObj.dense = (Math.floor(cpm * 2) / 2).toFixed(2);
} else {
// cap at 20.00
returnObj.dense = '20.00';
}
}
} catch (e) {
utils.logError('Exception parsing CPM', 'bidmanager.js', e);
}

return returnObj;
}
3 changes: 2 additions & 1 deletion src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"MEDIUM": "medium",
"HIGH": "high",
"AUTO": "auto",
"DENSE": "dense"
"DENSE": "dense",
"CUSTOM": "custom"
},
"TARGETING_KEYS": [
"hb_bidder",
Expand Down
123 changes: 123 additions & 0 deletions src/cpmBucketManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const _defaultPrecision = 2;
const _lgPriceConfig = {
'buckets': [{
'min': 0,
'max': 5,
'increment': 0.5
}]
};
const _mgPriceConfig = {
'buckets': [{
'min': 0,
'max': 20,
'increment': 0.1
}]
};
const _hgPriceConfig = {
'buckets': [{
'min': 0,
'max': 20,
'increment': 0.01
}]
};
const _densePriceConfig = {
'buckets': [{
'min': 0,
'max': 3,
'increment': 0.01
},
{
'min': 3,
'max': 8,
'increment': 0.05
},
{
'min': 8,
'max': 20,
'increment': 0.5
}]
};
const _autoPriceConfig = {
'buckets': [{
'min': 0,
'max': 5,
'increment': 0.05
},
{
'min': 5,
'max': 10,
'increment': 0.1
},
{
'min': 10,
'max': 20,
'increment': 0.5
}]
};

function getPriceBucketString(cpm, customConfig) {
let cpmFloat = 0;
cpmFloat = parseFloat(cpm);
if (isNaN(cpmFloat)) {
cpmFloat = '';
}

return {
low: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _lgPriceConfig),
med: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _mgPriceConfig),
high: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _hgPriceConfig),
auto: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _autoPriceConfig),
dense: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _densePriceConfig),
custom: (cpmFloat === '') ? '' : getCpmStringValue(cpm, customConfig)
};
}

function getCpmStringValue(cpm, config) {
let cpmStr = '';
if (!isValidePriceConfig(config)) {
return cpmStr;
}
const cap = config.buckets.reduce((prev,curr) => {
if (prev.max > curr.max) {
return prev;
}
return curr;
}, {
'max': 0,
});
let bucket = config.buckets.find(bucket => {
if (cpm > cap.max) {
const precision = bucket.precision || _defaultPrecision;
cpmStr = bucket.max.toFixed(precision);
} else if (cpm <= bucket.max && cpm >= bucket.min) {
return bucket;
}
});
if (bucket) {
cpmStr = getCpmTarget(cpm, bucket.increment, bucket.precision);
}
return cpmStr;
}

function isValidePriceConfig(config) {
if (!config || !config.buckets || !Array.isArray(config.buckets)) {
return false;
}
let isValid = true;
config.buckets.forEach(bucket => {
if(typeof bucket.min === 'undefined' || !bucket.max || !bucket.increment) {
isValid = false;
}
});
return isValid;
}

function getCpmTarget(cpm, increment, precision) {
if (!precision) {
precision = _defaultPrecision;
}
let bucketSize = 1 / increment;
return (Math.floor(cpm * bucketSize) / bucketSize).toFixed(precision);
}

export { getPriceBucketString, isValidePriceConfig };
27 changes: 26 additions & 1 deletion src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { flatten, uniques, getKeys, isGptPubadsDefined, getHighestCpm } from './
import { videoAdUnit, hasNonVideoBidder } from './video';
import 'polyfill';
import {parse as parseURL, format as formatURL} from './url';
import {isValidePriceConfig} from './cpmBucketManager';

var $$PREBID_GLOBAL$$ = getGlobal();
var CONSTANTS = require('./constants.json');
Expand Down Expand Up @@ -759,13 +760,37 @@ $$PREBID_GLOBAL$$.aliasBidder = function (bidderCode, alias) {
}
};

/**
* Sets a default price granularity scheme.
* @param {String|Object} granularity - the granularity scheme.
* "low": $0.50 increments, capped at $5 CPM
* "medium": $0.10 increments, capped at $20 CPM (the default)
* "high": $0.01 increments, capped at $20 CPM
* "auto": Applies a sliding scale to determine granularity
* "dense": Like "auto", but the bid price granularity uses smaller increments, especially at lower CPMs
*
* Alternatively a custom object can be specified:
* { "buckets" : [{"min" : 0,"max" : 20,"increment" : 0.1,"cap" : true}]};
* See http://prebid.org/dev-docs/publisher-api-reference.html#module_pbjs.setPriceGranularity for more details
*/
$$PREBID_GLOBAL$$.setPriceGranularity = function (granularity) {
utils.logInfo('Invoking $$PREBID_GLOBAL$$.setPriceGranularity', arguments);
if (!granularity) {
utils.logError('Prebid Error: no value passed to `setPriceGranularity()`');
} else {
return;
}
if(typeof granularity === 'string') {
bidmanager.setPriceGranularity(granularity);
}
else if(typeof granularity === 'object') {
if(!isValidePriceConfig(granularity)){
utils.logError('Invalid custom price value passed to `setPriceGranularity()`');
return;
}
bidmanager.setCustomPriceBucket(granularity);
bidmanager.setPriceGranularity(CONSTANTS.GRANULARITY_OPTIONS.CUSTOM);
utils.logMessage('Using custom price granularity');
}
};

$$PREBID_GLOBAL$$.enableSendAllBids = function () {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/cpmInputsOutputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cpmInputs": ["17.638", "19.836", "11.501", "14.384", "23.224", "21.279", "8.886", "16.555", "10.579", "1.331", "1.998", "14.988", "14.864", "10.369", "0.262", "5.269", "6.874", "5.598", "7.191", "15.218", "10.958", "4.420", "17.749", "23.808", "12.353", "21.726", "1.562", "18.085", "1.184", "15.470", "13.841", "17.966", "22.150", "9.088", "13.613", "18.384", "13.690", "23.639", "5.085", "5.779", "11.456", "0.315", "18.557", "20.813", "18.813", "10.202", "10.143", "2.483", "16.147", "2.909", "0.652"],
"priceStringOutputs": [{"low":"5.00","med":"17.60","high":"17.63","auto":"17.50","dense":"17.50","custom":""},{"low":"5.00","med":"19.80","high":"19.83","auto":"19.50","dense":"19.50","custom":""},{"low":"5.00","med":"11.50","high":"11.50","auto":"11.50","dense":"11.50","custom":""},{"low":"5.00","med":"14.30","high":"14.38","auto":"14.00","dense":"14.00","custom":""},{"low":"5.00","med":"20.00","high":"20.00","auto":"20.00","dense":"20.00","custom":""},{"low":"5.00","med":"20.00","high":"20.00","auto":"20.00","dense":"20.00","custom":""},{"low":"5.00","med":"8.80","high":"8.88","auto":"8.80","dense":"8.50","custom":""},{"low":"5.00","med":"16.50","high":"16.55","auto":"16.50","dense":"16.50","custom":""},{"low":"5.00","med":"10.50","high":"10.57","auto":"10.50","dense":"10.50","custom":""},{"low":"1.00","med":"1.30","high":"1.33","auto":"1.30","dense":"1.33","custom":""},{"low":"1.50","med":"1.90","high":"1.99","auto":"1.95","dense":"1.99","custom":""},{"low":"5.00","med":"14.90","high":"14.98","auto":"14.50","dense":"14.50","custom":""},{"low":"5.00","med":"14.80","high":"14.86","auto":"14.50","dense":"14.50","custom":""},{"low":"5.00","med":"10.30","high":"10.36","auto":"10.00","dense":"10.00","custom":""},{"low":"0.00","med":"0.20","high":"0.26","auto":"0.25","dense":"0.26","custom":""},{"low":"5.00","med":"5.20","high":"5.26","auto":"5.20","dense":"5.25","custom":""},{"low":"5.00","med":"6.80","high":"6.87","auto":"6.80","dense":"6.85","custom":""},{"low":"5.00","med":"5.50","high":"5.59","auto":"5.50","dense":"5.55","custom":""},{"low":"5.00","med":"7.10","high":"7.19","auto":"7.10","dense":"7.15","custom":""},{"low":"5.00","med":"15.20","high":"15.21","auto":"15.00","dense":"15.00","custom":""},{"low":"5.00","med":"10.90","high":"10.95","auto":"10.50","dense":"10.50","custom":""},{"low":"4.00","med":"4.40","high":"4.42","auto":"4.40","dense":"4.40","custom":""},{"low":"5.00","med":"17.70","high":"17.74","auto":"17.50","dense":"17.50","custom":""},{"low":"5.00","med":"20.00","high":"20.00","auto":"20.00","dense":"20.00","custom":""},{"low":"5.00","med":"12.30","high":"12.35","auto":"12.00","dense":"12.00","custom":""},{"low":"5.00","med":"20.00","high":"20.00","auto":"20.00","dense":"20.00","custom":""},{"low":"1.50","med":"1.50","high":"1.56","auto":"1.55","dense":"1.56","custom":""},{"low":"5.00","med":"18.00","high":"18.08","auto":"18.00","dense":"18.00","custom":""},{"low":"1.00","med":"1.10","high":"1.18","auto":"1.15","dense":"1.18","custom":""},{"low":"5.00","med":"15.40","high":"15.47","auto":"15.00","dense":"15.00","custom":""},{"low":"5.00","med":"13.80","high":"13.84","auto":"13.50","dense":"13.50","custom":""},{"low":"5.00","med":"17.90","high":"17.96","auto":"17.50","dense":"17.50","custom":""},{"low":"5.00","med":"20.00","high":"20.00","auto":"20.00","dense":"20.00","custom":""},{"low":"5.00","med":"9.00","high":"9.08","auto":"9.00","dense":"9.00","custom":""},{"low":"5.00","med":"13.60","high":"13.61","auto":"13.50","dense":"13.50","custom":""},{"low":"5.00","med":"18.30","high":"18.38","auto":"18.00","dense":"18.00","custom":""},{"low":"5.00","med":"13.60","high":"13.69","auto":"13.50","dense":"13.50","custom":""},{"low":"5.00","med":"20.00","high":"20.00","auto":"20.00","dense":"20.00","custom":""},{"low":"5.00","med":"5.00","high":"5.08","auto":"5.00","dense":"5.05","custom":""},{"low":"5.00","med":"5.70","high":"5.77","auto":"5.70","dense":"5.75","custom":""},{"low":"5.00","med":"11.40","high":"11.45","auto":"11.00","dense":"11.00","custom":""},{"low":"0.00","med":"0.30","high":"0.31","auto":"0.30","dense":"0.31","custom":""},{"low":"5.00","med":"18.50","high":"18.55","auto":"18.50","dense":"18.50","custom":""},{"low":"5.00","med":"20.00","high":"20.00","auto":"20.00","dense":"20.00","custom":""},{"low":"5.00","med":"18.80","high":"18.81","auto":"18.50","dense":"18.50","custom":""},{"low":"5.00","med":"10.20","high":"10.20","auto":"10.00","dense":"10.00","custom":""},{"low":"5.00","med":"10.10","high":"10.14","auto":"10.00","dense":"10.00","custom":""},{"low":"2.00","med":"2.40","high":"2.48","auto":"2.45","dense":"2.48","custom":""},{"low":"5.00","med":"16.10","high":"16.14","auto":"16.00","dense":"16.00","custom":""},{"low":"2.50","med":"2.90","high":"2.90","auto":"2.90","dense":"2.90","custom":""},{"low":"0.50","med":"0.60","high":"0.65","auto":"0.65","dense":"0.65","custom":""}]
}
58 changes: 58 additions & 0 deletions test/spec/cpmBucketManager_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect } from 'chai';
import {getPriceBucketString, isValidePriceConfig} from 'src/cpmBucketManager';
let cpmFixtures = require('test/fixtures/cpmInputsOutputs.json');

describe('cpmBucketManager', () => {

it('getPriceBucketString function generates the correct price strings', () => {
let input = cpmFixtures.cpmInputs;
for(let i = 0; i < input.length; i++){
let output = getPriceBucketString(input[i]);
let jsonOutput = JSON.stringify(output);
expect(jsonOutput).to.deep.equal(JSON.stringify(cpmFixtures.priceStringOutputs[i]));
}
});

it('gets the correct custom bucket strings', () => {
let cpm = 16.50908;
let customConfig = {
"buckets" : [{
"precision" : 4,
"min" : 0,
"max" : 3,
"increment" : 0.01,
},
{
"precision" : 4,
"min" : 3,
"max" : 18,
"increment" : 0.05,
"cap" : true
}
]
};
let expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"16.5000"}';
let output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});

it('checks whether custom config is valid', () => {
let badConfig = {
"buckets" : [{
"min" : 0,
"max" : 3,
"increment" : 0.01,
},
{
//missing min prop
"max" : 18,
"increment" : 0.05,
"cap" : true
}
]
};

expect(isValidePriceConfig(badConfig)).to.be.false;
});

});
Loading