forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheskimiBidAdapter.js
326 lines (281 loc) · 9.25 KB
/
eskimiBidAdapter.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
import {ortbConverter} from '../libraries/ortbConverter/converter.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import * as utils from '../src/utils.js';
import {getBidIdParameter, logInfo, mergeDeep} from '../src/utils.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').validBidRequests} validBidRequests
*/
const BIDDER_CODE = 'eskimi';
const DEFAULT_BID_TTL = 30;
const DEFAULT_CURRENCY = 'USD';
const DEFAULT_NET_REVENUE = true;
const GVLID = 814;
const VIDEO_ORTB_PARAMS = [
'mimes',
'minduration',
'maxduration',
'plcmt',
'protocols',
'startdelay',
'skip',
'skipafter',
'minbitrate',
'maxbitrate',
'delivery',
'playbackmethod',
'api',
'linearity',
'battr'
];
const BANNER_ORTB_PARAMS = [
'battr'
];
const REGION_SUBDOMAIN_SUFFIX = {
EU: '',
US: '-us-e',
APAC: '-asia'
};
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs,
/**
* Register bidder specific code, which will execute if a bid from this bidder won the auction
* @param {Bid} bid The bid that won the auction
*/
onBidWon: function (bid) {
logInfo('Bid won: ', bid);
if (bid.burl) {
utils.triggerPixel(bid.burl);
}
},
onTimeout: function (timeoutData) {
logInfo('Timeout: ', timeoutData);
},
onBidderError: function ({error, bidderRequest}) {
logInfo('Error: ', error, bidderRequest);
},
}
registerBidder(spec);
const CONVERTER = ortbConverter({
context: {
netRevenue: DEFAULT_NET_REVENUE,
ttl: DEFAULT_BID_TTL,
currency: DEFAULT_CURRENCY
},
imp(buildImp, bidRequest, context) {
let imp = buildImp(bidRequest, context);
imp.secure = bidRequest.ortb2Imp?.secure ?? 1;
if (!imp.bidfloor && bidRequest.params.bidFloor) {
imp.bidfloor = bidRequest.params.bidFloor;
imp.bidfloorcur = getBidIdParameter('bidFloorCur', bidRequest.params).toUpperCase() || 'USD'
}
if (bidRequest.mediaTypes[VIDEO]) {
imp = buildVideoImp(bidRequest, imp);
} else if (bidRequest.mediaTypes[BANNER]) {
imp = buildBannerImp(bidRequest, imp);
}
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const req = buildRequest(imps, bidderRequest, context);
mergeDeep(req, {
at: 1,
ext: {
pv: '$prebid.version$'
}
})
const bid = context.bidRequests[0];
if (bid.params.coppa) {
utils.deepSetValue(req, 'regs.coppa', 1);
}
if (bid.params.test) {
req.test = 1
}
return req;
},
});
function isBidRequestValid(bidRequest) {
return (isPlacementIdValid(bidRequest) && (isValidBannerRequest(bidRequest) || isValidVideoRequest(bidRequest)));
}
function isPlacementIdValid(bidRequest) {
return !!parseInt(bidRequest.params.placementId);
}
function isValidBannerRequest(bidRequest) {
const bannerSizes = utils.deepAccess(bidRequest, `mediaTypes.${BANNER}.sizes`);
return utils.isArray(bannerSizes) && bannerSizes.length > 0 && bannerSizes.every(size => utils.isNumber(size[0]) && utils.isNumber(size[1]));
}
function isValidVideoRequest(bidRequest) {
const videoSizes = utils.deepAccess(bidRequest, `mediaTypes.${VIDEO}.playerSize`);
return utils.isArray(videoSizes) && videoSizes.length > 0 && videoSizes.every(size => utils.isNumber(size[0]) && utils.isNumber(size[1]));
}
/**
* Takes an array of valid bid requests, all of which are guaranteed to have passed the isBidRequestValid() test.
* Make a server request from the list of BidRequests.
*
* @param {*} validBidRequests
* @param {*} bidderRequest
* @return ServerRequest Info describing the request to the server.
*/
function buildRequests(validBidRequests, bidderRequest) {
let videoBids = validBidRequests.filter(bid => isVideoBid(bid));
let bannerBids = validBidRequests.filter(bid => isBannerBid(bid));
let requests = [];
bannerBids.forEach(bid => {
requests.push(createRequest([bid], bidderRequest, BANNER));
});
videoBids.forEach(bid => {
requests.push(createRequest([bid], bidderRequest, VIDEO));
});
return requests;
}
function interpretResponse(response, request) {
return CONVERTER.fromORTB({request: request.data, response: response.body}).bids;
}
function buildVideoImp(bidRequest, imp) {
const videoAdUnitParams = utils.deepAccess(bidRequest, `mediaTypes.${VIDEO}`, {});
const videoBidderParams = utils.deepAccess(bidRequest, `params.${VIDEO}`, {});
const videoParams = {...videoAdUnitParams, ...videoBidderParams};
const videoSizes = (videoAdUnitParams && videoAdUnitParams.playerSize) || [];
if (videoSizes && videoSizes.length > 0) {
utils.deepSetValue(imp, 'video.w', videoSizes[0][0]);
utils.deepSetValue(imp, 'video.h', videoSizes[0][1]);
}
VIDEO_ORTB_PARAMS.forEach((param) => {
if (videoParams.hasOwnProperty(param)) {
utils.deepSetValue(imp, `video.${param}`, videoParams[param]);
}
});
if (imp.video && videoParams?.context === 'outstream') {
imp.video.plcmt = imp.video.plcmt || 4;
}
return {...imp};
}
function buildBannerImp(bidRequest, imp) {
const bannerAdUnitParams = utils.deepAccess(bidRequest, `mediaTypes.${BANNER}`, {});
const bannerBidderParams = utils.deepAccess(bidRequest, `params.${BANNER}`, {});
const bannerParams = {...bannerAdUnitParams, ...bannerBidderParams};
let sizes = bidRequest.mediaTypes.banner.sizes;
if (sizes) {
utils.deepSetValue(imp, 'banner.w', sizes[0][0]);
utils.deepSetValue(imp, 'banner.h', sizes[0][1]);
}
BANNER_ORTB_PARAMS.forEach((param) => {
if (bannerParams.hasOwnProperty(param)) {
utils.deepSetValue(imp, `banner.${param}`, bannerParams[param]);
}
});
return {...imp};
}
function createRequest(bidRequests, bidderRequest, mediaType) {
const data = CONVERTER.toORTB({bidRequests, bidderRequest, context: {mediaType}})
const bid = bidRequests.find((b) => b.params.placementId)
if (!data.site) data.site = {}
data.site.ext = {placementId: parseInt(bid.params.placementId)}
if (bidderRequest.gdprConsent) {
if (!data.user) data.user = {};
if (!data.user.ext) data.user.ext = {};
if (!data.regs) data.regs = {};
if (!data.regs.ext) data.regs.ext = {};
data.user.ext.consent = bidderRequest.gdprConsent.consentString;
data.regs.ext.gdpr = bidderRequest.gdprConsent.gdprApplies ? 1 : 0;
}
if (bid.params.bcat) data.bcat = bid.params.bcat;
if (bid.params.badv) data.badv = bid.params.badv;
if (bid.params.bapp) data.bapp = bid.params.bapp;
return {
method: 'POST',
url: getBidRequestUrlByRegion(),
data: data,
options: {contentType: 'application/json;charset=UTF-8', withCredentials: false}
}
}
function isVideoBid(bid) {
return utils.deepAccess(bid, 'mediaTypes.video');
}
function isBannerBid(bid) {
return utils.deepAccess(bid, 'mediaTypes.banner') || !isVideoBid(bid);
}
/**
* @param syncOptions
* @param responses
* @param gdprConsent
* @param uspConsent
* @param gppConsent
* @return {{type: (string), url: (*|string)}[]}
*/
function getUserSyncs(syncOptions, responses, gdprConsent, uspConsent, gppConsent) {
if ((syncOptions.iframeEnabled || syncOptions.pixelEnabled)) {
let pixelType = syncOptions.iframeEnabled ? 'iframe' : 'image';
let query = [];
let syncUrl = getUserSyncUrlByRegion();
// GDPR Consent Params in UserSync url
if (gdprConsent) {
query.push('gdpr=' + (gdprConsent.gdprApplies & 1));
query.push('gdpr_consent=' + encodeURIComponent(gdprConsent.consentString || ''));
}
// US Privacy Consent
if (uspConsent) {
query.push('us_privacy=' + encodeURIComponent(uspConsent));
}
// Global Privacy Platform Consent
if (gppConsent?.gppString && gppConsent?.applicableSections?.length) {
query.push('gpp=' + encodeURIComponent(gppConsent.gppString));
query.push('gpp_sid=' + encodeURIComponent(gppConsent.applicableSections.join(',')));
}
return [{
type: pixelType,
url: `${syncUrl}${query.length > 0 ? '&' + query.join('&') : ''}`
}];
}
}
/**
* Get Bid Request endpoint url by region
* @return {string}
*/
function getBidRequestUrlByRegion() {
return `https://ittr${getRegionSubdomainSuffix()}.eskimi.com/prebidjs`;
}
/**
* Get User Sync endpoint url by region
* @return {string}
*/
function getUserSyncUrlByRegion() {
return `https://ittpx${getRegionSubdomainSuffix()}.eskimi.com/sync?sp_id=137`;
}
/**
* Get subdomain URL suffix by region
* @return {string}
*/
function getRegionSubdomainSuffix() {
try {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const region = timezone.split('/')[0];
switch (region) {
case 'Europe':
case 'Africa':
case 'Atlantic':
case 'Arctic':
return REGION_SUBDOMAIN_SUFFIX['EU'];
case 'Asia':
case 'Australia':
case 'Antarctica':
case 'Pacific':
case 'Indian':
return REGION_SUBDOMAIN_SUFFIX['APAC'];
case 'America':
return REGION_SUBDOMAIN_SUFFIX['US'];
default:
return REGION_SUBDOMAIN_SUFFIX['EU'];
}
} catch (err) {
return REGION_SUBDOMAIN_SUFFIX['EU'];
}
}