forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversantBidAdapter.js
327 lines (295 loc) · 9.71 KB
/
conversantBidAdapter.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
import {
buildUrl,
deepAccess,
deepSetValue,
getBidIdParameter,
isArray,
isFn,
isPlainObject,
isStr,
logError,
logWarn,
mergeDeep,
parseUrl,
} 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 {ortbConverter} from '../libraries/ortbConverter/converter.js';
import {ORTB_MTYPES} from '../libraries/ortbConverter/processors/mediaType.js';
// Maintainer: [email protected]
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerRequest} ServerRequest
* @typedef {import('../src/adapters/bidderFactory.js').Device} Device
*/
const GVLID = 24;
const BIDDER_CODE = 'conversant';
export const storage = getStorageManager({gvlid: GVLID, bidderCode: BIDDER_CODE});
const URL = 'https://web.hb.ad.cpe.dotomi.com/cvx/client/hb/ortb/25';
function setSiteId(bidRequest, request) {
if (bidRequest.params.site_id) {
if (request.site) {
request.site.id = bidRequest.params.site_id;
}
if (request.app) {
request.app.id = bidRequest.params.site_id;
}
}
}
function setPubcid(bidRequest, request) {
// Add common id if available
const pubcid = getPubcid(bidRequest);
if (pubcid) {
deepSetValue(request, 'user.ext.fpc', pubcid);
}
}
const converter = ortbConverter({
context: {
netRevenue: true,
ttl: 300
},
request: function (buildRequest, imps, bidderRequest, context) {
const request = buildRequest(imps, bidderRequest, context);
request.at = 1;
request.cur = ['USD'];
if (context.bidRequests) {
const bidRequest = context.bidRequests[0];
setSiteId(bidRequest, request);
setPubcid(bidRequest, request);
}
return request;
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
const data = {
secure: 1,
bidfloor: getBidFloor(bidRequest) || 0,
displaymanager: 'Prebid.js',
displaymanagerver: '$prebid.version$'
};
copyOptProperty(bidRequest.params.tag_id, data, 'tagid');
mergeDeep(imp, data, imp);
return imp;
},
bidResponse: function (buildBidResponse, bid, context) {
if (!bid.price) return;
// ensure that context.mediaType is set to banner or video otherwise
if (!context.mediaType && context.bidRequest.mediaTypes) {
const [type] = Object.keys(context.bidRequest.mediaTypes);
if (Object.values(ORTB_MTYPES).includes(type)) {
context.mediaType = type;
}
}
const bidResponse = buildBidResponse(bid, context);
return bidResponse;
},
response(buildResponse, bidResponses, ortbResponse, context) {
const response = buildResponse(bidResponses, ortbResponse, context);
return response;
},
overrides: {
imp: {
banner(fillBannerImp, imp, bidRequest, context) {
if (bidRequest.mediaTypes && !bidRequest.mediaTypes.banner) return;
if (bidRequest.params.position) {
// fillBannerImp looks for mediaTypes.banner.pos so put it under the right name here
mergeDeep(bidRequest, {mediaTypes: {banner: {pos: bidRequest.params.position}}});
}
fillBannerImp(imp, bidRequest, context);
},
video(fillVideoImp, imp, bidRequest, context) {
if (bidRequest.mediaTypes && !bidRequest.mediaTypes.video) return;
const videoData = {};
copyOptProperty(bidRequest.params?.position, videoData, 'pos');
copyOptProperty(bidRequest.params?.mimes, videoData, 'mimes');
copyOptProperty(bidRequest.params?.maxduration, videoData, 'maxduration');
copyOptProperty(bidRequest.params?.protocols, videoData, 'protocols');
copyOptProperty(bidRequest.params?.api, videoData, 'api');
imp.video = mergeDeep(videoData, imp.video);
fillVideoImp(imp, bidRequest, context);
}
},
}
});
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
aliases: ['cnvr', 'epsilon'], // short code
supportedMediaTypes: [BANNER, VIDEO],
/**
* 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) {
if (!bid || !bid.params) {
logWarn(BIDDER_CODE + ': Missing bid parameters');
return false;
}
if (!isStr(bid.params.site_id)) {
logWarn(BIDDER_CODE + ': site_id must be specified as a string');
return false;
}
if (isVideoRequest(bid)) {
const mimes = bid.params.mimes || deepAccess(bid, 'mediaTypes.video.mimes');
if (!mimes) {
// Give a warning but let it pass
logWarn(BIDDER_CODE + ': mimes should be specified for videos');
} else if (!isArray(mimes) || !mimes.every(s => isStr(s))) {
logWarn(BIDDER_CODE + ': mimes must be an array of strings');
return false;
}
}
return true;
},
buildRequests: function(bidRequests, bidderRequest) {
const payload = converter.toORTB({bidderRequest, bidRequests});
const result = {
method: 'POST',
url: makeBidUrl(bidRequests[0]),
data: payload,
};
return result;
},
/**
* 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 ortbBids = converter.fromORTB({request: bidRequest.data, response: serverResponse.body});
return ortbBids;
},
/**
* Register User Sync.
*/
getUserSyncs: function(syncOptions, responses, gdprConsent, uspConsent) {
let params = {};
const syncs = [];
// Attaching GDPR Consent Params in UserSync url
if (gdprConsent) {
params.gdpr = (gdprConsent.gdprApplies) ? 1 : 0;
params.gdpr_consent = encodeURIComponent(gdprConsent.consentString || '');
}
// CCPA
if (uspConsent) {
params.us_privacy = encodeURIComponent(uspConsent);
}
if (responses && responses.ext) {
const pixels = [{urls: responses.ext.fsyncs, type: 'iframe'}, {urls: responses.ext.psyncs, type: 'image'}]
.filter((entry) => {
return entry.urls &&
((entry.type === 'iframe' && syncOptions.iframeEnabled) ||
(entry.type === 'image' && syncOptions.pixelEnabled));
})
.map((entry) => {
return entry.urls.map((endpoint) => {
let urlInfo = parseUrl(endpoint);
mergeDeep(urlInfo.search, params);
if (Object.keys(urlInfo.search).length === 0) {
delete urlInfo.search; // empty search object causes buildUrl to add a trailing ? to the url
}
return {type: entry.type, url: buildUrl(urlInfo)};
})
.reduce((x, y) => x.concat(y), []);
})
.reduce((x, y) => x.concat(y), []);
syncs.push(...pixels);
}
return syncs;
}
};
function getPubcid(bidRequest) {
let pubcid = null;
if (bidRequest.userId && bidRequest.userId.pubcid) {
pubcid = bidRequest.userId.pubcid;
} else if (bidRequest.crumbs && bidRequest.crumbs.pubcid) {
pubcid = bidRequest.crumbs.pubcid;
}
if (!pubcid) {
const pubcidName = getBidIdParameter('pubcid_name', bidRequest.params) || '_pubcid';
pubcid = readStoredValue(pubcidName);
}
return pubcid;
}
/**
* Check if it's a video bid request
*
* @param {BidRequest} bid - Bid request generated from ad slots
* @returns {boolean} True if it's a video bid
*/
function isVideoRequest(bid) {
return bid.mediaType === 'video' || !!deepAccess(bid, 'mediaTypes.video');
}
/**
* Copy property if exists from src to dst
*
* @param {object} src - source object
* @param {object} dst - destination object
* @param {string} dstName - destination property name
*/
function copyOptProperty(src, dst, dstName) {
if (src) {
dst[dstName] = src;
}
}
/**
* Look for a stored value from both cookie and local storage and return the first value found.
* @param key Key for the search
* @return {string} Stored value
*/
function readStoredValue(key) {
let storedValue;
try {
// check cookies first
storedValue = storage.getCookie(key);
if (!storedValue) {
// check expiration time before reading local storage
const storedValueExp = storage.getDataFromLocalStorage(`${key}_exp`);
if (storedValueExp === '' || (storedValueExp && (new Date(storedValueExp)).getTime() - Date.now() > 0)) {
storedValue = storage.getDataFromLocalStorage(key);
storedValue = storedValue ? decodeURIComponent(storedValue) : storedValue;
}
}
// deserialize JSON if needed
if (isStr(storedValue) && storedValue.charAt(0) === '{') {
storedValue = JSON.parse(storedValue);
}
} catch (e) {
logError(e);
}
return storedValue;
}
/**
* Get the floor price from bid.params for backward compatibility.
* If not found, then check floor module.
* @param bid A valid bid object
* @returns {*|number} floor price
*/
function getBidFloor(bid) {
let floor = getBidIdParameter('bidfloor', bid.params);
if (!floor && isFn(bid.getFloor)) {
const floorObj = bid.getFloor({
currency: 'USD',
mediaType: '*',
size: '*'
});
if (isPlainObject(floorObj) && !isNaN(floorObj.floor) && floorObj.currency === 'USD') {
floor = floorObj.floor;
}
}
return floor
}
function makeBidUrl(bid) {
let bidurl = URL;
if (bid.params.white_label_url) {
bidurl = bid.params.white_label_url;
}
return bidurl;
}
registerBidder(spec);