-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
dxkultureBidAdapter.js
340 lines (284 loc) · 9.22 KB
/
dxkultureBidAdapter.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
import {
logInfo,
logWarn,
logError,
logMessage,
deepAccess,
deepSetValue,
mergeDeep
} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import { Renderer } from '../src/Renderer.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.js'
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
*/
const BIDDER_CODE = 'dxkulture';
const DEFAULT_BID_TTL = 300;
const DEFAULT_NET_REVENUE = true;
const DEFAULT_CURRENCY = 'USD';
const DEFAULT_OUTSTREAM_RENDERER_URL = 'https://cdn.dxkulture.com/players/dxOutstreamPlayer.js';
const converter = ortbConverter({
context: {
netRevenue: DEFAULT_NET_REVENUE,
ttl: DEFAULT_BID_TTL
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
if (!imp.bidfloor) {
imp.bidfloor = bidRequest.params.bidfloor || 0;
imp.bidfloorcur = bidRequest.params.currency || DEFAULT_CURRENCY;
}
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const req = buildRequest(imps, bidderRequest, context);
mergeDeep(req, {
ext: {
hb: 1,
prebidver: '$prebid.version$',
adapterver: '1.0.0',
}
})
// Attaching GDPR Consent Params
if (bidderRequest.gdprConsent) {
deepSetValue(req, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(req, 'regs.ext.gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}
// CCPA
if (bidderRequest.uspConsent) {
deepSetValue(req, 'regs.ext.us_privacy', bidderRequest.uspConsent);
}
return req;
},
bidResponse(buildBidResponse, bid, context) {
let resMediaType;
const {bidRequest} = context;
if (bid.adm?.trim().startsWith('<VAST')) {
resMediaType = VIDEO;
} else {
resMediaType = BANNER;
}
context.mediaType = resMediaType;
context.currency = DEFAULT_CURRENCY;
if (resMediaType === VIDEO) {
context.vastXml = bid.adm;
}
const bidResponse = buildBidResponse(bid, context);
if (resMediaType === VIDEO && bidRequest.mediaTypes.video.context === 'outstream') {
bidResponse.renderer = outstreamRenderer(bidResponse);
}
return bidResponse;
}
});
export const spec = {
code: BIDDER_CODE,
VERSION: '1.0.0',
supportedMediaTypes: [BANNER, VIDEO],
ENDPOINT: 'https://ads.dxkulture.com/pbjs',
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
return (
_validateParams(bid) &&
_validateBanner(bid) &&
_validateVideo(bid)
);
},
buildRequests: function (validBidRequests, bidderRequest) {
let contextMediaType = BANNER;
if (hasVideoMediaType(validBidRequests)) {
contextMediaType = VIDEO;
}
const data = converter.toORTB({ bidRequests: validBidRequests, bidderRequest, context: {contextMediaType} });
let publisherId = validBidRequests[0].params.publisherId;
let placementId = validBidRequests[0].params.placementId;
if (validBidRequests[0].params.e2etest) {
logMessage('dxkulture: E2E test mode enabled');
publisherId = 'e2etest'
}
let baseEndpoint = spec.ENDPOINT + '?pid=' + publisherId;
if (placementId) {
baseEndpoint += '&placementId=' + placementId
}
return {
method: 'POST',
url: baseEndpoint,
data: data
};
},
interpretResponse: function (serverResponse, bidRequest) {
const bids = converter.fromORTB({response: serverResponse.body, request: bidRequest.data}).bids;
return bids;
},
getUserSyncs: function (syncOptions, serverResponses, gdprConsent, uspConsent) {
logInfo('dxkulture.getUserSyncs', 'syncOptions', syncOptions, 'serverResponses', serverResponses);
let syncs = [];
if (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled) {
return syncs;
}
serverResponses.forEach(resp => {
const userSync = deepAccess(resp, 'body.ext.usersync');
if (userSync) {
let syncDetails = [];
Object.keys(userSync).forEach(key => {
const value = userSync[key];
if (value.syncs && value.syncs.length) {
syncDetails = syncDetails.concat(value.syncs);
}
});
syncDetails.forEach(syncDetails => {
let queryParamStrings = [];
let syncUrl = syncDetails.url;
if (syncDetails.type === 'iframe') {
if (gdprConsent) {
queryParamStrings.push('gdpr=' + (gdprConsent.gdprApplies ? 1 : 0));
queryParamStrings.push('gdpr_consent=' + encodeURIComponent(gdprConsent.consentString || ''));
}
if (uspConsent) {
queryParamStrings.push('us_privacy=' + encodeURIComponent(uspConsent));
}
syncUrl = `${syncDetails.url}${queryParamStrings.length > 0 ? '?' + queryParamStrings.join('&') : ''}`
}
syncs.push({
type: syncDetails.type === 'iframe' ? 'iframe' : 'image',
url: syncUrl
});
});
if (syncOptions.iframeEnabled) {
syncs = syncs.filter(s => s.type == 'iframe');
} else if (syncOptions.pixelEnabled) {
syncs = syncs.filter(s => s.type == 'image');
}
}
});
logInfo('dxkulture.getUserSyncs result=%o', syncs);
return syncs;
},
};
function outstreamRenderer(bid) {
const rendererConfig = {
width: bid.width,
height: bid.height,
vastTimeout: 5000,
maxAllowedVastTagRedirects: 3,
allowVpaid: false,
autoPlay: true,
preload: true,
mute: false
}
const renderer = Renderer.install({
id: bid.adId,
url: DEFAULT_OUTSTREAM_RENDERER_URL,
config: rendererConfig,
loaded: false,
targetId: bid.adUnitCode,
adUnitCode: bid.adUnitCode
});
try {
renderer.setRender(function (bid) {
bid.renderer.push(() => {
const { id, config } = bid.renderer;
window.dxOutstreamPlayer(bid, id, config);
});
});
} catch (err) {
logWarn('dxkulture: Prebid Error calling setRender on renderer', err);
}
return renderer;
}
/* =======================================
* Util Functions
*======================================= */
function hasBannerMediaType(bidRequest) {
return !!deepAccess(bidRequest, 'mediaTypes.banner');
}
function hasVideoMediaType(bidRequest) {
return !!deepAccess(bidRequest, 'mediaTypes.video');
}
function _validateParams(bidRequest) {
if (!bidRequest.params) {
return false;
}
if (bidRequest.params.e2etest) {
return true;
}
if (!bidRequest.params.publisherId) {
logError('dxkulture: Validation failed: publisherId not declared');
return false;
}
if (!bidRequest.params.placementId) {
logError('dxkulture: Validation failed: placementId not declared');
return false;
}
const mediaTypesExists = hasVideoMediaType(bidRequest) || hasBannerMediaType(bidRequest);
if (!mediaTypesExists) {
return false;
}
return true;
}
/**
* Validates banner bid request. If it is not banner media type returns true.
* @param {BidRequest} bidRequest bid to validate
* @return boolean, true if valid, otherwise false
*/
function _validateBanner(bidRequest) {
// If there's no banner no need to validate
if (!hasBannerMediaType(bidRequest)) {
return true;
}
const banner = deepAccess(bidRequest, 'mediaTypes.banner');
if (!Array.isArray(banner.sizes)) {
return false;
}
return true;
}
/**
* Validates video bid request. If it is not video media type returns true.
* @param {BidRequest} bidRequest, bid to validate
* @return boolean, true if valid, otherwise false
*/
function _validateVideo(bidRequest) {
// If there's no video no need to validate
if (!hasVideoMediaType(bidRequest)) {
return true;
}
const videoPlacement = deepAccess(bidRequest, 'mediaTypes.video', {});
const videoBidderParams = deepAccess(bidRequest, 'params.video', {});
const params = deepAccess(bidRequest, 'params', {});
if (params && params.e2etest) {
return true;
}
const videoParams = {
...videoPlacement,
...videoBidderParams // Bidder Specific overrides
};
if (!Array.isArray(videoParams.mimes) || videoParams.mimes.length === 0) {
logError('dxkulture: Validation failed: mimes are invalid');
return false;
}
if (!Array.isArray(videoParams.protocols) || videoParams.protocols.length === 0) {
logError('dxkulture: Validation failed: protocols are invalid');
return false;
}
if (!videoParams.context) {
logError('dxkulture: Validation failed: context id not declared');
return false;
}
if (videoParams.context !== 'instream') {
logError('dxkulture: Validation failed: only context instream is supported ');
return false;
}
if (typeof videoParams.playerSize === 'undefined' || !Array.isArray(videoParams.playerSize) || !Array.isArray(videoParams.playerSize[0])) {
logError('dxkulture: Validation failed: player size not declared or is not in format [[w,h]]');
return false;
}
return true;
}
registerBidder(spec);