forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapterManager.js
498 lines (434 loc) · 17.4 KB
/
adapterManager.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/** @module adaptermanger */
import { flatten, getBidderCodes, getDefinedParams, shuffle, timestamp, getBidderRequest } from './utils';
import { getLabels, resolveStatus } from './sizeMapping';
import { processNativeAdUnitParams, nativeAdapters } from './native';
import { newBidder } from './adapters/bidderFactory';
import { ajaxBuilder } from './ajax';
import { config, RANDOM } from './config';
import includes from 'core-js/library/fn/array/includes';
import find from 'core-js/library/fn/array/find';
import { adunitCounter } from './adUnits';
import { getRefererInfo } from './refererDetection';
var utils = require('./utils.js');
var CONSTANTS = require('./constants.json');
var events = require('./events');
let s2sTestingModule; // store s2sTesting module if it's loaded
let adapterManager = {};
let _bidderRegistry = adapterManager.bidderRegistry = {};
let _aliasRegistry = adapterManager.aliasRegistry = {};
let _s2sConfig = {};
config.getConfig('s2sConfig', config => {
_s2sConfig = config.s2sConfig;
});
var _analyticsRegistry = {};
/**
* @typedef {object} LabelDescriptor
* @property {boolean} labelAll describes whether or not this object expects all labels to match, or any label to match
* @property {Array<string>} labels the labels listed on the bidder or adUnit
* @property {Array<string>} activeLabels the labels specified as being active by requestBids
*/
function getBids({bidderCode, auctionId, bidderRequestId, adUnits, labels, src}) {
return adUnits.reduce((result, adUnit) => {
let {
active,
mediaTypes: filteredMediaTypes,
filterResults
} = resolveStatus(
getLabels(adUnit, labels),
adUnit.mediaTypes,
adUnit.sizes
);
if (!active) {
utils.logInfo(`Size mapping disabled adUnit "${adUnit.code}"`);
} else if (filterResults) {
utils.logInfo(`Size mapping filtered adUnit "${adUnit.code}" banner sizes from `, filterResults.before, 'to ', filterResults.after);
}
if (active) {
result.push(adUnit.bids.filter(bid => bid.bidder === bidderCode)
.reduce((bids, bid) => {
const nativeParams =
adUnit.nativeParams || utils.deepAccess(adUnit, 'mediaTypes.native');
if (nativeParams) {
bid = Object.assign({}, bid, {
nativeParams: processNativeAdUnitParams(nativeParams),
});
}
bid = Object.assign({}, bid, getDefinedParams(adUnit, [
'mediaType',
'renderer'
]));
let {
active,
mediaTypes,
filterResults
} = resolveStatus(getLabels(bid, labels), filteredMediaTypes);
if (!active) {
utils.logInfo(`Size mapping deactivated adUnit "${adUnit.code}" bidder "${bid.bidder}"`);
} else if (filterResults) {
utils.logInfo(`Size mapping filtered adUnit "${adUnit.code}" bidder "${bid.bidder}" banner sizes from `, filterResults.before, 'to ', filterResults.after);
}
if (utils.isValidMediaTypes(mediaTypes)) {
bid = Object.assign({}, bid, {
mediaTypes
});
} else {
utils.logError(
`mediaTypes is not correctly configured for adunit ${adUnit.code}`
);
}
if (active) {
bids.push(Object.assign({}, bid, {
adUnitCode: adUnit.code,
transactionId: adUnit.transactionId,
sizes: utils.deepAccess(mediaTypes, 'banner.sizes') || utils.deepAccess(mediaTypes, 'video.playerSize') || [],
bidId: bid.bid_id || utils.getUniqueIdentifierStr(),
bidderRequestId,
auctionId,
src,
bidRequestsCount: adunitCounter.getCounter(adUnit.code),
}));
}
return bids;
}, [])
);
}
return result;
}, []).reduce(flatten, []).filter(val => val !== '');
}
function getAdUnitCopyForPrebidServer(adUnits) {
let adaptersServerSide = _s2sConfig.bidders;
let adUnitsCopy = utils.deepClone(adUnits);
adUnitsCopy.forEach((adUnit) => {
// filter out client side bids
adUnit.bids = adUnit.bids.filter((bid) => {
return includes(adaptersServerSide, bid.bidder) &&
(!doingS2STesting() || bid.finalSource !== s2sTestingModule.CLIENT);
}).map((bid) => {
bid.bid_id = utils.getUniqueIdentifierStr();
return bid;
});
});
// don't send empty requests
adUnitsCopy = adUnitsCopy.filter(adUnit => {
return adUnit.bids.length !== 0;
});
return adUnitsCopy;
}
function getAdUnitCopyForClientAdapters(adUnits) {
let adUnitsClientCopy = utils.deepClone(adUnits);
// filter out s2s bids
adUnitsClientCopy.forEach((adUnit) => {
adUnit.bids = adUnit.bids.filter((bid) => {
return !doingS2STesting() || bid.finalSource !== s2sTestingModule.SERVER;
})
});
// don't send empty requests
adUnitsClientCopy = adUnitsClientCopy.filter(adUnit => {
return adUnit.bids.length !== 0;
});
return adUnitsClientCopy;
}
export let gdprDataHandler = {
consentData: null,
setConsentData: function(consentInfo) {
gdprDataHandler.consentData = consentInfo;
},
getConsentData: function() {
return gdprDataHandler.consentData;
}
};
adapterManager.makeBidRequests = function(adUnits, auctionStart, auctionId, cbTimeout, labels) {
let bidRequests = [];
let bidderCodes = getBidderCodes(adUnits);
if (config.getConfig('bidderSequence') === RANDOM) {
bidderCodes = shuffle(bidderCodes);
}
const refererInfo = getRefererInfo();
let clientBidderCodes = bidderCodes;
let clientTestAdapters = [];
if (_s2sConfig.enabled) {
// if s2sConfig.bidderControl testing is turned on
if (doingS2STesting()) {
// get all adapters doing client testing
const bidderMap = s2sTestingModule.getSourceBidderMap(adUnits);
clientTestAdapters = bidderMap[s2sTestingModule.CLIENT];
}
// these are called on the s2s adapter
let adaptersServerSide = _s2sConfig.bidders;
// don't call these client side (unless client request is needed for testing)
clientBidderCodes = bidderCodes.filter(elm =>
!includes(adaptersServerSide, elm) || includes(clientTestAdapters, elm)
);
const adUnitsContainServerRequests = adUnits => Boolean(
find(adUnits, adUnit => find(adUnit.bids, bid => (
bid.bidSource ||
(_s2sConfig.bidderControl && _s2sConfig.bidderControl[bid.bidder])
) && bid.finalSource === s2sTestingModule.SERVER))
);
if (isTestingServerOnly() && adUnitsContainServerRequests(adUnits)) {
clientBidderCodes.length = 0;
}
let adUnitsS2SCopy = getAdUnitCopyForPrebidServer(adUnits);
let tid = utils.generateUUID();
adaptersServerSide.forEach(bidderCode => {
const bidderRequestId = utils.getUniqueIdentifierStr();
const bidderRequest = {
bidderCode,
auctionId,
bidderRequestId,
tid,
bids: getBids({bidderCode, auctionId, bidderRequestId, 'adUnits': utils.deepClone(adUnitsS2SCopy), labels, src: CONSTANTS.S2S.SRC}),
auctionStart: auctionStart,
timeout: _s2sConfig.timeout,
src: CONSTANTS.S2S.SRC,
refererInfo
};
if (bidderRequest.bids.length !== 0) {
bidRequests.push(bidderRequest);
}
});
// update the s2sAdUnits object and remove all bids that didn't pass sizeConfig/label checks from getBids()
// this is to keep consistency and only allow bids/adunits that passed the checks to go to pbs
adUnitsS2SCopy.forEach((adUnitCopy) => {
let validBids = adUnitCopy.bids.filter((adUnitBid) => {
return find(bidRequests, request => {
return find(request.bids, (reqBid) => reqBid.bidId === adUnitBid.bid_id);
});
});
adUnitCopy.bids = validBids;
});
bidRequests.forEach(request => {
request.adUnitsS2SCopy = adUnitsS2SCopy.filter(adUnitCopy => adUnitCopy.bids.length > 0);
});
}
// client adapters
let adUnitsClientCopy = getAdUnitCopyForClientAdapters(adUnits);
clientBidderCodes.forEach(bidderCode => {
const bidderRequestId = utils.getUniqueIdentifierStr();
const bidderRequest = {
bidderCode,
auctionId,
bidderRequestId,
bids: getBids({bidderCode, auctionId, bidderRequestId, 'adUnits': utils.deepClone(adUnitsClientCopy), labels, src: 'client'}),
auctionStart: auctionStart,
timeout: cbTimeout,
refererInfo
};
const adapter = _bidderRegistry[bidderCode];
if (!adapter) {
utils.logError(`Trying to make a request for bidder that does not exist: ${bidderCode}`);
}
if (adapter && bidderRequest.bids && bidderRequest.bids.length !== 0) {
bidRequests.push(bidderRequest);
}
});
if (gdprDataHandler.getConsentData()) {
bidRequests.forEach(bidRequest => {
bidRequest['gdprConsent'] = gdprDataHandler.getConsentData();
});
}
return bidRequests;
};
adapterManager.callBids = (adUnits, bidRequests, addBidResponse, doneCb, requestCallbacks, requestBidsTimeout, onTimelyResponse) => {
if (!bidRequests.length) {
utils.logWarn('callBids executed with no bidRequests. Were they filtered by labels or sizing?');
return;
}
let [clientBidRequests, serverBidRequests] = bidRequests.reduce((partitions, bidRequest) => {
partitions[Number(typeof bidRequest.src !== 'undefined' && bidRequest.src === CONSTANTS.S2S.SRC)].push(bidRequest);
return partitions;
}, [[], []]);
if (serverBidRequests.length) {
// s2s should get the same client side timeout as other client side requests.
const s2sAjax = ajaxBuilder(requestBidsTimeout, requestCallbacks ? {
request: requestCallbacks.request.bind(null, 's2s'),
done: requestCallbacks.done
} : undefined);
let adaptersServerSide = _s2sConfig.bidders;
const s2sAdapter = _bidderRegistry[_s2sConfig.adapter];
let tid = serverBidRequests[0].tid;
let adUnitsS2SCopy = serverBidRequests[0].adUnitsS2SCopy;
if (s2sAdapter) {
let s2sBidRequest = {tid, 'ad_units': adUnitsS2SCopy};
if (s2sBidRequest.ad_units.length) {
let doneCbs = serverBidRequests.map(bidRequest => {
bidRequest.start = timestamp();
return doneCb.bind(bidRequest);
});
// only log adapters that actually have adUnit bids
let allBidders = s2sBidRequest.ad_units.reduce((adapters, adUnit) => {
return adapters.concat((adUnit.bids || []).reduce((adapters, bid) => { return adapters.concat(bid.bidder) }, []));
}, []);
utils.logMessage(`CALLING S2S HEADER BIDDERS ==== ${adaptersServerSide.filter(adapter => {
return includes(allBidders, adapter);
}).join(',')}`);
// fire BID_REQUESTED event for each s2s bidRequest
serverBidRequests.forEach(bidRequest => {
events.emit(CONSTANTS.EVENTS.BID_REQUESTED, bidRequest);
});
// make bid requests
s2sAdapter.callBids(
s2sBidRequest,
serverBidRequests,
function(adUnitCode, bid) {
let bidderRequest = getBidderRequest(serverBidRequests, bid.bidderCode, adUnitCode);
if (bidderRequest) {
addBidResponse.call(bidderRequest, adUnitCode, bid)
}
},
() => doneCbs.forEach(done => done()),
s2sAjax
);
}
}
}
// handle client adapter requests
clientBidRequests.forEach(bidRequest => {
bidRequest.start = timestamp();
// TODO : Do we check for bid in pool from here and skip calling adapter again ?
const adapter = _bidderRegistry[bidRequest.bidderCode];
utils.logMessage(`CALLING BIDDER ======= ${bidRequest.bidderCode}`);
events.emit(CONSTANTS.EVENTS.BID_REQUESTED, bidRequest);
let ajax = ajaxBuilder(requestBidsTimeout, requestCallbacks ? {
request: requestCallbacks.request.bind(null, bidRequest.bidderCode),
done: requestCallbacks.done
} : undefined);
adapter.callBids(bidRequest, addBidResponse.bind(bidRequest), doneCb.bind(bidRequest), ajax, onTimelyResponse);
});
}
function doingS2STesting() {
return _s2sConfig && _s2sConfig.enabled && _s2sConfig.testing && s2sTestingModule;
}
function isTestingServerOnly() {
return Boolean(doingS2STesting() && _s2sConfig.testServerOnly);
};
function getSupportedMediaTypes(bidderCode) {
let result = [];
if (includes(adapterManager.videoAdapters, bidderCode)) result.push('video');
if (includes(nativeAdapters, bidderCode)) result.push('native');
return result;
}
adapterManager.videoAdapters = []; // added by adapterLoader for now
adapterManager.registerBidAdapter = function (bidAdaptor, bidderCode, {supportedMediaTypes = []} = {}) {
if (bidAdaptor && bidderCode) {
if (typeof bidAdaptor.callBids === 'function') {
_bidderRegistry[bidderCode] = bidAdaptor;
if (includes(supportedMediaTypes, 'video')) {
adapterManager.videoAdapters.push(bidderCode);
}
if (includes(supportedMediaTypes, 'native')) {
nativeAdapters.push(bidderCode);
}
} else {
utils.logError('Bidder adaptor error for bidder code: ' + bidderCode + 'bidder must implement a callBids() function');
}
} else {
utils.logError('bidAdaptor or bidderCode not specified');
}
};
adapterManager.aliasBidAdapter = function (bidderCode, alias) {
let existingAlias = _bidderRegistry[alias];
if (typeof existingAlias === 'undefined') {
let bidAdaptor = _bidderRegistry[bidderCode];
if (typeof bidAdaptor === 'undefined') {
// check if alias is part of s2sConfig and allow them to register if so (as base bidder may be s2s-only)
const s2sConfig = config.getConfig('s2sConfig');
const s2sBidders = s2sConfig && s2sConfig.bidders;
if (!(s2sBidders && includes(s2sBidders, alias))) {
utils.logError('bidderCode "' + bidderCode + '" is not an existing bidder.', 'adapterManager.aliasBidAdapter');
} else {
_aliasRegistry[alias] = bidderCode;
}
} else {
try {
let newAdapter;
let supportedMediaTypes = getSupportedMediaTypes(bidderCode);
// Have kept old code to support backward compatibilitiy.
// Remove this if loop when all adapters are supporting bidderFactory. i.e When Prebid.js is 1.0
if (bidAdaptor.constructor.prototype != Object.prototype) {
newAdapter = new bidAdaptor.constructor();
newAdapter.setBidderCode(alias);
} else {
let spec = bidAdaptor.getSpec();
newAdapter = newBidder(Object.assign({}, spec, { code: alias }));
_aliasRegistry[alias] = bidderCode;
}
adapterManager.registerBidAdapter(newAdapter, alias, {
supportedMediaTypes
});
} catch (e) {
utils.logError(bidderCode + ' bidder does not currently support aliasing.', 'adapterManager.aliasBidAdapter');
}
}
} else {
utils.logMessage('alias name "' + alias + '" has been already specified.');
}
};
adapterManager.registerAnalyticsAdapter = function ({adapter, code}) {
if (adapter && code) {
if (typeof adapter.enableAnalytics === 'function') {
adapter.code = code;
_analyticsRegistry[code] = adapter;
} else {
utils.logError(`Prebid Error: Analytics adaptor error for analytics "${code}"
analytics adapter must implement an enableAnalytics() function`);
}
} else {
utils.logError('Prebid Error: analyticsAdapter or analyticsCode not specified');
}
};
adapterManager.enableAnalytics = function (config) {
if (!utils.isArray(config)) {
config = [config];
}
utils._each(config, adapterConfig => {
var adapter = _analyticsRegistry[adapterConfig.provider];
if (adapter) {
adapter.enableAnalytics(adapterConfig);
} else {
utils.logError(`Prebid Error: no analytics adapter found in registry for
${adapterConfig.provider}.`);
}
});
};
adapterManager.getBidAdapter = function(bidder) {
return _bidderRegistry[bidder];
};
// the s2sTesting module is injected when it's loaded rather than being imported
// importing it causes the packager to include it even when it's not explicitly included in the build
export function setS2STestingModule(module) {
s2sTestingModule = module;
}
function tryCallBidderMethod(bidder, method, param) {
try {
const adapter = _bidderRegistry[bidder];
const spec = adapter.getSpec();
if (spec && spec[method] && typeof spec[method] === 'function') {
utils.logInfo(`Invoking ${bidder}.${method}`);
spec[method](param);
}
} catch (e) {
utils.logWarn(`Error calling ${method} of ${bidder}`);
}
}
adapterManager.callTimedOutBidders = function(adUnits, timedOutBidders, cbTimeout) {
timedOutBidders = timedOutBidders.map((timedOutBidder) => {
// Adding user configured params & timeout to timeout event data
timedOutBidder.params = utils.getUserConfiguredParams(adUnits, timedOutBidder.adUnitCode, timedOutBidder.bidder);
timedOutBidder.timeout = cbTimeout;
return timedOutBidder;
});
timedOutBidders = utils.groupBy(timedOutBidders, 'bidder');
Object.keys(timedOutBidders).forEach((bidder) => {
tryCallBidderMethod(bidder, 'onTimeout', timedOutBidders[bidder]);
});
}
adapterManager.callBidWonBidder = function(bidder, bid, adUnits) {
// Adding user configured params to bidWon event data
bid.params = utils.getUserConfiguredParams(adUnits, bid.adUnitCode, bid.bidder);
tryCallBidderMethod(bidder, 'onBidWon', bid);
};
adapterManager.callSetTargetingBidder = function(bidder, bid) {
tryCallBidderMethod(bidder, 'onSetTargeting', bid);
};
export default adapterManager;