-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mediasniperBidAdapter.js
318 lines (268 loc) · 7.25 KB
/
mediasniperBidAdapter.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import {
deepAccess,
deepClone,
deepSetValue, getBidIdParameter,
inIframe,
isArray,
isEmpty,
isFn,
isNumber,
isPlainObject,
isStr,
logError,
logMessage,
logWarn,
triggerPixel,
} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
const BIDDER_CODE = 'mediasniper';
const DEFAULT_BID_TTL = 360;
const DEFAULT_CURRENCY = 'RUB';
const DEFAULT_NET_REVENUE = true;
const ENDPOINT = 'https://sapi.bumlam.com/prebid/';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
isBidRequestValid: function (bid) {
logMessage('Hello!! bid: ', JSON.stringify(bid));
if (!bid || isEmpty(bid)) {
return false;
}
if (!bid.params || isEmpty(bid.params)) {
return false;
}
if (!isStr(bid.params.placementId) && !isNumber(bid.params.placementId)) {
return false;
}
const banner = deepAccess(bid, 'mediaTypes.banner', {});
if (!banner || isEmpty(banner)) {
return false;
}
const sizes = deepAccess(bid, 'mediaTypes.banner.sizes', []);
if (!isArray(sizes) || isEmpty(sizes)) {
return false;
}
return true;
},
buildRequests: function (validBidRequests, bidderRequest) {
const payload = createOrtbTemplate();
deepSetValue(payload, 'id', bidderRequest.bidderRequestId);
validBidRequests.forEach((validBid) => {
let bid = deepClone(validBid);
const imp = createImp(bid);
payload.imp.push(imp);
});
// params
const siteId = getBidIdParameter('siteid', validBidRequests[0].params) + '';
deepSetValue(payload, 'site.id', siteId);
// Assign payload.site from refererinfo
if (bidderRequest.refererInfo) {
// TODO: reachedTop is probably not the right check - it may be false when page is available or vice-versa
if (bidderRequest.refererInfo.reachedTop) {
const sitePage = bidderRequest.refererInfo.page;
deepSetValue(payload, 'site.page', sitePage);
deepSetValue(
payload,
'site.domain',
bidderRequest.refererInfo.domain
);
if (bidderRequest.refererInfo?.ref) {
deepSetValue(payload, 'site.ref', bidderRequest.refererInfo.ref);
}
}
}
const request = {
method: 'POST',
url: ENDPOINT,
data: JSON.stringify(payload),
};
return request;
},
interpretResponse(serverResponse, bidRequest) {
const bidResponses = [];
try {
if (
serverResponse.body &&
serverResponse.body.seatbid &&
isArray(serverResponse.body.seatbid)
) {
serverResponse.body.seatbid.forEach((bidderSeat) => {
if (!isArray(bidderSeat.bid) || !bidderSeat.bid.length) {
return;
}
bidderSeat.bid.forEach((bid) => {
const newBid = {
requestId: bid.impid,
cpm: bid.price || 0,
width: bid.w,
height: bid.h,
creativeId: bid.crid || bid.adid || bid.id,
dealId: bid.dealid || null,
currency: serverResponse.body.cur || DEFAULT_CURRENCY,
netRevenue: DEFAULT_NET_REVENUE,
ttl: DEFAULT_BID_TTL, // seconds. https://docs.prebid.org/dev-docs/faq.html#does-prebidjs-cache-bids
ad: bid.adm,
mediaType: BANNER,
burl: bid.nurl,
meta: {
advertiserDomains:
Array.isArray(bid.adomain) && bid.adomain.length
? bid.adomain
: [],
mediaType: BANNER,
},
};
logMessage('answer: ', JSON.stringify(newBid));
bidResponses.push(newBid);
});
});
}
} catch (e) {
logError(BIDDER_CODE, e);
}
return bidResponses;
},
onBidWon: function (bid) {
if (!bid.burl) {
return;
}
const url = bid.burl.replace(/\$\{AUCTION_PRICE\}/, bid.cpm);
triggerPixel(url);
},
};
registerBidder(spec);
/**
* Returns an openRTB 2.5 object.
* This one will be populated at each step of the buildRequest process.
*
* @returns {object}
*/
function createOrtbTemplate() {
return {
id: '',
cur: [DEFAULT_CURRENCY],
imp: [],
site: {},
device: {
ip: '',
js: 1,
ua: navigator.userAgent,
},
user: {},
};
}
/**
* Create the OpenRTB 2.5 imp object.
*
* @param {*} bid Prebid bid object from request
* @returns
*/
function createImp(bid) {
let placementId = '';
if (isStr(bid.params.placementId)) {
placementId = bid.params.placementId;
} else if (isNumber(bid.params.placementId)) {
placementId = bid.params.placementId.toString();
}
const imp = {
id: bid.bidId,
tagid: placementId,
bidfloorcur: DEFAULT_CURRENCY,
secure: 1,
};
// There is no default floor. bidfloor is set only
// if the priceFloors module is activated and returns a valid floor.
const floor = getMinFloor(bid);
if (isNumber(floor)) {
imp.bidfloor = floor;
}
// Only supports proper mediaTypes definition…
for (let mediaType in bid.mediaTypes) {
switch (mediaType) {
case BANNER:
imp.banner = createBannerImp(bid);
break;
}
}
// dealid
const dealId = getBidIdParameter('dealid', bid.params);
if (dealId) {
imp.pmp = {
private_auction: 1,
deals: [
{
id: dealId,
bidfloor: floor || 0,
bidfloorcur: DEFAULT_CURRENCY,
},
],
};
}
return imp;
}
/**
* Returns floor from priceFloors module or MediaKey default value.
*
* @param {*} bid a Prebid.js bid (request) object
* @param {string} mediaType the mediaType or the wildcard '*'
* @param {string|Array} size the size array or the wildcard '*'
* @returns {number|boolean}
*/
function getFloor(bid, mediaType, size = '*') {
if (!isFn(bid.getFloor)) {
return false;
}
if (spec.supportedMediaTypes.indexOf(mediaType) === -1) {
logWarn(
`${BIDDER_CODE}: Unable to detect floor price for unsupported mediaType ${mediaType}. No floor will be used.`
);
return false;
}
const floor = bid.getFloor({
currency: DEFAULT_CURRENCY,
mediaType,
size,
});
return isPlainObject(floor) && !isNaN(floor.floor) && floor.currency === DEFAULT_CURRENCY
? floor.floor
: false;
}
function getMinFloor(bid) {
const floors = [];
for (let mediaType in bid.mediaTypes) {
const floor = getFloor(bid, mediaType);
if (isNumber(floor)) {
floors.push(floor);
}
}
if (!floors.length) {
return false;
}
return floors.reduce((a, b) => {
return Math.min(a, b);
});
}
/**
* Returns an openRtb 2.5 banner object.
*
* @param {object} bid Prebid bid object from request
* @returns {object}
*/
function createBannerImp(bid) {
let sizes = bid.mediaTypes.banner.sizes;
const params = deepAccess(bid, 'params', {});
const banner = {};
banner.w = parseInt(sizes[0][0], 10);
banner.h = parseInt(sizes[0][1], 10);
const format = [];
sizes.forEach(function (size) {
if (size.length && size.length > 1) {
format.push({ w: size[0], h: size[1] });
}
});
banner.format = format;
banner.topframe = inIframe() ? 0 : 1;
banner.pos = params.pos || 0;
return banner;
}