-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
kueezRtbBidAdapter.js
348 lines (305 loc) · 8.93 KB
/
kueezRtbBidAdapter.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
import {_each, deepAccess, parseSizesInput, parseUrl, uniques, isFn} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {getStorageManager} from '../src/storageManager.js';
import {config} from '../src/config.js';
const GVLID = 1165;
const DEFAULT_SUB_DOMAIN = 'exchange';
const BIDDER_CODE = 'kueezrtb';
const BIDDER_VERSION = '1.0.0';
const CURRENCY = 'USD';
const TTL_SECONDS = 60 * 5;
const UNIQUE_DEAL_ID_EXPIRY = 1000 * 60 * 15;
const storage = getStorageManager({bidderCode: BIDDER_CODE});
function getTopWindowQueryParams() {
try {
const parsedUrl = parseUrl(window.top.document.URL, {decodeSearchAsString: true});
return parsedUrl.search;
} catch (e) {
return '';
}
}
export function createDomain(subDomain = DEFAULT_SUB_DOMAIN) {
return `https://${subDomain}.kueezrtb.com`;
}
export function extractCID(params) {
return params.cId || params.CID || params.cID || params.CId || params.cid || params.ciD || params.Cid || params.CiD;
}
export function extractPID(params) {
return params.pId || params.PID || params.pID || params.PId || params.pid || params.piD || params.Pid || params.PiD;
}
export function extractSubDomain(params) {
return params.subDomain || params.SubDomain || params.Subdomain || params.subdomain || params.SUBDOMAIN || params.subDOMAIN;
}
function isBidRequestValid(bid) {
const params = bid.params || {};
return !!(extractCID(params) && extractPID(params));
}
function buildRequest(bid, topWindowUrl, sizes, bidderRequest, bidderTimeout) {
const {
params,
bidId,
userId,
adUnitCode,
schain,
mediaTypes,
auctionId,
transactionId,
bidderRequestId,
bidRequestsCount,
bidderRequestsCount,
bidderWinsCount
} = bid;
let {bidFloor, ext} = params;
const hashUrl = hashCode(topWindowUrl);
const uniqueDealId = getUniqueDealId(hashUrl);
const cId = extractCID(params);
const pId = extractPID(params);
const subDomain = extractSubDomain(params);
const gpid = deepAccess(bid, 'ortb2Imp.ext.gpid') || deepAccess(bid, 'ortb2Imp.ext.data.pbadslot', '');
if (isFn(bid.getFloor)) {
const floorInfo = bid.getFloor({
currency: 'USD',
mediaType: '*',
size: '*'
});
if (floorInfo.currency === 'USD') {
bidFloor = floorInfo.floor;
}
}
let data = {
url: encodeURIComponent(topWindowUrl),
uqs: getTopWindowQueryParams(),
cb: Date.now(),
bidFloor: bidFloor,
bidId: bidId,
referrer: bidderRequest.refererInfo.ref,
adUnitCode: adUnitCode,
publisherId: pId,
sizes: sizes,
uniqueDealId: uniqueDealId,
bidderVersion: BIDDER_VERSION,
prebidVersion: '$prebid.version$',
res: `${screen.width}x${screen.height}`,
schain: schain,
mediaTypes: mediaTypes,
gpid: gpid,
// TODO: fix auctionId/transactionId leak: https://github.com/prebid/Prebid.js/issues/9781
auctionId: auctionId,
transactionId: transactionId,
bidderRequestId: bidderRequestId,
bidRequestsCount: bidRequestsCount,
bidderRequestsCount: bidderRequestsCount,
bidderWinsCount: bidderWinsCount,
bidderTimeout: bidderTimeout
};
appendUserIdsToRequestPayload(data, userId);
const sua = deepAccess(bidderRequest, 'ortb2.device.sua');
if (sua) {
data.sua = sua;
}
if (bidderRequest.gdprConsent) {
if (bidderRequest.gdprConsent.consentString) {
data.gdprConsent = bidderRequest.gdprConsent.consentString;
}
if (bidderRequest.gdprConsent.gdprApplies !== undefined) {
data.gdpr = bidderRequest.gdprConsent.gdprApplies ? 1 : 0;
}
}
if (bidderRequest.uspConsent) {
data.usPrivacy = bidderRequest.uspConsent;
}
if (bidderRequest.gppConsent) {
data.gppString = bidderRequest.gppConsent.gppString;
data.gppSid = bidderRequest.gppConsent.applicableSections;
} else if (bidderRequest.ortb2?.regs?.gpp) {
data.gppString = bidderRequest.ortb2.regs.gpp;
data.gppSid = bidderRequest.ortb2.regs.gpp_sid;
}
const dto = {
method: 'POST',
url: `${createDomain(subDomain)}/prebid/multi/${cId}`,
data: data
};
_each(ext, (value, key) => {
dto.data['ext.' + key] = value;
});
return dto;
}
function appendUserIdsToRequestPayload(payloadRef, userIds) {
let key;
_each(userIds, (userId, idSystemProviderName) => {
key = `uid.${idSystemProviderName}`;
switch (idSystemProviderName) {
case 'digitrustid':
payloadRef[key] = deepAccess(userId, 'data.id');
break;
case 'lipb':
payloadRef[key] = userId.lipbid;
break;
case 'parrableId':
payloadRef[key] = userId.eid;
break;
case 'id5id':
payloadRef[key] = userId.uid;
break;
default:
payloadRef[key] = userId;
}
});
}
function buildRequests(validBidRequests, bidderRequest) {
const topWindowUrl = bidderRequest.refererInfo.page || bidderRequest.refererInfo.topmostLocation;
const bidderTimeout = bidderRequest.timeout ?? config.getConfig('bidderTimeout');
const requests = [];
validBidRequests.forEach(validBidRequest => {
const sizes = parseSizesInput(validBidRequest.sizes);
const request = buildRequest(validBidRequest, topWindowUrl, sizes, bidderRequest, bidderTimeout);
requests.push(request);
});
return requests;
}
function interpretResponse(serverResponse, request) {
if (!serverResponse || !serverResponse.body) {
return [];
}
const {bidId} = request.data;
const {results} = serverResponse.body;
let output = [];
try {
results.forEach(result => {
const {
creativeId,
ad,
price,
exp,
width,
height,
currency,
metaData,
advertiserDomains,
mediaType = BANNER
} = result;
if (!ad || !price) {
return;
}
const response = {
requestId: bidId,
cpm: price,
width: width,
height: height,
creativeId: creativeId,
currency: currency || CURRENCY,
netRevenue: true,
ttl: exp || TTL_SECONDS,
};
if (metaData) {
Object.assign(response, {
meta: metaData
})
} else {
Object.assign(response, {
meta: {
advertiserDomains: advertiserDomains || []
}
})
}
if (mediaType === BANNER) {
Object.assign(response, {
ad: ad,
});
} else {
Object.assign(response, {
vastXml: ad,
mediaType: VIDEO
});
}
output.push(response);
});
return output;
} catch (e) {
return [];
}
}
function getUserSyncs(syncOptions, responses, gdprConsent = {}, uspConsent = '', gppConsent = {}) {
let syncs = [];
const {iframeEnabled, pixelEnabled} = syncOptions;
const {gdprApplies, consentString = ''} = gdprConsent;
const {gppString, applicableSections} = gppConsent;
const cidArr = responses.filter(resp => deepAccess(resp, 'body.cid')).map(resp => resp.body.cid).filter(uniques);
let params = `?cid=${encodeURIComponent(cidArr.join(','))}&gdpr=${gdprApplies ? 1 : 0}&gdpr_consent=${encodeURIComponent(consentString || '')}&us_privacy=${encodeURIComponent(uspConsent || '')}`
if (gppString && applicableSections?.length) {
params += '&gpp=' + encodeURIComponent(gppString);
params += '&gpp_sid=' + encodeURIComponent(applicableSections.join(','));
}
if (iframeEnabled) {
syncs.push({
type: 'iframe',
url: `https://sync.kueezrtb.com/api/sync/iframe/${params}`
});
}
if (pixelEnabled) {
syncs.push({
type: 'image',
url: `https://sync.kueezrtb.com/api/sync/image/${params}`
});
}
return syncs;
}
export function hashCode(s, prefix = '_') {
const l = s.length;
let h = 0
let i = 0;
if (l > 0) {
while (i < l) {
h = (h << 5) - h + s.charCodeAt(i++) | 0;
}
}
return prefix + h;
}
export function getUniqueDealId(key, expiry = UNIQUE_DEAL_ID_EXPIRY) {
const storageKey = `u_${key}`;
const now = Date.now();
const data = getStorageItem(storageKey);
let uniqueId;
if (!data || !data.value || now - data.created > expiry) {
uniqueId = `${key}_${now.toString()}`;
setStorageItem(storageKey, uniqueId);
} else {
uniqueId = data.value;
}
return uniqueId;
}
export function getStorageItem(key) {
try {
return tryParseJSON(storage.getDataFromLocalStorage(key));
} catch (e) {
}
return null;
}
export function setStorageItem(key, value, timestamp) {
try {
const created = timestamp || Date.now();
const data = JSON.stringify({value, created});
storage.setDataInLocalStorage(key, data);
} catch (e) {
}
}
export function tryParseJSON(value) {
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}
export const spec = {
code: BIDDER_CODE,
version: BIDDER_VERSION,
gvlid: GVLID,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs
};
registerBidder(spec);