forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpmBucketManager.js
123 lines (117 loc) · 2.64 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
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 };