-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtripleliftBidAdapter.js
420 lines (350 loc) · 11.2 KB
/
tripleliftBidAdapter.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
import * as utils from '../src/utils.js';
import { logMessage, logError, isEmpty, logWarn } from '../src/utils.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { getStorageManager } from '../src/storageManager.js';
import {tryAppendQueryString} from '../libraries/urlUtils/urlUtils.js';
const GVLID = 28;
const BIDDER_CODE = 'triplelift';
const STR_ENDPOINT = 'https://tlx.3lift.com/header/auction?';
const BANNER_TIME_TO_LIVE = 300;
const VIDEO_TIME_TO_LIVE = 3600;
let gdprApplies = null;
let consentString = null;
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const tripleliftAdapterSpec = {
gvlid: GVLID,
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid: function (bid) {
return typeof bid.params.inventoryCode !== 'undefined';
},
buildRequests: function(bidRequests, bidderRequest) {
let tlCall = STR_ENDPOINT;
let data = _buildPostBody(bidRequests, bidderRequest);
tlCall = tryAppendQueryString(tlCall, 'lib', 'prebid');
tlCall = tryAppendQueryString(tlCall, 'v', '$prebid.version$');
if (bidderRequest && bidderRequest.refererInfo) {
let referrer = bidderRequest.refererInfo.page;
tlCall = tryAppendQueryString(tlCall, 'referrer', referrer);
}
if (bidderRequest && bidderRequest.timeout) {
tlCall = tryAppendQueryString(tlCall, 'tmax', bidderRequest.timeout);
}
if (bidderRequest && bidderRequest.gdprConsent) {
if (typeof bidderRequest.gdprConsent.gdprApplies !== 'undefined') {
gdprApplies = bidderRequest.gdprConsent.gdprApplies;
} else {
gdprApplies = true;
}
tlCall = tryAppendQueryString(tlCall, 'gdpr', gdprApplies.toString());
if (typeof bidderRequest.gdprConsent.consentString !== 'undefined') {
consentString = bidderRequest.gdprConsent.consentString;
tlCall = tryAppendQueryString(tlCall, 'cmp_cs', consentString);
}
}
if (bidderRequest && bidderRequest.uspConsent) {
tlCall = tryAppendQueryString(tlCall, 'us_privacy', bidderRequest.uspConsent);
}
if (bidderRequest?.paapi?.enabled) {
tlCall = tryAppendQueryString(tlCall, 'fledge', bidderRequest.paapi.enabled);
}
if (config.getConfig('coppa') === true) {
tlCall = tryAppendQueryString(tlCall, 'coppa', true);
}
if (tlCall.lastIndexOf('&') === tlCall.length - 1) {
tlCall = tlCall.substring(0, tlCall.length - 1);
}
logMessage('tlCall request built: ' + tlCall);
return {
method: 'POST',
url: tlCall,
data,
bidderRequest
};
},
interpretResponse: function(serverResponse, {bidderRequest}) {
let bids = serverResponse.body.bids || [];
const paapi = serverResponse.body.paapi || [];
bids = bids.map(bid => _buildResponseObject(bidderRequest, bid));
if (paapi.length > 0) {
const fledgeAuctionConfigs = paapi.map(config => {
return {
bidId: bidderRequest.bids[config.imp_id].bidId,
config: config.auctionConfig
};
});
logMessage('Response with FLEDGE:', { bids, fledgeAuctionConfigs });
return {
bids,
paapi: fledgeAuctionConfigs
};
} else {
return bids;
}
},
getUserSyncs: function(syncOptions, responses, gdprConsent, usPrivacy, gppConsent) {
let syncType = _getSyncType(syncOptions);
if (!syncType) return;
let syncEndpoint = 'https://eb2.3lift.com/sync?';
if (syncType === 'image') {
syncEndpoint = tryAppendQueryString(syncEndpoint, 'px', 1);
syncEndpoint = tryAppendQueryString(syncEndpoint, 'src', 'prebid');
}
if (consentString !== null || gdprApplies) {
syncEndpoint = tryAppendQueryString(syncEndpoint, 'gdpr', gdprApplies);
syncEndpoint = tryAppendQueryString(syncEndpoint, 'cmp_cs', consentString);
}
if (usPrivacy) {
syncEndpoint = tryAppendQueryString(syncEndpoint, 'us_privacy', usPrivacy);
}
if (gppConsent) {
if (gppConsent.gppString) {
syncEndpoint = tryAppendQueryString(syncEndpoint, 'gpp', gppConsent.gppString);
}
if (gppConsent.applicableSections && gppConsent.applicableSections.length !== 0) {
syncEndpoint = tryAppendQueryString(syncEndpoint, 'gpp_sid', _filterSid(gppConsent.applicableSections));
}
}
return [{
type: syncType,
url: syncEndpoint
}];
}
};
function _getSyncType(syncOptions) {
if (!syncOptions) return;
if (syncOptions.iframeEnabled) return 'iframe';
if (syncOptions.pixelEnabled) return 'image';
}
function _filterSid(sid) {
return sid.filter(element => {
return Number.isInteger(element);
})
.join(',');
}
function _buildPostBody(bidRequests, bidderRequest) {
let data = {};
let { schain } = bidRequests[0];
const globalFpd = _getGlobalFpd(bidderRequest);
data.imp = bidRequests.map(function(bidRequest, index) {
let imp = {
id: index,
tagid: bidRequest.params.inventoryCode,
floor: _getFloor(bidRequest)
};
// Check for video bidrequest
if (_isVideoBidRequest(bidRequest)) {
imp.video = _getORTBVideo(bidRequest);
}
// append banner if applicable and request is not for instream
if (bidRequest.mediaTypes.banner && !_isInstream(bidRequest)) {
imp.banner = { format: _sizes(bidRequest.sizes) };
}
if (!isEmpty(bidRequest.ortb2Imp)) {
// legacy method for extracting ortb2Imp.ext
imp.fpd = _getAdUnitFpd(bidRequest.ortb2Imp);
// preferred method for extracting ortb2Imp.ext
if (!isEmpty(bidRequest.ortb2Imp.ext)) {
imp.ext = { ...bidRequest.ortb2Imp.ext };
}
}
return imp;
});
let eids = [];
if (bidRequests[0].userIdAsEids) {
eids = utils.deepAccess(bidRequests[0], 'userIdAsEids');
data.user = {
ext: { eids }
};
}
let ext = _getExt(schain, globalFpd);
if (!isEmpty(ext)) {
data.ext = ext;
}
if (bidderRequest?.ortb2?.regs?.gpp) {
data.regs = Object.assign({}, bidderRequest.ortb2.regs);
}
if (bidderRequest?.ortb2) {
data.ext.ortb2 = Object.assign({}, bidderRequest.ortb2);
}
return data;
}
function _isVideoBidRequest(bidRequest) {
return _isValidVideoObject(bidRequest) && (_isInstream(bidRequest) || _isOutstream(bidRequest));
}
function _isOutstream(bidRequest) {
return _isValidVideoObject(bidRequest) && bidRequest.mediaTypes.video.context.toLowerCase() === 'outstream';
}
function _isInstream(bidRequest) {
return _isValidVideoObject(bidRequest) && bidRequest.mediaTypes.video.context.toLowerCase() === 'instream';
}
function _isValidVideoObject(bidRequest) {
return bidRequest.mediaTypes.video && bidRequest.mediaTypes.video.context;
}
function _getORTBVideo(bidRequest) {
// give precedent to mediaTypes.video
let video = { ...bidRequest.params.video, ...bidRequest.mediaTypes.video };
try {
if (!video.w) video.w = video.playerSize[0][0];
if (!video.h) video.h = video.playerSize[0][1];
} catch (err) {
logWarn('Video size not defined', err);
}
if (video.playbackmethod && Number.isInteger(video.playbackmethod)) {
video.playbackmethod = Array.from(String(video.playbackmethod), Number);
}
// clean up oRTB object
delete video.playerSize;
return video;
}
function _getFloor (bid) {
let floor = null;
if (typeof bid.getFloor === 'function') {
try {
const floorInfo = bid.getFloor({
currency: 'USD',
mediaType: _isVideoBidRequest(bid) ? 'video' : 'banner',
size: '*'
});
if (utils.isPlainObject(floorInfo) &&
floorInfo.currency === 'USD' && !isNaN(parseFloat(floorInfo.floor))) {
floor = parseFloat(floorInfo.floor);
}
} catch (err) {
logError('Triplelift: getFloor threw an error: ', err);
}
}
return floor !== null ? floor : bid.params.floor;
}
function _getGlobalFpd(bidderRequest) {
const fpd = {};
const context = {}
const user = {};
const ortbData = bidderRequest.ortb2 || {};
const opeCloudStorage = _fetchOpeCloud();
const fpdContext = Object.assign({}, ortbData.site);
const fpdUser = Object.assign({}, ortbData.user);
if (opeCloudStorage) {
fpdUser.data = fpdUser.data || []
try {
fpdUser.data.push({
name: 'www.1plusx.com',
ext: opeCloudStorage
})
} catch (err) {
logError('Triplelift: error adding 1plusX segments: ', err);
}
}
_addEntries(context, fpdContext);
_addEntries(user, fpdUser);
if (!isEmpty(context)) {
fpd.context = context;
}
if (!isEmpty(user)) {
fpd.user = user;
}
return fpd;
}
function _fetchOpeCloud() {
const opeCloud = storage.getDataFromLocalStorage('opecloud_ctx');
if (!opeCloud) return null;
try {
const parsedJson = JSON.parse(opeCloud);
return parsedJson
} catch (err) {
logError('Triplelift: error parsing JSON: ', err);
return null
}
}
function _getAdUnitFpd(adUnitFpd) {
const fpd = {};
const context = {};
_addEntries(context, adUnitFpd.ext);
if (!isEmpty(context)) {
fpd.context = context;
}
return fpd;
}
function _addEntries(target, source) {
if (!isEmpty(source)) {
Object.keys(source).forEach(key => {
if (source[key] != null) {
target[key] = source[key];
}
});
}
}
function _getExt(schain, fpd) {
let ext = {};
if (!isEmpty(schain)) {
ext.schain = { ...schain };
}
if (!isEmpty(fpd)) {
ext.fpd = { ...fpd };
}
return ext;
}
function _sizes(sizeArray) {
let sizes = sizeArray.filter(_isValidSize);
return sizes.map(function(size) {
return {
w: size[0],
h: size[1]
};
});
}
function _isValidSize(size) {
return (size.length === 2 && typeof size[0] === 'number' && typeof size[1] === 'number');
}
function _buildResponseObject(bidderRequest, bid) {
let bidResponse = {};
let width = bid.width || 1;
let height = bid.height || 1;
let dealId = bid.deal_id || '';
let creativeId = bid.crid || '';
let breq = bidderRequest.bids[bid.imp_id];
if (bid.cpm != 0 && bid.ad) {
bidResponse = {
requestId: breq.bidId,
cpm: bid.cpm,
width: width,
height: height,
netRevenue: true,
ad: bid.ad,
creativeId: creativeId,
dealId: dealId,
currency: 'USD',
ttl: BANNER_TIME_TO_LIVE,
tl_source: bid.tl_source,
meta: {}
};
if (_isVideoBidRequest(breq) && bid.media_type === 'video') {
bidResponse.vastXml = bid.ad;
bidResponse.mediaType = 'video';
bidResponse.ttl = VIDEO_TIME_TO_LIVE;
};
if (bid.advertiser_name) {
bidResponse.meta.advertiserName = bid.advertiser_name;
}
if (bid.adomain && bid.adomain.length) {
bidResponse.meta.advertiserDomains = bid.adomain;
}
if (bid.tl_source && bid.tl_source == 'hdx') {
if (_isVideoBidRequest(breq) && bid.media_type === 'video') {
bidResponse.meta.mediaType = 'video'
} else {
bidResponse.meta.mediaType = 'banner'
}
}
if (bid.tl_source && bid.tl_source == 'tlx') {
bidResponse.meta.mediaType = 'native';
}
if (creativeId) {
bidResponse.meta.networkId = creativeId.slice(0, creativeId.indexOf('_'));
}
};
return bidResponse;
}
registerBidder(tripleliftAdapterSpec);