-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
outbrainBidAdapter.js
420 lines (378 loc) · 12.4 KB
/
outbrainBidAdapter.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
// jshint esversion: 6, es3: false, node: true
'use strict';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import { getStorageManager } from '../src/storageManager.js';
import {OUTSTREAM} from '../src/video.js';
import {_map, deepAccess, deepSetValue, logWarn, replaceAuctionPrice, setOnAny, parseGPTSingleSizeArrayToRtbSize, isPlainObject} from '../src/utils.js';
import {ajax} from '../src/ajax.js';
import {config} from '../src/config.js';
import {convertOrtbRequestToProprietaryNative} from '../src/native.js';
import {Renderer} from '../src/Renderer.js';
const BIDDER_CODE = 'outbrain';
const GVLID = 164;
const CURRENCY = 'USD';
const NATIVE_ASSET_IDS = { 0: 'title', 2: 'icon', 3: 'image', 5: 'sponsoredBy', 4: 'body', 1: 'cta' };
const NATIVE_PARAMS = {
title: { id: 0, name: 'title' },
icon: { id: 2, type: 1, name: 'img' },
image: { id: 3, type: 3, name: 'img' },
sponsoredBy: { id: 5, name: 'data', type: 1 },
body: { id: 4, name: 'data', type: 2 },
cta: { id: 1, type: 12, name: 'data' }
};
const OUTSTREAM_RENDERER_URL = 'https://acdn.adnxs.com/video/outstream/ANOutstreamVideo.js';
const OB_USER_TOKEN_KEY = 'OB-USER-TOKEN';
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [ NATIVE, BANNER, VIDEO ],
isBidRequestValid: (bid) => {
if (typeof bid.params !== 'object') {
return false;
}
if (typeof deepAccess(bid, 'params.publisher.id') !== 'string') {
return false;
}
if (!!bid.params.tagid && typeof bid.params.tagid !== 'string') {
return false;
}
if (!!bid.params.bcat && (typeof bid.params.bcat !== 'object' || !bid.params.bcat.every(item => typeof item === 'string'))) {
return false;
}
if (!!bid.params.badv && (typeof bid.params.badv !== 'object' || !bid.params.badv.every(item => typeof item === 'string'))) {
return false;
}
return (
!!config.getConfig('outbrain.bidderUrl') &&
(!!(bid.nativeParams || bid.sizes) || isValidVideoRequest(bid))
);
},
buildRequests: (validBidRequests, bidderRequest) => {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
const ortb2 = bidderRequest.ortb2 || {};
const page = bidderRequest.refererInfo.page;
const ua = navigator.userAgent;
const test = setOnAny(validBidRequests, 'params.test');
const publisher = setOnAny(validBidRequests, 'params.publisher');
const bcat = ortb2.bcat || setOnAny(validBidRequests, 'params.bcat');
const badv = ortb2.badv || setOnAny(validBidRequests, 'params.badv');
const eids = setOnAny(validBidRequests, 'userIdAsEids');
const wlang = ortb2.wlang;
const cur = CURRENCY;
const endpointUrl = config.getConfig('outbrain.bidderUrl');
const timeout = bidderRequest.timeout;
const imps = validBidRequests.map((bid, id) => {
bid.netRevenue = 'net';
const imp = {
id: id + 1 + ''
}
if (bid.params.tagid) {
imp.tagid = bid.params.tagid
}
if (bid.nativeParams) {
imp.native = {
request: JSON.stringify({
assets: getNativeAssets(bid)
})
}
} else if (isVideoRequest(bid)) {
imp.video = getVideoAsset(bid);
} else {
imp.banner = {
format: bid.sizes?.map((size) => parseGPTSingleSizeArrayToRtbSize(size))
}
}
if (typeof bid.getFloor === 'function') {
const floor = _getFloor(bid, bid.nativeParams ? NATIVE : BANNER);
if (floor) {
imp.bidfloor = floor;
}
}
return imp;
});
const request = {
id: bidderRequest.bidderRequestId,
site: { page, publisher },
device: ortb2?.device || { ua },
source: { fd: 1 },
cur: [cur],
tmax: timeout,
imp: imps,
bcat: bcat,
badv: badv,
wlang: wlang,
ext: {
prebid: {
channel: {
name: 'pbjs',
version: '$prebid.version$'
}
}
}
};
if (test) {
request.is_debug = !!test;
request.test = 1;
}
const obUserToken = storage.getDataFromLocalStorage(OB_USER_TOKEN_KEY)
if (obUserToken) {
deepSetValue(request, 'user.ext.obusertoken', obUserToken)
}
if (deepAccess(bidderRequest, 'gdprConsent.gdprApplies')) {
deepSetValue(request, 'user.ext.consent', bidderRequest.gdprConsent.consentString)
deepSetValue(request, 'regs.ext.gdpr', bidderRequest.gdprConsent.gdprApplies & 1)
}
if (bidderRequest.uspConsent) {
deepSetValue(request, 'regs.ext.us_privacy', bidderRequest.uspConsent)
}
if (config.getConfig('coppa') === true) {
deepSetValue(request, 'regs.coppa', config.getConfig('coppa') & 1)
}
if (bidderRequest.gppConsent) {
deepSetValue(request, 'regs.ext.gpp', bidderRequest.gppConsent.gppString)
deepSetValue(request, 'regs.ext.gpp_sid', bidderRequest.gppConsent.applicableSections)
} else if (deepAccess(bidderRequest, 'ortb2.regs.gpp')) {
deepSetValue(request, 'regs.ext.gpp', bidderRequest.ortb2.regs.gpp)
deepSetValue(request, 'regs.ext.gpp_sid', bidderRequest.ortb2.regs.gpp_sid)
}
if (eids) {
deepSetValue(request, 'user.ext.eids', eids);
}
return {
method: 'POST',
url: endpointUrl,
data: JSON.stringify(request),
bids: validBidRequests
};
},
interpretResponse: (serverResponse, { bids }) => {
if (!serverResponse.body) {
return [];
}
const { seatbid, cur } = serverResponse.body;
const bidResponses = seatbid.map(seat => seat.bid).flat().reduce((result, bid) => {
result[bid.impid - 1] = bid;
return result;
}, []);
return bids.map((bid, id) => {
const bidResponse = bidResponses[id];
if (bidResponse) {
let type = BANNER;
if (bid.nativeParams) {
type = NATIVE;
} else if (isVideoRequest(bid)) {
type = VIDEO;
}
const bidObject = {
requestId: bid.bidId,
cpm: bidResponse.price,
creativeId: bidResponse.crid,
ttl: 360,
netRevenue: bid.netRevenue === 'net',
currency: cur,
mediaType: type,
nurl: bidResponse.nurl,
};
if (type === NATIVE) {
bidObject.native = parseNative(bidResponse);
} else if (type === BANNER) {
bidObject.ad = bidResponse.adm;
bidObject.width = bidResponse.w;
bidObject.height = bidResponse.h;
} else if (type === VIDEO) {
bidObject.vastXml = bidResponse.adm;
const videoContext = deepAccess(bid, 'mediaTypes.video.context');
if (videoContext === OUTSTREAM) {
bidObject.renderer = createRenderer(bid);
}
}
bidObject.meta = {};
if (bidResponse.adomain && bidResponse.adomain.length > 0) {
bidObject.meta.advertiserDomains = bidResponse.adomain;
}
return bidObject;
}
}).filter(Boolean);
},
getUserSyncs: (syncOptions, responses, gdprConsent, uspConsent, gppConsent) => {
const syncs = [];
let syncUrl = config.getConfig('outbrain.usersyncUrl');
let query = [];
if (syncOptions.pixelEnabled && syncUrl) {
if (gdprConsent) {
query.push('gdpr=' + (gdprConsent.gdprApplies & 1));
query.push('gdpr_consent=' + encodeURIComponent(gdprConsent.consentString || ''));
}
if (uspConsent) {
query.push('us_privacy=' + encodeURIComponent(uspConsent));
}
if (gppConsent) {
query.push('gpp=' + encodeURIComponent(gppConsent.gppString));
query.push('gpp_sid=' + encodeURIComponent(gppConsent.applicableSections.join(',')));
}
syncs.push({
type: 'image',
url: syncUrl + (query.length ? '?' + query.join('&') : '')
});
}
return syncs;
},
onBidWon: (bid) => {
// for native requests we put the nurl as an imp tracker, otherwise if the auction takes place on prebid server
// the server JS adapter puts the nurl in the adm as a tracking pixel and removes the attribute
if (bid.nurl) {
ajax(replaceAuctionPrice(bid.nurl, bid.originalCpm))
}
}
};
registerBidder(spec);
function parseNative(bid) {
const { assets, link, privacy, eventtrackers } = JSON.parse(bid.adm);
const result = {
clickUrl: link.url,
clickTrackers: link.clicktrackers || undefined
};
assets.forEach(asset => {
const kind = NATIVE_ASSET_IDS[asset.id];
const content = kind && asset[NATIVE_PARAMS[kind].name];
if (content) {
result[kind] = content.text || content.value || { url: content.url, width: content.w, height: content.h };
}
});
if (privacy) {
result.privacyLink = privacy;
}
if (eventtrackers) {
result.impressionTrackers = [];
eventtrackers.forEach(tracker => {
if (tracker.event !== 1) return;
switch (tracker.method) {
case 1: // img
result.impressionTrackers.push(tracker.url);
break;
case 2: // js
result.javascriptTrackers = `<script src=\"${tracker.url}\"></script>`;
break;
}
});
}
return result;
}
function getNativeAssets(bid) {
return _map(bid.nativeParams, (bidParams, key) => {
const props = NATIVE_PARAMS[key];
const asset = {
required: bidParams.required & 1,
};
if (props) {
asset.id = props.id;
let wmin, hmin, w, h;
let aRatios = bidParams.aspect_ratios;
if (aRatios && aRatios[0]) {
aRatios = aRatios[0];
wmin = aRatios.min_width || 0;
hmin = aRatios.ratio_height * wmin / aRatios.ratio_width | 0;
}
if (bidParams.sizes) {
const sizes = bidParams.sizes.flat();
w = parseInt(sizes[0], 10);
h = parseInt(sizes[1], 10);
}
asset[props.name] = {
len: bidParams.len,
type: props.type,
wmin,
hmin,
w,
h
};
return asset;
}
}).filter(Boolean);
}
function getVideoAsset(bid) {
const sizes = bid.mediaTypes.video.playerSize.flat();
return {
w: parseInt(sizes[0], 10),
h: parseInt(sizes[1], 10),
protocols: bid.mediaTypes.video.protocols,
playbackmethod: bid.mediaTypes.video.playbackmethod,
mimes: bid.mediaTypes.video.mimes,
skip: bid.mediaTypes.video.skip,
delivery: bid.mediaTypes.video.delivery,
api: bid.mediaTypes.video.api,
minbitrate: bid.mediaTypes.video.minbitrate,
maxbitrate: bid.mediaTypes.video.maxbitrate,
minduration: bid.mediaTypes.video.minduration,
maxduration: bid.mediaTypes.video.maxduration,
startdelay: bid.mediaTypes.video.startdelay,
placement: bid.mediaTypes.video.placement,
plcmt: bid.mediaTypes.video.plcmt,
linearity: bid.mediaTypes.video.linearity
};
}
function _getFloor(bid, type) {
const floorInfo = bid.getFloor({
currency: CURRENCY,
mediaType: type,
size: '*'
});
if (isPlainObject(floorInfo) && floorInfo.currency === CURRENCY && !isNaN(parseFloat(floorInfo.floor))) {
return parseFloat(floorInfo.floor);
}
return null;
}
function isVideoRequest(bid) {
return bid.mediaType === 'video' || !!deepAccess(bid, 'mediaTypes.video');
}
function createRenderer(bid) {
let config = {};
let playerUrl = OUTSTREAM_RENDERER_URL;
let render = function (bid) {
bid.renderer.push(() => {
window.ANOutstreamVideo.renderAd({
sizes: bid.sizes,
targetId: bid.adUnitCode,
adResponse: { content: bid.vastXml }
});
});
};
let externalRenderer = deepAccess(bid, 'mediaTypes.video.renderer');
if (!externalRenderer) {
externalRenderer = deepAccess(bid, 'renderer');
}
if (externalRenderer) {
config = externalRenderer.options;
playerUrl = externalRenderer.url;
render = externalRenderer.render;
}
const renderer = Renderer.install({
id: bid.adUnitCode,
url: playerUrl,
config: config,
adUnitCode: bid.adUnitCode,
loaded: false
});
try {
renderer.setRender(render);
} catch (err) {
logWarn('Prebid Error calling setRender on renderer', err);
}
return renderer;
}
function isValidVideoRequest(bid) {
const videoAdUnit = deepAccess(bid, 'mediaTypes.video')
if (!videoAdUnit) {
return false;
}
if (!Array.isArray(videoAdUnit.playerSize)) {
return false;
}
if (videoAdUnit.context == '') {
return false;
}
return true;
}