forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dianomiBidAdapter.js
357 lines (314 loc) · 9 KB
/
dianomiBidAdapter.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
// jshint esversion: 6, es3: false, node: true
'use strict';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { NATIVE, BANNER, VIDEO } from '../src/mediaTypes.js';
import {
mergeDeep,
_map,
deepAccess,
parseSizesInput,
deepSetValue,
formatQS,
setOnAny
} from '../src/utils.js';
import { config } from '../src/config.js';
import { Renderer } from '../src/Renderer.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
import {getUserSyncParams} from '../libraries/userSyncUtils/userSyncUtils.js';
const { getConfig } = config;
const BIDDER_CODE = 'dianomi';
const GVLID = 885;
const BIDDER_ALIAS = [{ code: 'dia', gvlid: GVLID }];
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',
},
};
let endpoint = 'www-prebid.dianomi.com';
const OUTSTREAM_RENDERER_URL = (hostname) => `https://${hostname}/prebid/outstream/renderer.js`;
export const spec = {
code: BIDDER_CODE,
aliases: BIDDER_ALIAS,
gvlid: GVLID,
supportedMediaTypes: [NATIVE, BANNER, VIDEO],
isBidRequestValid: (bid) => {
const params = bid.params || {};
const { smartadId } = params;
return !!smartadId;
},
buildRequests: (validBidRequests, bidderRequest) => {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
let app, site;
const commonFpd = bidderRequest.ortb2 || {};
let { user } = commonFpd;
if (typeof getConfig('app') === 'object') {
app = getConfig('app') || {};
if (commonFpd.app) {
mergeDeep(app, commonFpd.app);
}
} else {
site = getConfig('site') || {};
if (commonFpd.site) {
mergeDeep(site, commonFpd.site);
}
if (!site.page) {
site.page = bidderRequest.refererInfo.page;
}
}
const device = getConfig('device') || {};
device.w = device.w || window.innerWidth;
device.h = device.h || window.innerHeight;
device.ua = device.ua || navigator.userAgent;
const paramsEndpoint = setOnAny(validBidRequests, 'params.endpoint');
if (paramsEndpoint) {
endpoint = paramsEndpoint;
}
const pt =
setOnAny(validBidRequests, 'params.pt') ||
setOnAny(validBidRequests, 'params.priceType') ||
'net';
const tid = bidderRequest.ortb2?.source?.tid;
const currency = getConfig('currency.adServerCurrency');
const cur = currency && [currency];
const eids = setOnAny(validBidRequests, 'userIdAsEids');
const schain = setOnAny(validBidRequests, 'schain');
const imp = validBidRequests.map((bid, id) => {
bid.netRevenue = pt;
const floorInfo = bid.getFloor
? bid.getFloor({
currency: currency || 'USD',
})
: {};
const bidfloor = floorInfo.floor;
const bidfloorcur = floorInfo.currency;
const { smartadId } = bid.params;
const imp = {
id: id + 1,
tagid: smartadId,
bidfloor,
bidfloorcur,
ext: {
bidder: {
smartadId: smartadId,
},
},
};
const assets = _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 = flatten(bidParams.sizes);
w = sizes[0];
h = sizes[1];
}
asset[props.name] = {
len: bidParams.len,
type: props.type,
wmin,
hmin,
w,
h,
};
return asset;
}
}).filter(Boolean);
if (assets.length) {
imp.native = {
assets,
};
}
const bannerParams = deepAccess(bid, 'mediaTypes.banner');
if (bannerParams && bannerParams.sizes) {
const sizes = parseSizesInput(bannerParams.sizes);
const format = sizes.map((size) => {
const [width, height] = size.split('x');
const w = parseInt(width, 10);
const h = parseInt(height, 10);
return { w, h };
});
imp.banner = {
format,
};
}
const videoParams = deepAccess(bid, 'mediaTypes.video');
if (videoParams) {
imp.video = videoParams;
}
return imp;
});
const request = {
id: bidderRequest.auctionId,
site,
app,
user,
device,
source: { tid, fd: 1 },
ext: { pt },
cur,
imp,
};
if (deepAccess(bidderRequest, 'gdprConsent.gdprApplies') !== undefined) {
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 (eids) {
deepSetValue(request, 'user.ext.eids', eids);
}
if (schain) {
deepSetValue(request, 'source.ext.schain', schain);
}
return {
method: 'POST',
url: 'https://' + endpoint + '/cgi-bin/smartads_prebid.pl',
data: JSON.stringify(request),
bids: validBidRequests,
};
},
interpretResponse: function (serverResponse, { bids }) {
if (!serverResponse.body || serverResponse?.body?.nbr) {
return;
}
const { seatbid, cur } = serverResponse.body;
const bidResponses = flatten(seatbid.map((seat) => seat.bid)).reduce((result, bid) => {
result[bid.impid - 1] = bid;
return result;
}, []);
return bids
.map((bid, id) => {
const bidResponse = bidResponses[id];
if (bidResponse) {
const mediaType = deepAccess(bidResponse, 'ext.prebid.type');
const result = {
requestId: bid.bidId,
cpm: bidResponse.price,
creativeId: bidResponse.crid,
ttl: 360,
netRevenue: bid.netRevenue === 'net',
currency: cur,
mediaType,
width: bidResponse.w,
height: bidResponse.h,
dealId: bidResponse.dealid,
meta: {
mediaType,
advertiserDomains: bidResponse.adomain,
},
};
if (bidResponse.native) {
result.native = parseNative(bidResponse);
} else {
result[mediaType === VIDEO ? 'vastXml' : 'ad'] = bidResponse.adm;
}
if (
!bid.renderer &&
mediaType === VIDEO &&
deepAccess(bid, 'mediaTypes.video.context') === 'outstream'
) {
result.renderer = Renderer.install({
id: bid.bidId,
url: OUTSTREAM_RENDERER_URL(endpoint),
adUnitCode: bid.adUnitCode,
});
result.renderer.setRender(renderer);
}
return result;
}
})
.filter(Boolean);
},
getUserSyncs: (syncOptions, responses, gdprConsent, uspConsent) => {
const params = getUserSyncParams(gdprConsent, uspConsent);
if (syncOptions.iframeEnabled) {
// data is only assigned if params are available to pass to syncEndpoint
return {
type: 'iframe',
url: `https://${endpoint}/prebid/usersync/index.html?${formatQS(params)}`,
};
} else if (syncOptions.pixelEnabled) {
return {
type: 'image',
url: `https://${endpoint.includes('dev') ? 'dev-' : ''}data.dianomi.com/frontend/usync?${formatQS(params)}`,
};
}
},
};
registerBidder(spec);
function parseNative(bid) {
const { assets, link, imptrackers, jstracker } = bid.native;
const result = {
clickUrl: link.url,
clickTrackers: link.clicktrackers || undefined,
impressionTrackers: imptrackers || undefined,
javascriptTrackers: jstracker ? [jstracker] : 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,
};
}
});
return result;
}
function flatten(arr) {
return [].concat(...arr);
}
function renderer(bid) {
bid.renderer.push(() => {
window.Dianomi.renderOutstream(bid);
});
}