forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridgeuppBidAdapter.js
285 lines (249 loc) · 8.5 KB
/
bridgeuppBidAdapter.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
import {deepSetValue, isFn, logWarn} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.js';
import {tryAppendQueryString} from '../libraries/urlUtils/urlUtils.js';
/**
* @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').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').TimedOutBid} TimedOutBid
*/
export const ADAPTER_VERSION = 1.0;
export const BIDDER_CODE = 'sonarads';
export const GVLID = 1300;
export const DEFAULT_CUR = 'USD';
export const SERVER_PATH_US1_BID = 'https://prebidjs-bids-us1.sonar-ads.com/analyze_request/bids';
export const SERVER_PATH_US1_EVENTS = 'https://prebidjs-events-us1.sonar-ads.com/events';
export const SERVER_PATH_US1_SYNC = 'https://prebidjs-sync-us1.sonar-ads.com/sync';
/**
* Bridgeupp : Report events for analytics and debuging.
*/
function reportEvents(eventType, eventData) {
if (!eventData || spec?.reportEventsEnabled !== true) {
return;
}
const payload = JSON.stringify({
domain: location.hostname,
prebidVersion: '$prebid.version$',
eventType: eventType,
eventPayload: eventData
});
fetch(`${SERVER_PATH_US1_EVENTS}`, {
body: payload,
keepalive: true,
credentials: 'include',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}).catch((_e) => {
// ignore errors for now
});
}
/**
* Bridgeupp : Defines the core oRTB converter inherited from converter library and all customization functions.
*/
const CONVERTER = ortbConverter({
context: {
netRevenue: true,
ttl: 300,
currency: DEFAULT_CUR
},
imp,
request,
bidResponse,
response
});
/**
* Bridgeupp : Builds an impression object for oRTB requests based on the bid request.
*
* @param {function} buildImp - Function to build the imp object.
* @param {Object} bidRequest - The request containing bid details.
* @param {Object} context - Context for the impression.
* @returns {Object} The constructed impression object.
*/
function imp(buildImp, bidRequest, context) {
let imp = buildImp(bidRequest, context);
const params = bidRequest.params;
imp.tagid = bidRequest.adUnitCode;
let floorInfo = {};
if (isFn(bidRequest.getFloor)) {
floorInfo = bidRequest.getFloor();
}
// if floor price module is not set reading from bidRequest.params or default
if (!imp.bidfloor) {
imp.bidfloor = bidRequest.params.bidfloor || 0.001;
imp.bidfloorcur = DEFAULT_CUR;
}
imp.secure = bidRequest.ortb2Imp?.secure ?? 1;
deepSetValue(imp, 'ext', {
...imp.ext,
params: bidRequest.params,
bidder: {
siteId: params?.siteId,
},
floorInfo: floorInfo
});
return imp;
}
/**
* Bridgeupp: Constructs the request object.
*
* @param {function} buildRequest - Function to build the request.
* @param {Array} imps - Array of impression objects.
* @param {Object} bidderRequest - Object containing bidder request information.
* @param {Object} context - Additional context.
* @returns {Object} The complete oRTB request object.
*/
function request(buildRequest, imps, bidderRequest, context) {
let request = buildRequest(imps, bidderRequest, context);
const siteId = context.bidRequests[0]?.params?.siteId;
deepSetValue(request, 'ext.prebid.channel', {
name: 'pbjs_bridgeupp',
pbjsversion: '$prebid.version$',
adapterversion: ADAPTER_VERSION,
siteId: siteId
});
return request;
}
function bidResponse(buildBidResponse, bid, context) {
return buildBidResponse(bid, context);
}
/**
* Bridgeupp bid response
*
* @param {function} buildResponse - Function to build the response.
* @param {Array} bidResponses - List of bid responses.
* @param {Object} ortbResponse - Original oRTB response data.
* @param {Object} context - Additional context.
* @returns {Object} Prebid.js compatible bid response.
*/
function response(buildResponse, bidResponses, ortbResponse, context) {
return buildResponse(bidResponses, ortbResponse, context);
}
export const spec = {
reportEventsEnabled: false,
code: BIDDER_CODE,
aliases: ['bridgeupp'],
gvlid: GVLID,
supportedMediaTypes: [BANNER],
/**
* Bridgeupp : Determines whether the given bid request is valid.
*
* @param {Object} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: (bid) => {
if (!bid || bid.bidder !== BIDDER_CODE || !bid.params.siteId) {
logWarn('Bridgeupp - bid is not valid, reach out to [email protected]');
return false;
}
return true;
},
/**
* Bridgeupp: Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} validBidRequests - an array of bids
* @param {Object} bidderRequest - Additional request details.
* @return {ServerRequest} Info describing the request to the server.
*/
buildRequests: (validBidRequests, bidderRequest) => {
const data = CONVERTER.toORTB({ bidderRequest: bidderRequest, bidRequests: validBidRequests, });
if (data) {
return {
method: 'POST',
url: SERVER_PATH_US1_BID,
data: data,
options: {
contentType: 'application/json',
crossOrigin: true,
withCredentials: true
}
};
}
},
/**
* Bridgeupp: Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param {ServerRequest} bidRequest - Original bid request.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, bidRequest) {
if (typeof serverResponse?.body == 'undefined') {
return [];
}
// reportEventsEnabled is returned from the server default false
spec.reportEventsEnabled = serverResponse.headers.get('reportEventsEnabled') > 0
const interpretedResponse = CONVERTER.fromORTB({ response: serverResponse.body, request: bidRequest.data });
return interpretedResponse.bids || [];
},
/**
* Bridgeupp : User sync options based on consent, support only iframe for now.
*/
getUserSyncs: (syncOptions, serverResponses, gdprConsent, uspConsent, gppConsent) => {
if (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled) {
logWarn('Bridgeupp - Bidder ConnectAd: No User-Matching allowed');
return [];
}
let pixelType = syncOptions.iframeEnabled ? 'iframe' : 'image';
let syncUrl = SERVER_PATH_US1_SYNC + '?';
syncUrl = gdprConsent ? tryAppendQueryString(syncUrl, 'gdpr', gdprConsent.gdprApplies ? 1 : 0) : syncUrl;
syncUrl = gdprConsent?.consentString ? tryAppendQueryString(syncUrl, 'gdpr_consent', gdprConsent.consentString) : syncUrl;
syncUrl = uspConsent ? tryAppendQueryString(syncUrl, 'us_privacy', uspConsent) : syncUrl;
if (gppConsent?.gppString && gppConsent?.applicableSections?.length) {
syncUrl = tryAppendQueryString(syncUrl, 'gpp', gppConsent.gppString);
syncUrl = tryAppendQueryString(syncUrl, 'gpp_sid', gppConsent.applicableSections.join(','));
}
if ((syncUrl.slice(-1) === '&') || (syncUrl.slice(-1) === '?')) {
syncUrl = syncUrl.slice(0, -1);
}
return [{
type: pixelType,
url: syncUrl
}];
},
/**
* Bridgeupp: Callback to report timeout event.
*
* @param {TimedOutBid[]} timeoutData - Array of timeout details.
*/
onTimeout: (timeoutData) => {
reportEvents('onTimeout', timeoutData);
},
/**
* Bridgeupp: Callback to report targeting event.
*
* @param {Bid} bid - The bid object
*/
onSetTargeting: (bid) => {
reportEvents('onSetTargeting', bid);
},
/**
* Bridgeupp: Callback to report successful ad render event.
*
* @param {Bid} bid - The bid that successfully rendered.
*/
onAdRenderSucceeded: (bid) => {
reportEvents('onAdRenderSucceeded', bid);
},
/**
* Bridgeupp: Callback to report bidder error event.
*
* @param {Object} errorData - Details about the error.
*/
onBidderError: (errorData) => {
reportEvents('onBidderError', errorData);
},
/**
* Bridgeupp: Callback to report bid won event.
*
* @param {Bid} bid - The bid that won the auction.
*/
onBidWon: (bid) => {
reportEvents('onBidWon', bid);
}
};
registerBidder(spec);