forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
seedingAllianceBidAdapter.js
executable file
·301 lines (245 loc) · 7.88 KB
/
seedingAllianceBidAdapter.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
// jshint esversion: 6, es3: false, node: true
'use strict';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE} from '../src/mediaTypes.js';
import {_map, generateUUID, deepSetValue, isArray, isEmpty, replaceAuctionPrice} from '../src/utils.js';
import {config} from '../src/config.js';
import {convertOrtbRequestToProprietaryNative} from '../src/native.js';
import {getStorageManager} from '../src/storageManager.js';
const GVL_ID = 371;
const BIDDER_CODE = 'seedingAlliance';
const DEFAULT_CUR = 'EUR';
const ENDPOINT_URL = 'https://b.nativendo.de/cds/rtb/bid?format=openrtb2.5&ssp=pb';
const NATIVENDO_KEY = 'nativendo_id';
const NATIVE_ASSET_IDS = { 0: 'title', 1: 'body', 2: 'sponsoredBy', 3: 'image', 4: 'cta', 5: 'icon' };
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
const NATIVE_PARAMS = {
title: { id: 0, name: 'title' },
body: { id: 1, name: 'data', type: 2 },
sponsoredBy: { id: 2, name: 'data', type: 1 },
image: { id: 3, type: 3, name: 'img' },
cta: { id: 4, type: 12, name: 'data' },
icon: { id: 5, type: 1, name: 'img' }
};
export const spec = {
code: BIDDER_CODE,
gvlid: GVL_ID,
supportedMediaTypes: [NATIVE, BANNER],
isBidRequestValid: function (bid) {
return !!bid.params.adUnitId;
},
buildRequests: (validBidRequests = [], bidderRequest) => {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
let url = bidderRequest.refererInfo.page;
let eids = getEids(validBidRequests[0]);
const imps = validBidRequests.map((bidRequest, id) => {
const imp = {
id: String(id + 1),
tagid: bidRequest.params.adUnitId
};
/**
* Native Ad
*/
if (bidRequest.nativeParams) {
const assets = _map(bidRequest.nativeParams, (nativeAsset, key) => {
const props = NATIVE_PARAMS[key];
if (props) {
let wmin, hmin, w, h;
let aRatios = nativeAsset.aspect_ratios;
if (aRatios && aRatios[0]) {
aRatios = aRatios[0];
wmin = aRatios.min_width || 0;
hmin = aRatios.ratio_height * wmin / aRatios.ratio_width | 0;
}
if (nativeAsset.sizes) {
const sizes = flatten(nativeAsset.sizes);
w = parseInt(sizes[0], 10);
h = parseInt(sizes[1], 10);
}
const asset = {
id: props.id,
required: nativeAsset.required & 1
};
asset[props.name] = {
len: nativeAsset.len,
type: props.type,
wmin,
hmin,
w,
h
};
return asset;
} else {
// TODO Filter impressions with required assets we don't support
}
}).filter(Boolean);
imp.native = {
request: {
assets
}
};
} else {
let sizes = transformSizes(bidRequest.sizes);
imp.banner = {
format: sizes,
w: sizes[0] ? sizes[0].w : 0,
h: sizes[0] ? sizes[0].h : 0
}
}
if (bidRequest.params.url) {
url = bidRequest.params.url;
}
return imp;
});
const request = {
id: bidderRequest.bidderRequestId,
site: {
page: url
},
cur: [config.getConfig('currency.adServerCurrency') || DEFAULT_CUR],
imp: imps,
tmax: bidderRequest.timeout,
regs: {
ext: {
gdpr: 0,
pb_ver: '$prebid.version$'
}
}
};
if (bidderRequest.gdprConsent) {
request.user = {};
deepSetValue(request, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(request, 'regs.ext.gdpr', (typeof bidderRequest.gdprConsent.gdprApplies === 'boolean' && bidderRequest.gdprConsent.gdprApplies) ? 1 : 0);
deepSetValue(request, 'user.ext.eids', eids);
}
let endpoint = config.getConfig('seedingAlliance.endpoint') || ENDPOINT_URL;
return {
method: 'POST',
url: endpoint,
data: JSON.stringify(request),
bidRequests: validBidRequests
};
},
interpretResponse: function (serverResponse, { bidRequests }) {
if (isEmpty(serverResponse.body)) {
return [];
}
const { seatbid, cur } = serverResponse.body;
const bidResponses = (typeof seatbid != 'undefined') ? flatten(seatbid.map(seat => seat.bid)).reduce((result, bid) => {
result[bid.impid - 1] = bid;
return result;
}, []) : [];
return bidRequests
.map((bidRequest, id) => {
const bidResponse = bidResponses[id];
const type = bidRequest.nativeParams ? NATIVE : BANNER;
if (bidResponse) {
const bidObject = {
requestId: bidRequest.bidId, // TODO get this value from response?
cpm: bidResponse.price,
creativeId: bidResponse.crid,
ttl: 600,
netRevenue: true,
currency: cur,
mediaType: type,
meta: {
advertiserDomains: bidResponse.adomain && bidResponse.adomain.length > 0 ? bidResponse.adomain : []
}
};
if (type === NATIVE) {
bidObject.native = parseNative(bidResponse);
bidObject.mediaType = NATIVE;
}
if (type === BANNER) {
bidObject.ad = replaceAuctionPrice(bidResponse.adm, bidResponse.price);
bidObject.width = bidResponse.w;
bidObject.height = bidResponse.h;
bidObject.mediaType = BANNER;
}
return bidObject;
}
})
.filter(Boolean);
}
};
const getNativendoID = () => {
let nativendoID = storage.localStorageIsEnabled() &&
storage.getDataFromLocalStorage(NATIVENDO_KEY);
if (!nativendoID) {
if (storage.localStorageIsEnabled()) {
nativendoID = generateUUID();
storage.setDataInLocalStorage(NATIVENDO_KEY, nativendoID);
}
}
return nativendoID;
}
const getEids = (bidRequest) => {
const eids = [];
const nativendoID = getNativendoID();
if (nativendoID) {
const nativendoUserEid = {
source: 'nativendo.de',
uids: [
{
id: nativendoID,
atype: 1
}
]
};
eids.push(nativendoUserEid);
}
if (bidRequest.userIdAsEids) {
eids.push(bidRequest.userIdAsEids);
}
return eids;
}
function transformSizes(requestSizes) {
if (!isArray(requestSizes)) {
return [];
}
if (requestSizes.length === 2 && !isArray(requestSizes[0])) {
return [{
w: parseInt(requestSizes[0], 10),
h: parseInt(requestSizes[1], 10)
}];
} else if (isArray(requestSizes[0])) {
return requestSizes.map(item => ({
w: parseInt(item[0], 10),
h: parseInt(item[1], 10)
}));
}
return [];
}
function flatten(arr) {
return [].concat(...arr);
}
function parseNative(bid) {
const { assets, link, imptrackers } = bid.adm.native;
let clickUrl = link.url.replace(/\$\{AUCTION_PRICE\}/g, bid.price);
if (link.clicktrackers) {
link.clicktrackers.forEach(function (clicktracker, index) {
link.clicktrackers[index] = clicktracker.replace(/\$\{AUCTION_PRICE\}/g, bid.price);
});
}
if (imptrackers) {
imptrackers.forEach(function (imptracker, index) {
imptrackers[index] = imptracker.replace(/\$\{AUCTION_PRICE\}/g, bid.price);
});
}
const result = {
url: clickUrl,
clickUrl: clickUrl,
clickTrackers: link.clicktrackers || undefined,
impressionTrackers: imptrackers || 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;
}
registerBidder(spec);