forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpmBucketManager.js
140 lines (132 loc) · 3.91 KB
/
cpmBucketManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import find from 'core-js/library/fn/array/find';
const utils = require('./utils');
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, granularityMultiplier = 1) {
let cpmFloat = parseFloat(cpm);
if (isNaN(cpmFloat)) {
cpmFloat = '';
}
return {
low: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _lgPriceConfig, granularityMultiplier),
med: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _mgPriceConfig, granularityMultiplier),
high: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _hgPriceConfig, granularityMultiplier),
auto: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _autoPriceConfig, granularityMultiplier),
dense: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _densePriceConfig, granularityMultiplier),
custom: (cpmFloat === '') ? '' : getCpmStringValue(cpm, customConfig, granularityMultiplier)
};
}
function getCpmStringValue(cpm, config, granularityMultiplier) {
let cpmStr = '';
if (!isValidPriceConfig(config)) {
return cpmStr;
}
const cap = config.buckets.reduce((prev, curr) => {
if (prev.max > curr.max) {
return prev;
}
return curr;
}, {
'max': 0,
});
let bucket = find(config.buckets, bucket => {
if (cpm > cap.max * granularityMultiplier) {
// cpm exceeds cap, just return the cap.
let precision = bucket.precision;
if (typeof precision === 'undefined') {
precision = _defaultPrecision;
}
cpmStr = (bucket.max * granularityMultiplier).toFixed(precision);
} else if (cpm <= bucket.max * granularityMultiplier && cpm >= bucket.min * granularityMultiplier) {
return bucket;
}
});
if (bucket) {
cpmStr = getCpmTarget(cpm, bucket, granularityMultiplier);
}
return cpmStr;
}
function isValidPriceConfig(config) {
if (utils.isEmpty(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, bucket, granularityMultiplier) {
const precision = typeof bucket.precision !== 'undefined' ? bucket.precision : _defaultPrecision;
const increment = bucket.increment * granularityMultiplier;
const bucketMin = bucket.min * granularityMultiplier;
// start increments at the bucket min and then add bucket min back to arrive at the correct rounding
// note - we're padding the values to avoid using decimals in the math prior to flooring
// this is done as JS can return values slightly below the expected mark which would skew the price bucket target
// (eg 4.01 / 0.01 = 400.99999999999994)
// min precison should be 2 to move decimal place over.
let pow = Math.pow(10, precision + 2);
let cpmToFloor = ((cpm * pow) - (bucketMin * pow)) / (increment * pow);
let cpmTarget = ((Math.floor(cpmToFloor)) * increment) + bucketMin;
// force to 10 decimal places to deal with imprecise decimal/binary conversions
// (for example 0.1 * 3 = 0.30000000000000004)
cpmTarget = Number(cpmTarget.toFixed(10));
return cpmTarget.toFixed(precision);
}
export { getPriceBucketString, isValidPriceConfig };