forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridgewellBidAdapter.js
302 lines (265 loc) · 9.19 KB
/
bridgewellBidAdapter.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
import {_each, deepSetValue, inIframe} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE} from '../src/mediaTypes.js';
import {find} from '../src/polyfill.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
*/
const BIDDER_CODE = 'bridgewell';
const REQUEST_ENDPOINT = 'https://prebid.scupio.com/recweb/prebid.aspx?cb=';
const BIDDER_VERSION = '1.1.0';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, NATIVE],
/**
* 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) {
let valid = false;
if (bid && bid.params) {
if ((bid.params.cid) && (typeof bid.params.cid === 'number')) {
valid = true;
} else if (bid.params.ChannelID) {
valid = true;
}
}
return valid;
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} validBidRequests - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
const adUnits = [];
var bidderUrl = REQUEST_ENDPOINT + Math.random();
var userIds;
_each(validBidRequests, function (bid) {
userIds = bid.userId;
if (bid.params.cid) {
adUnits.push({
cid: bid.params.cid,
adUnitCode: bid.adUnitCode,
requestId: bid.bidId,
mediaTypes: bid.mediaTypes || {
banner: {
sizes: bid.sizes
}
},
userIds: userIds || {}
});
} else {
adUnits.push({
ChannelID: bid.params.ChannelID,
adUnitCode: bid.adUnitCode,
requestId: bid.bidId,
mediaTypes: bid.mediaTypes || {
banner: {
sizes: bid.sizes
}
},
userIds: userIds || {}
});
}
});
let topUrl = '';
if (bidderRequest && bidderRequest.refererInfo) {
topUrl = bidderRequest.refererInfo.page;
}
return {
method: 'POST',
url: bidderUrl,
data: {
version: {
prebid: '$prebid.version$',
bridgewell: BIDDER_VERSION
},
inIframe: inIframe(),
url: topUrl,
referrer: bidderRequest.refererInfo.ref,
adUnits: adUnits,
// TODO: please do not send internal data structures over the network
refererInfo: bidderRequest.refererInfo.legacy,
},
validBidRequests: validBidRequests
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @param {*} bidRequest
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequest) {
const bidResponses = [];
// map responses to requests
_each(bidRequest.validBidRequests, function (req) {
const bidResponse = {};
if (!serverResponse.body) {
return;
}
let matchedResponse = find(serverResponse.body, function (res) {
let valid = false;
if (res && !res.consumed) {
let mediaTypes = req.mediaTypes;
let adUnitCode = req.adUnitCode;
if (res.adUnitCode) {
return res.adUnitCode === adUnitCode;
} else if (res.width && res.height && mediaTypes) {
if (mediaTypes.native) { // dont care native sizes
valid = true;
} else if (mediaTypes.banner) {
if (mediaTypes.banner.sizes) {
let width = res.width;
let height = res.height;
let sizes = mediaTypes.banner.sizes;
// check response size validation
if (typeof sizes[0] === 'number') { // for foramt Array[Number] check
valid = width === sizes[0] && height === sizes[1];
} else { // for format Array[Array[Number]] check
valid = !!find(sizes, function (size) {
return (width === size[0] && height === size[1]);
});
}
}
}
}
}
return valid;
});
if (matchedResponse) {
matchedResponse.consumed = true;
// check required parameters
if (typeof matchedResponse.cpm !== 'number') {
return;
} else if (typeof matchedResponse.netRevenue !== 'boolean') {
return;
} else if (typeof matchedResponse.currency !== 'string') {
return;
} else if (typeof matchedResponse.mediaType !== 'string') {
return;
}
bidResponse.requestId = req.bidId;
bidResponse.cpm = matchedResponse.cpm;
bidResponse.width = matchedResponse.width;
bidResponse.height = matchedResponse.height;
bidResponse.ttl = matchedResponse.ttl;
bidResponse.creativeId = matchedResponse.id;
bidResponse.netRevenue = matchedResponse.netRevenue;
bidResponse.currency = matchedResponse.currency;
bidResponse.mediaType = matchedResponse.mediaType;
if (matchedResponse.adomain) {
deepSetValue(bidResponse, 'meta.advertiserDomains', Array.isArray(matchedResponse.adomain) ? matchedResponse.adomain : [matchedResponse.adomain]);
}
// check required parameters by matchedResponse.mediaType
switch (matchedResponse.mediaType) {
case BANNER:
// check banner required parameters
if (typeof matchedResponse.ad !== 'string') {
return;
}
bidResponse.ad = matchedResponse.ad;
break;
case NATIVE:
// check native required parameters
if (!matchedResponse.native) {
return;
}
let reqNativeLayout = req.mediaTypes.native;
let resNative = matchedResponse.native;
// check title
let title = reqNativeLayout.title;
if (title && title.required) {
if (typeof resNative.title !== 'string') {
return;
} else if (title.len && title.len < resNative.title.length) {
return;
}
}
// check body
let body = reqNativeLayout.body;
if (body && body.required) {
if (typeof resNative.body !== 'string') {
return;
}
}
// check image
let image = reqNativeLayout.image;
if (image && image.required) {
if (resNative.image) {
if (typeof resNative.image.url !== 'string') { // check image url
return;
} else {
if (resNative.image.width !== image.sizes[0] || resNative.image.height !== image.sizes[1]) { // check image sizes
return;
}
}
} else {
return;
}
}
// check sponsoredBy
let sponsoredBy = reqNativeLayout.sponsoredBy;
if (sponsoredBy && sponsoredBy.required) {
if (typeof resNative.sponsoredBy !== 'string') {
return;
}
}
// check icon
let icon = reqNativeLayout.icon;
if (icon && icon.required) {
if (resNative.icon) {
if (typeof resNative.icon.url !== 'string') { // check icon url
return;
} else {
if (resNative.icon.width !== icon.sizes[0] || resNative.icon.height !== icon.sizes[0]) { // check image sizes
return;
}
}
} else {
return;
}
}
// check clickUrl
if (typeof resNative.clickUrl !== 'string') {
return;
}
// check clickTracker
let clickTrackers = resNative.clickTrackers;
if (clickTrackers) {
if (clickTrackers.length === 0) {
return;
}
} else {
return;
}
// check impressionTrackers
let impressionTrackers = resNative.impressionTrackers;
if (impressionTrackers) {
if (impressionTrackers.length === 0) {
return;
}
} else {
return;
}
bidResponse.native = matchedResponse.native;
break;
default: // response mediaType is not supported
return;
}
bidResponses.push(bidResponse);
}
});
return bidResponses;
}
};
registerBidder(spec);