forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaduptechBidAdapter.js
365 lines (307 loc) · 9.52 KB
/
aduptechBidAdapter.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import {deepClone, isArray, isBoolean, isEmpty, isFn, isPlainObject} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE} from '../src/mediaTypes.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
import {getAdUnitSizes} from '../libraries/sizeUtils/sizeUtils.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').BidderRequest} BidderRequest
*/
export const BIDDER_CODE = 'aduptech';
export const GVLID = 647;
export const ENDPOINT_URL_PUBLISHER_PLACEHOLDER = '{PUBLISHER}';
export const ENDPOINT_URL = 'https://rtb.d.adup-tech.com/prebid/' + ENDPOINT_URL_PUBLISHER_PLACEHOLDER + '_bid';
export const ENDPOINT_METHOD = 'POST';
/**
* Internal utitlity functions
*/
export const internal = {
/**
* Extracts the GDPR information from given bidderRequest
*
* @param {BidderRequest} bidderRequest
* @returns {null|Object.<string, string|boolean>}
*/
extractGdpr: (bidderRequest) => {
if (!bidderRequest?.gdprConsent) {
return null;
}
return {
consentString: bidderRequest.gdprConsent.consentString,
consentRequired: (isBoolean(bidderRequest.gdprConsent.gdprApplies)) ? bidderRequest.gdprConsent.gdprApplies : true
};
},
/**
* Extracts the pageUrl from given bidderRequest.refererInfo or gobal "pageUrl" config or from (top) window location
*
* @param {BidderRequest} bidderRequest
* @returns {string}
*/
extractPageUrl: (bidderRequest) => {
// TODO: does it make sense to fall back here?
return bidderRequest?.refererInfo?.page || window.location.href;
},
/**
* Extracts the referrer based on given bidderRequest.refererInfo or from (top) document referrer
*
* @param {BidderRequest} bidderRequest
* @returns {string}
*/
extractReferrer: (bidderRequest) => {
// TODO: does it make sense to fall back here?
return bidderRequest?.refererInfo?.ref || window.document.referrer;
},
/**
* Extracts banner config from given bidRequest
*
* @param {BidRequest} bidRequest
* @returns {null|Object.<string, *>}
*/
extractBannerConfig: (bidRequest) => {
const adUnitSizes = getAdUnitSizes(bidRequest);
if (!isArray(adUnitSizes) || isEmpty(adUnitSizes)) {
return null;
}
const banner = { sizes: [] };
adUnitSizes.forEach(adUnitSize => {
const size = deepClone(adUnitSize);
// try to add floor for each banner size
const floor = internal.getFloor(bidRequest, { mediaType: BANNER, size: adUnitSize });
if (floor) {
size.push(floor.floor);
size.push(floor.currency);
}
banner.sizes.push(size);
});
// try to add default floor for banner
const floor = internal.getFloor(bidRequest, { mediaType: BANNER, size: '*' });
if (floor) {
banner.floorPrice = floor.floor;
banner.floorCurrency = floor.currency;
}
return banner;
},
/**
* Extracts native config from given bidRequest
*
* @param {BidRequest} bidRequest
* @returns {null|Object.<string, *>}
*/
extractNativeConfig: (bidRequest) => {
if (!bidRequest?.mediaTypes?.native) {
return null;
}
const native = deepClone(bidRequest.mediaTypes.native);
// try to add default floor for native
const floor = internal.getFloor(bidRequest, { mediaType: NATIVE, size: '*' });
if (floor) {
native.floorPrice = floor.floor;
native.floorCurrency = floor.currency;
}
return native;
},
/**
* Extracts the bidder params from given bidRequest
*
* @param {BidRequest} bidRequest
* @returns {null|Object.<string, *>}
*/
extractParams: (bidRequest) => {
if (!bidRequest?.params) {
return null;
}
return deepClone(bidRequest.params);
},
/**
* Try to get floor information via bidRequest.getFloor()
*
* @param {BidRequest} bidRequest
* @param {Object<string, *>} options
* @returns {null|Object.<string, *>}
*/
getFloor: (bidRequest, options) => {
if (!isFn(bidRequest?.getFloor)) {
return null;
}
try {
const floor = bidRequest.getFloor(options);
if (isPlainObject(floor) && !isNaN(floor.floor)) {
return floor;
}
} catch {}
return null;
},
/**
* Group given array of bidRequests by params.publisher
*
* @param {BidRequest[]} bidRequests
* @returns {Object.<string, BidRequest>}
*/
groupBidRequestsByPublisher: (bidRequests) => {
const groupedBidRequests = {};
if (!bidRequests || isEmpty(bidRequests)) {
return groupedBidRequests;
}
bidRequests.forEach(bidRequest => {
const publisher = internal.extractParams(bidRequest).publisher;
if (!publisher) {
return;
}
if (!groupedBidRequests[publisher]) {
groupedBidRequests[publisher] = [];
}
groupedBidRequests[publisher].push(bidRequest);
});
return groupedBidRequests;
},
/**
* Build ednpoint url based on given publisher code
*
* @param {string} publisher
* @returns {string}
*/
buildEndpointUrl: (publisher) => {
return ENDPOINT_URL.replace(ENDPOINT_URL_PUBLISHER_PLACEHOLDER, encodeURIComponent(publisher));
},
}
/**
* The bid adapter definition
*/
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, NATIVE],
gvlid: GVLID,
/**
* Validate given bid request
*
* @param {BidRequest[]} bidRequest
* @returns {boolean}
*/
isBidRequestValid: (bidRequest) => {
if (!bidRequest) {
return false;
}
// banner or native config has to be set
if (!internal.extractBannerConfig(bidRequest) && !internal.extractNativeConfig(bidRequest)) {
return false;
}
// publisher and placement param has to be set
const params = internal.extractParams(bidRequest);
if (!params || !params.publisher || !params.placement) {
return false;
}
return true;
},
/**
* Build real bid requests
*
* @param {BidRequest[]} validBidRequests
* @param {BidderRequest} bidderRequest
* @returns {Object[]}
*/
buildRequests: (validBidRequests, bidderRequest) => {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
const requests = [];
// stop here on invalid or empty data
if (!bidderRequest || !validBidRequests || isEmpty(validBidRequests)) {
return requests;
}
// collect required data
const auctionId = bidderRequest.auctionId;
const pageUrl = internal.extractPageUrl(bidderRequest);
const referrer = internal.extractReferrer(bidderRequest);
const gdpr = internal.extractGdpr(bidderRequest);
// group bid requests by publisher
const groupedBidRequests = internal.groupBidRequestsByPublisher(validBidRequests);
// build requests
for (const publisher in groupedBidRequests) {
const request = {
url: internal.buildEndpointUrl(publisher),
method: ENDPOINT_METHOD,
data: {
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
auctionId: auctionId,
pageUrl: pageUrl,
referrer: referrer,
imp: []
}
};
// add gdpr data
if (gdpr) {
request.data.gdpr = gdpr;
}
// handle multiple bids per request
groupedBidRequests[publisher].forEach(bidRequest => {
const bid = {
bidId: bidRequest.bidId,
transactionId: bidRequest.ortb2Imp?.ext?.tid,
adUnitCode: bidRequest.adUnitCode,
params: internal.extractParams(bidRequest)
};
// add banner config
const bannerConfig = internal.extractBannerConfig(bidRequest);
if (bannerConfig) {
bid.banner = bannerConfig;
}
// add native config
const nativeConfig = internal.extractNativeConfig(bidRequest);
if (nativeConfig) {
bid.native = nativeConfig;
}
// try to add default floor
const floor = internal.getFloor(bidRequest, { mediaType: '*', size: '*' });
if (floor) {
bid.floorPrice = floor.floor;
bid.floorCurrency = floor.currency;
}
request.data.imp.push(bid);
});
requests.push(request);
}
return requests;
},
/**
* Handle bid response
*
* @param {Object} response
* @returns {Object[]}
*/
interpretResponse: (response) => {
const bidResponses = [];
// stop here on invalid or empty data
if (!response?.body?.bids || isEmpty(response.body.bids)) {
return bidResponses;
}
// parse multiple bids per response
response.body.bids.forEach(bid => {
if (!bid || !bid.bid || !bid.creative) {
return;
}
const bidResponse = {
requestId: bid.bid.bidId,
cpm: bid.bid.price,
netRevenue: bid.bid.net,
currency: bid.bid.currency,
ttl: bid.bid.ttl,
creativeId: bid.creative.id,
meta: {
advertiserDomains: bid.creative.advertiserDomains
}
}
if (bid.creative.html) {
bidResponse.mediaType = BANNER;
bidResponse.ad = bid.creative.html;
bidResponse.width = bid.creative.width;
bidResponse.height = bid.creative.height;
}
if (bid.creative.native) {
bidResponse.mediaType = NATIVE;
bidResponse.native = bid.creative.native;
}
bidResponses.push(bidResponse);
});
return bidResponses;
}
};
registerBidder(spec);