forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
improvedigitalBidAdapter.js
405 lines (366 loc) · 13.6 KB
/
improvedigitalBidAdapter.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
import {deepAccess, deepSetValue, getBidIdParameter, getUniqueIdentifierStr, logWarn, mergeDeep} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {config} from '../src/config.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {Renderer} from '../src/Renderer.js';
import {hasPurpose1Consent} from '../src/utils/gdpr.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.js';
/**
* See https://github.com/prebid/Prebid.js/pull/8827 for details on linting exception
* ImproveDigital only imports after winning a bid and only if the creative cannot reach top
* Also see https://github.com/prebid/Prebid.js/issues/11656
*/
// eslint-disable-next-line no-restricted-imports
import {loadExternalScript} from '../src/adloader.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').SyncOptions} SyncOptions
* @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync
*/
const BIDDER_CODE = 'improvedigital';
const CREATIVE_TTL = 300;
const AD_SERVER_BASE_URL = 'https://ad.360yield.com';
const BASIC_ADS_BASE_URL = 'https://ad.360yield-basic.com';
const PB_ENDPOINT = 'pb';
const EXTEND_URL = 'https://pbs.360yield.com/openrtb2/auction';
const IFRAME_SYNC_URL = 'https://hb.360yield.com/prebid-universal-creative/load-cookie.html';
const VIDEO_PARAMS = {
DEFAULT_MIMES: ['video/mp4']
};
export const spec = {
code: BIDDER_CODE,
gvlid: 253,
aliases: ['id'],
supportedMediaTypes: [BANNER, NATIVE, VIDEO],
syncStore: { extendMode: false, placementId: null },
/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid(bid) {
return !!(bid && bid.params && bid.params.placementId && bid.params.publisherId);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @param bidderRequest
* @return ServerRequest Info describing the request to the server.
*/
buildRequests(bidRequests, bidderRequest) {
// Save a placement id to send it to the ad server when fetching the user syncs
this.syncStore.placementId = this.syncStore.placementId || bidRequests[0].params.placementId;
return ID_REQUEST.buildServerRequests(bidRequests, bidderRequest);
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @param bidderRequest
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse(serverResponse, { ortbRequest }) {
return CONVERTER.fromORTB({request: ortbRequest, response: serverResponse.body}).bids;
},
/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
if (config.getConfig('coppa') === true || !hasPurpose1Consent(gdprConsent)) {
return [];
}
const syncs = [];
if ((this.syncStore.extendMode || !syncOptions.pixelEnabled) && syncOptions.iframeEnabled) {
const { gdprApplies, consentString } = gdprConsent || {};
const bidders = new Set();
if (this.syncStore.extendMode && serverResponses) {
serverResponses.forEach(response => {
if (!response?.body?.ext?.responsetimemillis) return;
Object.keys(response.body.ext.responsetimemillis).forEach(b => bidders.add(b))
})
}
syncs.push({
type: 'iframe',
url: IFRAME_SYNC_URL +
`?placement_id=${this.syncStore.placementId}` +
(this.syncStore.extendMode ? '&pbs=1' : '') +
(typeof gdprApplies === 'boolean' ? `&gdpr=${Number(gdprApplies)}` : '') +
(consentString ? `&gdpr_consent=${consentString}` : '') +
(uspConsent ? `&us_privacy=${encodeURIComponent(uspConsent)}` : '') +
(bidders.size ? `&bidders=${[...bidders].join(',')}` : '')
});
} else if (syncOptions.pixelEnabled) {
serverResponses.forEach(response => {
const syncArr = deepAccess(response, `body.ext.${BIDDER_CODE}.sync`, []);
syncArr.forEach(url => {
if (!syncs.some(sync => sync.url === url)) {
syncs.push({ type: 'image', url });
}
});
});
}
return syncs;
}
};
registerBidder(spec);
export const CONVERTER = ortbConverter({
context: {
ttl: CREATIVE_TTL,
nativeRequest: {
eventtrackers: [
{event: 1, methods: [1, 2]},
]
}
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
imp.secure = Number(window.location.protocol === 'https:');
if (!imp.bidfloor && bidRequest.params.bidFloor) {
imp.bidfloor = bidRequest.params.bidFloor;
imp.bidfloorcur = getBidIdParameter('bidFloorCur', bidRequest.params).toUpperCase() || 'USD'
}
const bidderParamsPath = context.extendMode ? 'ext.prebid.bidder.improvedigital' : 'ext.bidder';
const placementId = bidRequest.params.placementId;
const publisherId = bidRequest.params.publisherId;
deepSetValue(imp, `${bidderParamsPath}.placementId`, placementId);
deepSetValue(imp, `${bidderParamsPath}.publisherId`, publisherId);
if (context.extendMode) {
deepSetValue(imp, 'ext.prebid.storedrequest.id', '' + placementId);
}
deepSetValue(imp, `${bidderParamsPath}.keyValues`, getBidIdParameter('keyValues', bidRequest.params) || undefined);
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const request = buildRequest(imps, bidderRequest, context);
mergeDeep(request, {
id: getUniqueIdentifierStr(),
source: {
},
ext: {
improvedigital: {
sdk: {
name: 'pbjs',
version: '$prebid.version$',
}
}
},
});
return request;
},
bidResponse(buildBidResponse, bid, context) {
if (!bid.adm || !bid.price || bid.hasOwnProperty('errorCode')) {
return;
}
const {bidRequest} = context;
context.mediaType = (() => {
const requestMediaTypes = Object.keys(bidRequest.mediaTypes);
if (requestMediaTypes.length === 1) return requestMediaTypes[0];
// Detect media type for multi-format response
if (bid.adm.search(/^(<\?xml|<vast)/i) !== -1) {
return VIDEO;
} else if (bid.adm[0] === '{') {
return NATIVE;
} else {
return BANNER;
}
})();
const bidResponse = buildBidResponse(bid, context);
const idExt = deepAccess(bid, `ext.${BIDDER_CODE}`, {});
// Programmatic guaranteed flag
if (idExt.pg === 1) {
bidResponse.adserverTargeting = { hb_deal_type_improve: 'pg' };
}
Object.assign(bidResponse, {
dealId: (typeof idExt.buying_type === 'string' && idExt.buying_type !== 'rtb') ? idExt.line_item_id : undefined,
netRevenue: idExt.is_net || false,
})
if (bidResponse.mediaType === VIDEO && ID_REQUEST.isOutstreamVideo(bidRequest)) {
Object.assign(bidResponse, {
adResponse: { content: bidResponse.vastXml },
renderer: ID_OUTSTREAM.createRenderer(bidRequest)
})
}
ID_RAZR.forwardBid({bidRequest, bid: bidResponse});
return bidResponse;
},
overrides: {
imp: {
banner(fillImpBanner, imp, bidRequest, context) {
// override to disregard banner.sizes if usePrebidSizes is false
if (!bidRequest.mediaTypes[BANNER]) return;
if (config.getConfig('improvedigital.usePrebidSizes') === false) {
const banner = Object.assign({}, bidRequest.mediaTypes[BANNER], {sizes: null});
bidRequest = {...bidRequest, mediaTypes: {[BANNER]: banner}}
}
fillImpBanner(imp, bidRequest, context);
},
video(fillImpVideo, imp, bidRequest, context) {
// override to use video params from both mediaTypes.video and bidRequest.params.video
if (!bidRequest.mediaTypes[VIDEO]) return;
const video = Object.assign(
{mimes: VIDEO_PARAMS.DEFAULT_MIMES},
bidRequest.mediaTypes[VIDEO],
bidRequest.params?.video
)
fillImpVideo(
imp,
{...bidRequest, mediaTypes: {[VIDEO]: video}},
context
);
deepSetValue(imp, 'ext.is_rewarded_inventory', (video.rewarded === 1 || deepAccess(video, 'ext.rewarded') === 1) || undefined);
}
}
}
})
const ID_REQUEST = {
buildServerRequests(bidRequests, bidderRequest) {
const globalExtendMode = config.getConfig('improvedigital.extend') === true;
const requests = [];
const singleRequestMode = config.getConfig('improvedigital.singleRequest') === true;
const extendBids = [];
const adServerBids = [];
function adServerUrl(extendMode, publisherId) {
if (extendMode) {
return EXTEND_URL;
}
const urlSegments = [];
urlSegments.push(hasPurpose1Consent(bidderRequest?.gdprConsent) ? AD_SERVER_BASE_URL : BASIC_ADS_BASE_URL)
if (publisherId) {
urlSegments.push(publisherId)
}
urlSegments.push(PB_ENDPOINT)
return urlSegments.join('/');
}
function formatRequest(bidRequests, publisherId, extendMode) {
const ortbRequest = CONVERTER.toORTB({bidRequests, bidderRequest, context: {extendMode}});
return {
method: 'POST',
url: adServerUrl(extendMode, publisherId),
data: JSON.stringify(ortbRequest),
ortbRequest,
bidderRequest
}
}
let publisherId = null;
bidRequests.map((bidRequest) => {
const bidParamsPublisherId = bidRequest.params.publisherId;
const extendModeEnabled = this.isExtendModeEnabled(globalExtendMode, bidRequest.params);
if (singleRequestMode) {
if (!publisherId) {
publisherId = bidParamsPublisherId;
} else if (bidParamsPublisherId && publisherId !== bidParamsPublisherId) {
throw new Error(`All Improve Digital placements in a single call must have the same publisherId. Please check your 'params.publisherId' or turn off the single request mode.`);
}
extendModeEnabled ? extendBids.push(bidRequest) : adServerBids.push(bidRequest);
} else {
requests.push(formatRequest([bidRequest], bidParamsPublisherId, extendModeEnabled));
}
});
if (!singleRequestMode) {
return requests;
}
// In the single request mode, split imps between those going to the ad server and those going to extend server
if (extendBids.length) {
requests.push(formatRequest(extendBids, publisherId, true));
}
if (adServerBids.length) {
requests.push(formatRequest(adServerBids, publisherId, false));
}
return requests;
},
isExtendModeEnabled(globalExtendMode, bidParams) {
const extendMode = typeof bidParams.extend === 'boolean' ? bidParams.extend : globalExtendMode;
if (extendMode && !spec.syncStore.extendMode) {
spec.syncStore.extendMode = true;
}
return extendMode;
},
isOutstreamVideo(bidRequest) {
return deepAccess(bidRequest, 'mediaTypes.video.context') === 'outstream';
},
};
const ID_OUTSTREAM = {
RENDERER_URL: 'https://acdn.adnxs.com/video/outstream/ANOutstreamVideo.js',
createRenderer(bidRequest) {
const renderer = Renderer.install({
id: bidRequest.adUnitCode,
url: this.RENDERER_URL,
config: deepAccess(bidRequest, 'renderer.options'),
adUnitCode: bidRequest.adUnitCode
});
try {
renderer.setRender(this.render);
} catch (err) {
logWarn('Prebid Error calling setRender on renderer', err);
}
return renderer;
},
render(bid) {
bid.renderer.push(() => {
window.ANOutstreamVideo.renderAd({
sizes: [bid.width, bid.height],
targetId: bid.adUnitCode,
adResponse: bid.adResponse,
rendererOptions: bid.renderer.getConfig()
}, ID_OUTSTREAM.handleRendererEvents.bind(null, bid));
});
},
handleRendererEvents(bid, id, eventName) {
bid.renderer.handleVideoEvent({ id, eventName });
},
};
const ID_RAZR = {
RENDERER_URL: 'https://cdn.360yield.com/razr/tag.js',
forwardBid({bidRequest, bid}) {
if (bid.mediaType !== BANNER) {
return;
}
const cfg = {
prebid: {
bidRequest,
bid
}
};
const cfgStr = JSON.stringify(cfg).replace(/<\/script>/ig, '\\x3C/script>');
const s = `<script>window.__razr_config = ${cfgStr};</script>`;
// prepend RAZR config to ad markup:
bid.ad = s + bid.ad;
this.installListener();
},
installListener() {
if (this._listenerInstalled) {
return;
}
window.addEventListener('message', function(e) {
const data = e.data?.razr?.load;
if (!data) {
return;
}
if (e.source) {
data.source = e.source;
if (data.id) {
e.source.postMessage({
razr: {
id: data.id
}
}, '*');
}
}
const ns = window.razr = window.razr || {};
ns.q = ns.q || [];
ns.q.push(data);
if (!ns.loaded) {
loadExternalScript(ID_RAZR.RENDERER_URL, BIDDER_CODE);
}
});
this._listenerInstalled = true;
}
};