forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apacdexBidAdapter.js
348 lines (314 loc) · 9.46 KB
/
apacdexBidAdapter.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 { deepAccess, isPlainObject, isArray, replaceAuctionPrice, isFn, logError, deepClone } from '../src/utils.js';
import { config } from '../src/config.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import {hasPurpose1Consent} from '../src/utils/gdpr.js';
import {parseDomain} from '../src/refererDetection.js';
const BIDDER_CODE = 'apacdex';
const ENDPOINT = 'https://useast.quantumdex.io/auction/pbjs'
const USERSYNC = 'https://sync.quantumdex.io/usersync/pbjs'
var bySlotTargetKey = {};
var bySlotSizesCount = {}
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: ['banner', 'video'],
aliases: ['quantumdex', 'valueimpression'],
isBidRequestValid: function (bid) {
if (!bid.params) {
return false;
}
if (!bid.params.siteId && !bid.params.placementId) {
return false;
}
if (!deepAccess(bid, 'mediaTypes.banner') && !deepAccess(bid, 'mediaTypes.video')) {
return false;
}
if (deepAccess(bid, 'mediaTypes.banner')) { // Not support multi type bids, favor banner over video
if (!deepAccess(bid, 'mediaTypes.banner.sizes')) {
// sizes at the banner is required.
return false;
}
} else if (deepAccess(bid, 'mediaTypes.video')) {
if (!deepAccess(bid, 'mediaTypes.video.playerSize')) {
// playerSize is required for instream adUnits.
return false;
}
}
return true;
},
buildRequests: function (validBidRequests, bidderRequest) {
let schain;
let eids;
let geo;
let test;
let bids = [];
test = config.getConfig('debug');
validBidRequests.forEach(bidReq => {
if (bidReq.schain) {
schain = schain || bidReq.schain
}
if (bidReq.userIdAsEids) {
eids = eids || bidReq.userIdAsEids
}
if (bidReq.params && bidReq.params.geo) {
if (validateGeoObject(bidReq.params.geo)) {
geo = bidReq.params.geo;
}
}
var targetKey = 0;
if (bySlotTargetKey[bidReq.adUnitCode] != undefined) {
targetKey = bySlotTargetKey[bidReq.adUnitCode];
} else {
var biggestSize = _getBiggestSize(bidReq.sizes);
if (biggestSize) {
if (bySlotSizesCount[biggestSize] != undefined) {
bySlotSizesCount[biggestSize]++
targetKey = bySlotSizesCount[biggestSize];
} else {
bySlotSizesCount[biggestSize] = 0;
targetKey = 0
}
}
}
bySlotTargetKey[bidReq.adUnitCode] = targetKey;
bidReq.targetKey = targetKey;
let bidFloor = getBidFloor(bidReq);
if (bidFloor) {
bidReq.bidFloor = bidFloor;
}
bids.push(deepClone(bidReq));
});
const payload = {};
payload.tmax = bidderRequest.timeout;
if (test) {
payload.test = 1;
}
payload.device = {};
payload.device.ua = navigator.userAgent;
payload.device.height = window.screen.height;
payload.device.width = window.screen.width;
payload.device.dnt = _getDoNotTrack();
payload.device.language = navigator.language;
var pageUrl = _extractTopWindowUrlFromBidderRequest(bidderRequest);
payload.site = {};
payload.site.page = pageUrl;
payload.site.referrer = _extractTopWindowReferrerFromBidderRequest(bidderRequest);
// TODO: does it make sense to fall back to window.location for the domain?
payload.site.hostname = bidderRequest.refererInfo?.domain || parseDomain(pageUrl);
// Apply GDPR parameters to request.
if (bidderRequest && bidderRequest.gdprConsent) {
payload.gdpr = {};
payload.gdpr.gdprApplies = !!bidderRequest.gdprConsent.gdprApplies;
if (bidderRequest.gdprConsent.consentString) {
payload.gdpr.consentString = bidderRequest.gdprConsent.consentString;
}
}
// Apply us_privacy.
if (bidderRequest && bidderRequest.uspConsent) {
payload.us_privacy = bidderRequest.uspConsent;
}
// Apply schain.
if (schain) {
payload.schain = schain;
}
// Apply eids.
if (eids) {
payload.eids = eids;
}
// Apply geo
if (geo) {
logError('apacdex adapter: Precise lat and long must be set on config; not on bidder parameters');
}
payload.bids = bids.map(function (bid) {
return {
params: bid.params,
mediaTypes: bid.mediaTypes,
transactionId: bid.ortb2Imp?.ext?.tid,
sizes: bid.sizes,
bidId: bid.bidId,
adUnitCode: bid.adUnitCode,
bidFloor: bid.bidFloor
}
});
return {
method: 'POST',
url: ENDPOINT,
data: payload,
withCredentials: true,
bidderRequests: bids
};
},
interpretResponse: function (serverResponse, bidRequest) {
const serverBody = serverResponse.body;
if (!serverBody || !isPlainObject(serverBody)) {
return [];
}
const serverBids = serverBody.bids;
if (!serverBids || !isArray(serverBids)) {
return [];
}
const bidResponses = [];
serverBids.forEach(bid => {
const dealId = bid.dealId || '';
const bidResponse = {
requestId: bid.requestId,
cpm: bid.cpm,
width: bid.width,
height: bid.height,
creativeId: bid.creativeId,
currency: bid.currency,
netRevenue: bid.netRevenue,
ttl: bid.ttl,
mediaType: bid.mediaType
};
if (dealId.length > 0) {
bidResponse.dealId = dealId;
}
if (bid.vastXml) {
bidResponse.vastXml = replaceAuctionPrice(bid.vastXml, bid.cpm);
} else {
bidResponse.ad = replaceAuctionPrice(bid.ad, bid.cpm);
}
bidResponse.meta = {};
if (bid.meta && bid.meta.advertiserDomains && isArray(bid.meta.advertiserDomains)) {
bidResponse.meta.advertiserDomains = bid.meta.advertiserDomains;
}
bidResponses.push(bidResponse);
});
return bidResponses;
},
getUserSyncs: function (syncOptions, serverResponses, gdprConsent, uspConsent) {
const syncs = [];
if (hasPurpose1Consent(gdprConsent)) {
let params = '';
if (gdprConsent && typeof gdprConsent.consentString === 'string') {
// add 'gdpr' only if 'gdprApplies' is defined
if (typeof gdprConsent.gdprApplies === 'boolean') {
params = `?gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${gdprConsent.consentString}`;
} else {
params = `?gdpr_consent=${gdprConsent.consentString}`;
}
}
if (uspConsent) {
params += `${params ? '&' : '?'}us_privacy=${encodeURIComponent(uspConsent)}`;
}
try {
if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: USERSYNC + params
});
}
if (serverResponses.length > 0 && serverResponses[0].body && serverResponses[0].body.pixel) {
serverResponses[0].body.pixel.forEach(px => {
if (px.type === 'image' && syncOptions.pixelEnabled) {
syncs.push({
type: 'image',
url: px.url + params
});
}
if (px.type === 'iframe' && syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: px.url + params
});
}
});
}
} catch (e) { }
}
return syncs;
}
};
function _getBiggestSize(sizes) {
if (sizes.length <= 0) return false
var acreage = 0;
var index = 0;
for (var i = 0; i < sizes.length; i++) {
var currentAcreage = sizes[i][0] * sizes[i][1];
if (currentAcreage >= acreage) {
acreage = currentAcreage;
index = i;
}
}
return sizes[index][0] + 'x' + sizes[index][1];
}
function _getDoNotTrack() {
try {
if (window.top.doNotTrack && window.top.doNotTrack == '1') {
return 1;
}
} catch (e) { }
try {
if (navigator.doNotTrack && (navigator.doNotTrack == 'yes' || navigator.doNotTrack == '1')) {
return 1;
}
} catch (e) { }
try {
if (navigator.msDoNotTrack && navigator.msDoNotTrack == '1') {
return 1;
}
} catch (e) { }
return 0
}
/**
* Extracts the page url from given bid request or use the (top) window location as fallback
*
* @param {*} bidderRequest
* @returns {string}
*/
function _extractTopWindowUrlFromBidderRequest(bidderRequest) {
// TODO: does it make sense to fall back to window.location?
return bidderRequest?.refererInfo?.page || window.location.href;
}
/**
* Extracts the referrer from given bid request or use the (top) document referrer as fallback
*
* @param {*} bidderRequest
* @returns {string}
*/
function _extractTopWindowReferrerFromBidderRequest(bidderRequest) {
// TODO: does it make sense to fall back to window.document.referrer?
return bidderRequest?.refererInfo?.ref || window.document.referrer;
}
/**
* Validate geo object
*
* @param {Object} geo
* @returns {boolean}
*/
export function validateGeoObject(geo) {
if (!isPlainObject(geo)) {
return false;
}
if (!geo.lat) {
return false;
}
if (!geo.lon) {
return false;
}
if (!geo.accuracy) {
return false;
}
return true;
}
/**
* Get bid floor from Price Floors Module
*
* @param {Object} bid
* @returns {?number}
*/
function getBidFloor(bid) {
if (!isFn(bid.getFloor)) {
return (bid.params.floorPrice) ? bid.params.floorPrice : null;
}
let floor = bid.getFloor({
currency: 'USD',
mediaType: '*',
size: '*'
});
if (isPlainObject(floor) && !isNaN(floor.floor) && floor.currency === 'USD') {
return floor.floor;
}
return null;
}
registerBidder(spec);