forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrichaudienceBidAdapter.js
384 lines (345 loc) · 11.9 KB
/
richaudienceBidAdapter.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import {deepAccess, isStr, triggerPixel} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {config} from '../src/config.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {Renderer} from '../src/Renderer.js';
import { getCurrencyFromBidderRequest } from '../libraries/ortb2Utils/currency.js';
const BIDDER_CODE = 'richaudience';
let REFERER = '';
export const spec = {
code: BIDDER_CODE,
gvlid: 108,
aliases: [{code: 'ra', gvlid: 108}],
supportedMediaTypes: [BANNER, VIDEO],
/***
* Determines whether or not the given bid request is valid
*
* @param {bidRequest} bid The bid params to validate.
* @returns {boolean} True if this is a valid bid, and false otherwise
*/
isBidRequestValid: function (bid) {
return !!(bid.params && bid.params.pid && bid.params.supplyType);
},
/***
* Build a server request from the list of valid BidRequests
* @param {validBidRequests} is an array of the valid bids
* @param {bidderRequest} bidder request object
* @returns {ServerRequest} Info describing the request to the server
*/
buildRequests: function (validBidRequests, bidderRequest) {
return validBidRequests.map(bid => {
var payload = {
bidfloor: raiGetFloor(bid, config),
ifa: bid.params.ifa,
pid: bid.params.pid,
supplyType: bid.params.supplyType,
currencyCode: getCurrencyFromBidderRequest(bidderRequest),
auctionId: bid.auctionId,
bidId: bid.bidId,
BidRequestsCount: bid.bidRequestsCount,
bidder: bid.bidder,
bidderRequestId: bid.bidderRequestId,
tagId: bid.adUnitCode,
sizes: raiGetSizes(bid),
referer: (typeof bidderRequest.refererInfo.page != 'undefined' ? encodeURIComponent(bidderRequest.refererInfo.page) : null),
numIframes: (typeof bidderRequest.refererInfo.numIframes != 'undefined' ? bidderRequest.refererInfo.numIframes : null),
transactionId: bid.ortb2Imp?.ext?.tid,
timeout: bidderRequest.timeout || 600,
user: raiSetEids(bid),
demand: raiGetDemandType(bid),
videoData: raiGetVideoInfo(bid),
scr_rsl: raiGetResolution(),
cpuc: (typeof window.navigator != 'undefined' ? window.navigator.hardwareConcurrency : null),
kws: bid.params.keywords,
schain: bid.schain,
gpid: raiSetPbAdSlot(bid),
dsa: setDSA(bid),
userData: deepAccess(bid, 'ortb2.user.data')
};
REFERER = (typeof bidderRequest.refererInfo.page != 'undefined' ? encodeURIComponent(bidderRequest.refererInfo.page) : null)
payload.gdpr_consent = '';
payload.gdpr = false;
if (bidderRequest && bidderRequest.gdprConsent) {
if (typeof bidderRequest.gdprConsent.gdprApplies != 'undefined') {
payload.gdpr = bidderRequest.gdprConsent.gdprApplies;
}
if (typeof bidderRequest.gdprConsent.consentString != 'undefined') {
payload.gdpr_consent = bidderRequest.gdprConsent.consentString;
}
}
if (bidderRequest?.gppConsent) {
payload.privacy = {
gpp: bidderRequest.gppConsent.gppString,
gpp_sid: bidderRequest.gppConsent.applicableSections
}
} else if (bidderRequest?.ortb2?.regs?.gpp) {
payload.privacy = {
gpp: bidderRequest.ortb2.regs.gpp,
gpp_sid: bidderRequest.ortb2.regs.gpp_sid
}
}
var payloadString = JSON.stringify(payload);
var endpoint = 'https://shb.richaudience.com/hb/';
return {
method: 'POST',
url: endpoint,
data: payloadString,
};
});
},
/***
* Read the response from the server and build a list of bids
* @param {serverResponse} Response from the server.
* @param {bidRequest} Bid request object
* @returns {bidResponses} Array of bids which were nested inside the server
*/
interpretResponse: function (serverResponse, bidRequest) {
const bidResponses = [];
// try catch
var response = serverResponse.body;
if (response) {
var bidResponse = {
requestId: JSON.parse(bidRequest.data).bidId,
cpm: response.cpm,
width: response.width,
height: response.height,
creativeId: response.creative_id,
mediaType: response.media_type,
netRevenue: response.netRevenue,
currency: response.currency,
ttl: response.ttl,
meta: {
advertiserDomains: [response.adomain[0]]
},
dealId: response.dealId
};
if (response.media_type === 'video') {
bidResponse.vastXml = response.vastXML;
try {
if (bidResponse.vastXml != null) {
if (JSON.parse(bidRequest.data).videoData.format == 'outstream' || JSON.parse(bidRequest.data).videoData.format == 'banner') {
bidResponse.renderer = Renderer.install({
id: bidRequest.bidId,
adunitcode: bidRequest.tagId,
loaded: false,
config: response.media_type,
url: 'https://cdn3.richaudience.com/prebidVideo/player.js'
});
}
bidResponse.renderer.setRender(renderer);
}
} catch (e) {
bidResponse.ad = response.adm;
}
} else {
bidResponse.ad = response.adm;
}
bidResponses.push(bidResponse);
}
return bidResponses;
},
/***
* User Syncs
*
* @param {syncOptions} Publisher prebid configuration
* @param {serverResponses} Response from the server
* @param {gdprConsent} GDPR consent object
* @returns {Array}
*/
getUserSyncs: function (syncOptions, responses, gdprConsent, uspConsent, gppConsent) {
const syncs = [];
var rand = Math.floor(Math.random() * 9999999999);
var syncUrl = '';
var consent = '';
var consentGPP = '';
var raiSync = {};
raiSync = raiGetSyncInclude(config);
if (gdprConsent && typeof gdprConsent.consentString === 'string' && typeof gdprConsent.consentString != 'undefined') {
consent = `consentString=${gdprConsent.consentString}`
}
// GPP Consent
if (gppConsent?.gppString && gppConsent?.applicableSections?.length) {
consentGPP = 'gpp=' + encodeURIComponent(gppConsent.gppString);
consentGPP += '&gpp_sid=' + encodeURIComponent(gppConsent?.applicableSections?.join(','));
}
if (syncOptions.iframeEnabled && raiSync.raiIframe != 'exclude') {
syncUrl = 'https://sync.richaudience.com/dcf3528a0b8aa83634892d50e91c306e/?ord=' + rand
if (consent != '') {
syncUrl += `&${consent}`
}
if (consentGPP != '') {
syncUrl += `&${consentGPP}`
}
syncs.push({
type: 'iframe',
url: syncUrl
});
}
if (syncOptions.pixelEnabled && REFERER != null && syncs.length == 0 && raiSync.raiImage != 'exclude') {
syncUrl = `https://sync.richaudience.com/bf7c142f4339da0278e83698a02b0854/?referrer=${REFERER}`;
if (consent != '') {
syncUrl += `&${consent}`
}
if (consentGPP != '') {
syncUrl += `&${consentGPP}`
}
syncs.push({
type: 'image',
url: syncUrl
});
}
return syncs
},
onTimeout: function (data) {
let url = raiGetTimeoutURL(data);
if (url) {
triggerPixel(url);
}
}
};
registerBidder(spec);
function raiGetSizes(bid) {
let raiNewSizes;
if (bid.mediaTypes && bid.mediaTypes.banner && bid.mediaTypes.banner.sizes) {
raiNewSizes = bid.mediaTypes.banner.sizes
}
if (raiNewSizes != null) {
return raiNewSizes.map(size => ({
w: size[0],
h: size[1]
}));
}
}
function raiGetDemandType(bid) {
let raiFormat = 'display';
if (typeof bid.sizes != 'undefined') {
bid.sizes.forEach(function (sz) {
if ((sz[0] == '1800' && sz[1] == '1000') || (sz[0] == '1' && sz[1] == '1')) {
raiFormat = 'skin'
}
})
}
if (bid.mediaTypes != undefined) {
if (bid.mediaTypes.video != undefined) {
raiFormat = 'video';
}
}
return raiFormat;
}
function raiGetVideoInfo(bid) {
let videoData;
if (raiGetDemandType(bid) == 'video') {
videoData = {
format: bid.mediaTypes.video.context,
playerSize: bid.mediaTypes.video.playerSize,
mimes: bid.mediaTypes.video.mimes
};
} else {
videoData = {
format: 'banner'
}
}
return videoData;
}
function raiSetEids(bid) {
let eids = [];
if (bid && bid.userId) {
raiSetUserId(bid, eids, 'id5-sync.com', deepAccess(bid, `userId.id5id.uid`));
raiSetUserId(bid, eids, 'pubcommon', deepAccess(bid, `userId.pubcid`));
raiSetUserId(bid, eids, 'criteo.com', deepAccess(bid, `userId.criteoId`));
raiSetUserId(bid, eids, 'liveramp.com', deepAccess(bid, `userId.idl_env`));
raiSetUserId(bid, eids, 'liveintent.com', deepAccess(bid, `userId.lipb.lipbid`));
raiSetUserId(bid, eids, 'adserver.org', deepAccess(bid, `userId.tdid`));
}
return eids;
}
function raiSetUserId(bid, eids, source, value) {
if (isStr(value)) {
eids.push({
userId: value,
source: source
});
}
}
function renderer(bid) {
bid.renderer.push(() => {
renderAd(bid)
});
}
function renderAd(bid) {
let raOutstreamHBPassback = `${bid.vastXml}`;
let raPlayerHB = {
config: bid.params[0].player != undefined ? {
end: bid.params[0].player.end != null ? bid.params[0].player.end : 'close',
init: bid.params[0].player.init != null ? bid.params[0].player.init : 'close',
skin: bid.params[0].player.skin != null ? bid.params[0].player.skin : 'light',
} : {end: 'close', init: 'close', skin: 'light'},
pid: bid.params[0].pid,
adUnit: bid.adUnitCode
};
window.raParams(raPlayerHB, raOutstreamHBPassback, true);
}
function raiGetResolution() {
let resolution = '';
if (typeof window.screen != 'undefined') {
resolution = window.screen.width + 'x' + window.screen.height;
}
return resolution;
}
function raiSetPbAdSlot(bid) {
let pbAdSlot = '';
if (deepAccess(bid, 'ortb2Imp.ext.data.pbadslot') != null) {
pbAdSlot = deepAccess(bid, 'ortb2Imp.ext.data.pbadslot')
}
return pbAdSlot
}
function raiGetSyncInclude(config) {
try {
let raConfig = null;
let raiSync = {};
if (config.getConfig('userSync').filterSettings != null && typeof config.getConfig('userSync').filterSettings != 'undefined') {
raConfig = config.getConfig('userSync').filterSettings
if (raConfig.iframe != null && typeof raConfig.iframe != 'undefined') {
raiSync.raiIframe = raConfig.iframe.bidders == 'richaudience' || raConfig.iframe.bidders == '*' ? raConfig.iframe.filter : 'exclude';
}
if (raConfig.image != null && typeof raConfig.image != 'undefined') {
raiSync.raiImage = raConfig.image.bidders == 'richaudience' || raConfig.image.bidders == '*' ? raConfig.image.filter : 'exclude';
}
}
return raiSync;
} catch (e) {
return null;
}
}
function raiGetFloor(bid, config) {
try {
let raiFloor;
if (bid.params.bidfloor != null) {
raiFloor = bid.params.bidfloor;
} else if (typeof bid.getFloor == 'function') {
let floorSpec = bid.getFloor({
currency: config.getConfig('floors.data.currency') != null ? config.getConfig('floors.data.currency') : 'USD',
mediaType: typeof bid.mediaTypes['banner'] == 'object' ? 'banner' : 'video',
size: '*'
})
raiFloor = floorSpec.floor;
}
return raiFloor
} catch (e) {
return 0
}
}
function raiGetTimeoutURL(data) {
let {params, timeout} = data[0]
let url = 'https://s.richaudience.com/err/?ec=6&ev=[timeout_publisher]&pla=[placement_hash]&int=PREBID&pltfm=&node=&dm=[domain]';
url = url.replace('[timeout_publisher]', timeout)
url = url.replace('[placement_hash]', params[0].pid)
if (document.location.host != null) {
url = url.replace('[domain]', document.location.host)
}
return url
}
function setDSA(bid) {
let dsa = bid?.ortb2?.regs?.ext?.dsa ? bid?.ortb2?.regs?.ext?.dsa : null;
return dsa;
}