forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sharethroughBidAdapter.js
319 lines (278 loc) · 10.5 KB
/
sharethroughBidAdapter.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
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { deepAccess, generateUUID, inIframe, mergeDeep } from '../src/utils.js';
const VERSION = '4.3.0';
const BIDDER_CODE = 'sharethrough';
const SUPPLY_ID = 'WYu2BXv1';
const STR_ENDPOINT = `https://btlr.sharethrough.com/universal/v1?supply_id=${SUPPLY_ID}`;
// this allows stubbing of utility function that is used internally by the sharethrough adapter
export const sharethroughInternal = {
getProtocol,
};
export const sharethroughAdapterSpec = {
code: BIDDER_CODE,
supportedMediaTypes: [VIDEO, BANNER],
gvlid: 80,
isBidRequestValid: (bid) => !!bid.params.pkey,
buildRequests: (bidRequests, bidderRequest) => {
const timeout = bidderRequest.timeout;
const firstPartyData = bidderRequest.ortb2 || {};
const nonHttp = sharethroughInternal.getProtocol().indexOf('http') < 0;
const secure = nonHttp || sharethroughInternal.getProtocol().indexOf('https') > -1;
const req = {
id: generateUUID(),
at: 1,
cur: ['USD'],
tmax: timeout,
site: {
domain: deepAccess(bidderRequest, 'refererInfo.domain', window.location.hostname),
page: deepAccess(bidderRequest, 'refererInfo.page', window.location.href),
ref: deepAccess(bidderRequest, 'refererInfo.ref'),
...firstPartyData.site,
},
device: {
ua: navigator.userAgent,
language: navigator.language,
js: 1,
dnt: navigator.doNotTrack === '1' ? 1 : 0,
h: window.screen.height,
w: window.screen.width,
ext: {},
},
regs: {
coppa: config.getConfig('coppa') === true ? 1 : 0,
ext: {},
},
source: {
tid: bidderRequest.ortb2?.source?.tid,
ext: {
version: '$prebid.version$',
str: VERSION,
schain: bidRequests[0].schain,
},
},
bcat: deepAccess(bidderRequest.ortb2, 'bcat') || bidRequests[0].params.bcat || [],
badv: deepAccess(bidderRequest.ortb2, 'badv') || bidRequests[0].params.badv || [],
test: 0,
};
if (bidderRequest.ortb2?.device?.ext?.cdep) {
req.device.ext['cdep'] = bidderRequest.ortb2.device.ext.cdep;
}
// if present, merge device object from ortb2 into `req.device`
if (bidderRequest?.ortb2?.device) {
mergeDeep(req.device, bidderRequest.ortb2.device);
}
req.user = nullish(firstPartyData.user, {});
if (!req.user.ext) req.user.ext = {};
req.user.ext.eids = bidRequests[0].userIdAsEids || [];
if (bidderRequest.gdprConsent) {
const gdprApplies = bidderRequest.gdprConsent.gdprApplies === true;
req.regs.ext.gdpr = gdprApplies ? 1 : 0;
if (gdprApplies) {
req.user.ext.consent = bidderRequest.gdprConsent.consentString;
}
}
if (bidderRequest.uspConsent) {
req.regs.ext.us_privacy = bidderRequest.uspConsent;
}
if (bidderRequest?.gppConsent?.gppString) {
req.regs.gpp = bidderRequest.gppConsent.gppString;
req.regs.gpp_sid = bidderRequest.gppConsent.applicableSections;
} else if (bidderRequest?.ortb2?.regs?.gpp) {
req.regs.ext.gpp = bidderRequest.ortb2.regs.gpp;
req.regs.ext.gpp_sid = bidderRequest.ortb2.regs.gpp_sid;
}
if (bidderRequest?.ortb2?.regs?.ext?.dsa) {
req.regs.ext.dsa = bidderRequest.ortb2.regs.ext.dsa;
}
const imps = bidRequests
.map((bidReq) => {
const impression = { ext: {} };
// mergeDeep(impression, bidReq.ortb2Imp); // leaving this out for now as we may want to leave stuff out on purpose
const tid = deepAccess(bidReq, 'ortb2Imp.ext.tid');
if (tid) impression.ext.tid = tid;
const gpid = deepAccess(bidReq, 'ortb2Imp.ext.gpid') || deepAccess(bidReq, 'ortb2Imp.ext.data.pbadslot');
if (gpid) impression.ext.gpid = gpid;
const videoRequest = deepAccess(bidReq, 'mediaTypes.video');
if (bidderRequest.paapi?.enabled && bidReq.mediaTypes.banner) {
mergeDeep(impression, { ext: { ae: 1 } }); // ae = auction environment; if this is 1, ad server knows we have a fledge auction
}
if (videoRequest) {
// default playerSize, only change this if we know width and height are properly defined in the request
let [w, h] = [640, 360];
if (
videoRequest.playerSize &&
videoRequest.playerSize[0] &&
videoRequest.playerSize[0][0] &&
videoRequest.playerSize[0][1]
) {
[w, h] = videoRequest.playerSize[0];
}
const getVideoPlacementValue = (vidReq) => {
if (vidReq.plcmt) {
return vidReq.placement;
} else {
return vidReq.context === 'instream' ? 1 : +deepAccess(vidReq, 'placement', 4);
}
};
impression.video = {
pos: nullish(videoRequest.pos, 0),
topframe: inIframe() ? 0 : 1,
skip: nullish(videoRequest.skip, 0),
linearity: nullish(videoRequest.linearity, 1),
minduration: nullish(videoRequest.minduration, 5),
maxduration: nullish(videoRequest.maxduration, 60),
playbackmethod: videoRequest.playbackmethod || [2],
api: getVideoApi(videoRequest),
mimes: videoRequest.mimes || ['video/mp4'],
protocols: getVideoProtocols(videoRequest),
w,
h,
startdelay: nullish(videoRequest.startdelay, 0),
skipmin: nullish(videoRequest.skipmin, 0),
skipafter: nullish(videoRequest.skipafter, 0),
placement: getVideoPlacementValue(videoRequest),
plcmt: videoRequest.plcmt ? videoRequest.plcmt : null,
};
if (videoRequest.battr) impression.video.battr = videoRequest.battr;
if (videoRequest.delivery) impression.video.delivery = videoRequest.delivery;
if (videoRequest.companiontype) impression.video.companiontype = videoRequest.companiontype;
if (videoRequest.companionad) impression.video.companionad = videoRequest.companionad;
} else {
impression.banner = {
pos: deepAccess(bidReq, 'mediaTypes.banner.pos', 0),
topframe: inIframe() ? 0 : 1,
format: bidReq.sizes.map((size) => ({ w: +size[0], h: +size[1] })),
};
const battr = deepAccess(bidReq, 'mediaTypes.banner.battr', null) || deepAccess(bidReq, 'ortb2Imp.banner.battr')
if (battr) impression.banner.battr = battr
}
return {
id: bidReq.bidId,
tagid: String(bidReq.params.pkey),
secure: secure ? 1 : 0,
bidfloor: getBidRequestFloor(bidReq),
...impression,
};
})
.filter((imp) => !!imp);
return imps.map((impression) => {
return {
method: 'POST',
url: STR_ENDPOINT,
data: {
...req,
imp: [impression],
},
};
});
},
interpretResponse: ({ body }, req) => {
if (
!body ||
!body.seatbid ||
body.seatbid.length === 0 ||
!body.seatbid[0].bid ||
body.seatbid[0].bid.length === 0
) {
return [];
}
const fledgeAuctionEnabled = body.ext?.auctionConfigs;
const bidsFromExchange = body.seatbid[0].bid.map((bid) => {
// Spec: https://docs.prebid.org/dev-docs/bidder-adaptor.html#interpreting-the-response
const response = {
requestId: bid.impid,
width: +bid.w,
height: +bid.h,
cpm: +bid.price,
creativeId: bid.crid,
dealId: bid.dealid || null,
mediaType: req.data.imp[0].video ? VIDEO : BANNER,
currency: body.cur || 'USD',
netRevenue: true,
ttl: 360,
ad: bid.adm,
nurl: bid.nurl,
meta: {
advertiserDomains: bid.adomain || [],
networkId: bid.ext?.networkId || null,
networkName: bid.ext?.networkName || null,
agencyId: bid.ext?.agencyId || null,
agencyName: bid.ext?.agencyName || null,
advertiserId: bid.ext?.advertiserId || null,
advertiserName: bid.ext?.advertiserName || null,
brandId: bid.ext?.brandId || null,
brandName: bid.ext?.brandName || null,
demandSource: bid.ext?.demandSource || null,
dchain: bid.ext?.dchain || null,
primaryCatId: bid.ext?.primaryCatId || null,
secondaryCatIds: bid.ext?.secondaryCatIds || null,
mediaType: bid.ext?.mediaType || null,
},
};
if (response.mediaType === VIDEO) {
response.ttl = 3600;
response.vastXml = bid.adm;
}
return response;
});
if (fledgeAuctionEnabled) {
return {
bids: bidsFromExchange,
paapi: body.ext?.auctionConfigs || {},
};
} else {
return bidsFromExchange;
}
},
getUserSyncs: (syncOptions, serverResponses) => {
const shouldCookieSync =
syncOptions.pixelEnabled && deepAccess(serverResponses, '0.body.cookieSyncUrls') !== undefined;
return shouldCookieSync ? serverResponses[0].body.cookieSyncUrls.map((url) => ({ type: 'image', url: url })) : [];
},
// Empty implementation for prebid core to be able to find it
onTimeout: (data) => {},
// Empty implementation for prebid core to be able to find it
onBidWon: (bid) => {},
// Empty implementation for prebid core to be able to find it
onSetTargeting: (bid) => {},
};
function getVideoApi({ api }) {
let defaultValue = [2];
if (api && Array.isArray(api) && api.length > 0) {
return api;
} else {
return defaultValue;
}
}
function getVideoProtocols({ protocols }) {
let defaultValue = [2, 3, 5, 6, 7, 8];
if (protocols && Array.isArray(protocols) && protocols.length > 0) {
return protocols;
} else {
return defaultValue;
}
}
function getBidRequestFloor(bid) {
let floor = null;
if (typeof bid.getFloor === 'function') {
const floorInfo = bid.getFloor({
currency: 'USD',
mediaType: bid.mediaTypes && bid.mediaTypes.video ? 'video' : 'banner',
size: bid.sizes.map((size) => ({ w: size[0], h: size[1] })),
});
if (typeof floorInfo === 'object' && floorInfo.currency === 'USD' && !isNaN(parseFloat(floorInfo.floor))) {
floor = parseFloat(floorInfo.floor);
}
}
return floor !== null ? floor : bid.params.floor;
}
function getProtocol() {
return window.location.protocol;
}
// stub for ?? operator
function nullish(input, def) {
return input === null || input === undefined ? def : input;
}
registerBidder(sharethroughAdapterSpec);