-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
seedingAllianceBidAdapter.js
executable file
·244 lines (202 loc) · 6.57 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
// jshint esversion: 6, es3: false, node: true
'use strict';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE} from '../src/mediaTypes.js';
import {generateUUID, deepSetValue, isEmpty, replaceAuctionPrice} from '../src/utils.js';
import {config} from '../src/config.js';
import {getStorageManager} from '../src/storageManager.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.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';
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
const converter = ortbConverter({
context: {
ttl: 360,
netRevenue: true
},
request(buildRequest, imps, bidderRequest, context) {
const request = buildRequest(imps, bidderRequest, context);
// set basic page, this might be updated later by adunit param
deepSetValue(request, 'site.page', bidderRequest.refererInfo.page);
deepSetValue(request, 'regs.ext.pb_ver', '$prebid.version$');
deepSetValue(request, 'cur', [config.getConfig('currency') || DEFAULT_CUR]);
// As this is client side, we get needed info from headers
delete request.device;
return request;
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
// add tagid from params
imp.tagid = bidRequest.params.adUnitId;
return imp;
}
});
export const spec = {
code: BIDDER_CODE,
gvlid: GVL_ID,
supportedMediaTypes: [NATIVE, BANNER],
isBidRequestValid: function (bid) {
return !!bid.params.adUnitId;
},
buildRequests: (validBidRequests = [], bidderRequest) => {
const oRtbRequest = converter.toORTB({bidRequests: validBidRequests, bidderRequest});
let eids = getEids(validBidRequests[0]);
// check for url in params and set in site object
validBidRequests.forEach(bidRequest => {
if (bidRequest.params.url) {
deepSetValue(oRtbRequest, 'site.page', bidRequest.params.url);
}
});
if (bidderRequest.gdprConsent) {
oRtbRequest.user = {};
deepSetValue(oRtbRequest, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(oRtbRequest, 'regs.ext.gdpr', (typeof bidderRequest.gdprConsent.gdprApplies === 'boolean' && bidderRequest.gdprConsent.gdprApplies) ? 1 : 0);
deepSetValue(oRtbRequest, 'user.ext.eids', eids);
}
let endpoint = config.getConfig('seedingAlliance.endpoint') || ENDPOINT_URL;
return {
method: 'POST',
url: endpoint,
data: JSON.stringify(oRtbRequest),
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] = bid;
return result;
}, []) : [];
return bidRequests
.map((bidRequest) => {
const bidId = bidRequest.bidId;
const bidResponse = bidResponses[bidId];
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, bidRequest.nativeParams);
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 flatten(arr) {
return [].concat(...arr);
}
function parseNative(bid, nativeParams) {
let native;
if (typeof bid.adm === 'string') {
try {
native = JSON.parse(bid.adm).native;
} catch (e) {
return;
}
} else {
native = bid.adm.native;
}
const { assets, link, imptrackers } = 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
};
let nativeParamKeys = Object.keys(nativeParams);
let id = 0;
nativeParamKeys.forEach(nativeParam => {
assets.forEach(asset => {
if (asset.id == id) {
switch (nativeParam) {
case 'title':
result.title = asset.title.text;
break;
case 'body':
case 'cta':
case 'sponsoredBy':
result[nativeParam] = asset.data.value;
break;
case 'image':
case 'icon':
result[nativeParam] = {
url: asset.img.url,
width: asset.img.w,
height: asset.img.h
};
break;
}
}
});
id++;
});
return result;
}
registerBidder(spec);