forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
distroscaleBidAdapter.js
283 lines (254 loc) · 7.67 KB
/
distroscaleBidAdapter.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
import { logWarn, isPlainObject, isStr, isArray, isFn, inIframe, mergeDeep, deepSetValue, logError, deepClone } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { BANNER } from '../src/mediaTypes.js';
const BIDDER_CODE = 'distroscale';
const SHORT_CODE = 'ds';
const LOG_WARN_PREFIX = 'DistroScale: ';
const ENDPOINT = 'https://hb.jsrdn.com/hb?from=pbjs';
const DEFAULT_CURRENCY = 'USD';
const AUCTION_TYPE = 1;
const GVLID = 754;
const UNDEF = undefined;
const SUPPORTED_MEDIATYPES = [ BANNER ];
function _getHost(url) {
let a = document.createElement('a');
a.href = url;
return a.hostname;
}
function _getBidFloor(bid, mType, sz) {
if (isFn(bid.getFloor)) {
let floor = bid.getFloor({
currency: DEFAULT_CURRENCY,
mediaType: mType || '*',
size: sz || '*'
});
if (isPlainObject(floor) && !isNaN(floor.floor) && floor.currency === DEFAULT_CURRENCY) {
return floor.floor;
}
}
return null;
}
function _createImpressionObject(bid) {
var impObj = UNDEF;
var i;
var sizes = {};
var sizesCount = 0;
function addSize(arr) {
var w, h;
if (arr && arr.length > 1) {
w = parseInt(arr[0]);
h = parseInt(arr[1]);
}
sizes[w + 'x' + h] = {
w: w,
h: h,
area: w * h,
idx:
({
'970x250': 1,
'300x250': 2
})[w + 'x' + h] || Math.max(w * h, 200)
};
sizesCount++;
}
// Gather all sizes
if (isArray(bid.sizes)) {
for (i = 0; i < bid.sizes.length; i++) {
addSize(bid.sizes[i]);
}
}
if (bid.params && bid.params.width && bid.params.height) {
addSize([bid.params.width, bid.params.height]);
}
if (bid.mediaTypes && BANNER in bid.mediaTypes && bid.mediaTypes[BANNER] && bid.mediaTypes[BANNER].sizes) {
for (i = 0; i < bid.mediaTypes[BANNER].sizes.length; i++) {
addSize(bid.mediaTypes[BANNER].sizes[i]);
}
}
if (sizesCount == 0) {
logWarn(LOG_WARN_PREFIX + 'Error: missing sizes: ' + bid.params.adUnit + '. Ignoring the banner impression in the adunit.');
} else {
// Use the first preferred size
var keys = Object.keys(sizes);
keys.sort(function(a, b) {
return sizes[a].idx - sizes[b].idx
});
var bannerObj = {
pos: 0,
w: sizes[keys[0]].w,
h: sizes[keys[0]].h,
topframe: inIframe() ? 0 : 1,
format: [{
'w': sizes[keys[0]].w,
'h': sizes[keys[0]].h
}]
};
impObj = {
id: bid.bidId,
tagid: bid.params.zoneid || '',
secure: 1,
ext: {
pubid: bid.params.pubid || '',
zoneid: bid.params.zoneid || ''
}
};
var floor = _getBidFloor(bid, BANNER, [sizes[keys[0]].w, sizes[keys[0]].h]);
if (floor > 0) {
impObj.bidfloor = floor;
impObj.bidfloorcur = DEFAULT_CURRENCY;
}
impObj[BANNER] = bannerObj;
}
return impObj;
}
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: SUPPORTED_MEDIATYPES,
aliases: [SHORT_CODE],
isBidRequestValid: bid => {
if (bid && bid.params && bid.params.pubid && isStr(bid.params.pubid)) {
return true;
} else {
logWarn(LOG_WARN_PREFIX + 'Error: pubid is mandatory and cannot be numeric');
}
return false;
},
buildRequests: (validBidRequests, bidderRequest) => {
// TODO: does the fallback to window.location make sense?
var pageUrl = bidderRequest?.refererInfo?.page || window.location.href;
// check if dstag is already loaded in ancestry tree
var dsloaded = 0;
try {
var win = window;
while (true) {
if (win.vx.cs_loaded) {
dsloaded = 1;
}
if (win != win.parent) {
win = win.parent;
} else {
break;
}
}
} catch (error) {
// ignore exception
}
var payload = {
id: '' + (new Date()).getTime(),
at: AUCTION_TYPE,
cur: [DEFAULT_CURRENCY],
site: {
page: pageUrl
},
device: {
ua: navigator.userAgent,
js: 1,
h: screen.height,
w: screen.width,
language: (navigator.language && navigator.language.replace(/-.*/, '')) || 'en',
dnt: (navigator.doNotTrack == '1' || navigator.msDoNotTrack == '1' || navigator.doNotTrack == 'yes') ? 1 : 0
},
imp: [],
user: {},
ext: {
dsloaded: dsloaded
}
};
validBidRequests.forEach(b => {
var bid = deepClone(b);
var impObj = _createImpressionObject(bid);
if (impObj) {
payload.imp.push(impObj);
}
});
if (payload.imp.length == 0) {
return;
}
payload.site.domain = _getHost(payload.site.page);
// add the content object from config in request
if (typeof config.getConfig('content') === 'object') {
payload.site.content = config.getConfig('content');
}
// merge the device from config.getConfig('device')
if (typeof config.getConfig('device') === 'object') {
payload.device = Object.assign(payload.device, config.getConfig('device'));
}
// adding schain object
if (validBidRequests[0].schain) {
deepSetValue(payload, 'source.schain', validBidRequests[0].schain);
}
// Attaching GDPR Consent Params
if (bidderRequest && bidderRequest.gdprConsent) {
deepSetValue(payload, 'user.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(payload, 'regs.gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}
// CCPA
if (bidderRequest && bidderRequest.uspConsent) {
deepSetValue(payload, 'regs.us_privacy', bidderRequest.uspConsent);
}
// coppa compliance
if (config.getConfig('coppa') === true) {
deepSetValue(payload, 'regs.coppa', 1);
}
// First Party Data
const commonFpd = bidderRequest.ortb2 || {};
if (commonFpd.site) {
mergeDeep(payload, {site: commonFpd.site});
}
if (commonFpd.user) {
mergeDeep(payload, {user: commonFpd.user});
}
// User IDs
if (validBidRequests[0].userIdAsEids && validBidRequests[0].userIdAsEids.length > 0) {
// Standard ORTB structure
deepSetValue(payload, 'user.eids', validBidRequests[0].userIdAsEids);
} else if (validBidRequests[0].userId && Object.keys(validBidRequests[0].userId).length > 0) {
// Fallback to non-ortb structure
deepSetValue(payload, 'user.ext.userId', validBidRequests[0].userId);
}
return {
method: 'POST',
url: ENDPOINT,
data: payload,
bidderRequest: bidderRequest
};
},
interpretResponse: (response, request) => {
const bidResponses = [];
try {
if (response.body && response.body.seatbid && isArray(response.body.seatbid)) {
// Supporting multiple bid responses for same adSize
response.body.seatbid.forEach(seatbidder => {
seatbidder.bid &&
isArray(seatbidder.bid) &&
seatbidder.bid.forEach(bid => {
let newBid = {
requestId: bid.impid,
cpm: (parseFloat(bid.price) || 0),
currency: DEFAULT_CURRENCY,
width: parseInt(bid.w),
height: parseInt(bid.h),
creativeId: bid.crid || bid.id,
netRevenue: true,
ttl: 300,
ad: bid.adm,
meta: {
advertiserDomains: []
}
};
if (isArray(bid.adomain) && bid.adomain.length > 0) {
newBid.meta.advertiserDomains = bid.adomain;
}
bidResponses.push(newBid);
});
});
}
} catch (error) {
logError(error);
}
return bidResponses;
}
};
registerBidder(spec);