forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexverseBidAdapter.js
247 lines (226 loc) · 8.24 KB
/
nexverseBidAdapter.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
/* eslint-disable camelcase */
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO, NATIVE } from '../src/mediaTypes.js';
import { isArray } from '../src/utils.js';
import {getConnectionType} from '../libraries/connectionInfo/connectionUtils.js'
import { getDeviceType, getOS } from '../libraries/userAgentUtils/index.js';
import { getDeviceModel, buildEndpointUrl, isBidRequestValid, parseNativeResponse, printLog, getUid } from '../libraries/nexverseUtils/index.js';
import {getStorageManager} from '../src/storageManager.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';
import { getUserSyncs } from '../libraries/teqblazeUtils/bidderUtils.js';
import { getOsVersion } from '../libraries/advangUtils/index.js';
const BIDDER_CODE = 'nexverse';
const BIDDER_ENDPOINT = 'https://rtb.nexverse.ai/';
const SUPPORTED_MEDIA_TYPES = [BANNER, VIDEO, NATIVE];
const DEFAULT_CURRENCY = 'USD';
const BID_TTL = 300;
const DEFAULT_LANG = 'en';
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: SUPPORTED_MEDIA_TYPES,
isBidRequestValid,
/**
* Builds the OpenRTB server request from the list of valid bid requests.
*
* @param {Array} validBidRequests - Array of valid bid requests.
* @param {Object} bidderRequest - The bidder request object containing additional data.
* @returns {Array} Array of server requests to be sent to the endpoint.
*/
buildRequests(validBidRequests, bidderRequest) {
const requests = validBidRequests.map((bid) => {
// Build the endpoint URL with query parameters
const endpointUrl = buildEndpointUrl(BIDDER_ENDPOINT, bid);
// Build the OpenRTB payload
const payload = buildOpenRtbRequest(bid, bidderRequest);
if (!payload) {
printLog('error', 'Payload could not be built.');
return null; // Skip this bid
}
// Return the server request
return {
method: 'POST',
url: endpointUrl,
data: JSON.stringify(payload),
bidRequest: bid,
};
});
return requests.filter((request) => request !== null); // Remove null entries
},
/**
* Interprets the server's response and extracts bid information.
*
* @param {Object} serverResponse - The response from the server.
* @param {Object} request - The original server request.
* @returns {Array} Array of bids to be passed to the auction.
*/
interpretResponse(serverResponse, request) {
if (serverResponse && serverResponse.status === 204) {
printLog('info', 'No ad available (204 response).');
return [];
}
const bidResponses = [];
const response = serverResponse.body;
if (!response || !response.seatbid || !isArray(response.seatbid)) {
printLog('warning', 'No valid bids in the response.');
return bidResponses;
}
response.seatbid.forEach((seatbid) => {
seatbid.bid.forEach((bid) => {
const bidResponse = {
requestId: bid.impid,
cpm: bid.price,
currency: response.cur || DEFAULT_CURRENCY,
width: bid.width || 0,
height: bid.height || 0,
creativeId: bid.crid || bid.id,
ttl: BID_TTL,
netRevenue: true,
meta: {},
};
// Determine media type and assign the ad content
if (bid.ext && bid.ext.mediaType) {
bidResponse.mediaType = bid.ext.mediaType;
} else if (bid.adm && bid.adm.indexOf('<VAST') !== -1) {
bidResponse.mediaType = VIDEO;
bidResponse.vastXml = bid.adm;
} else if (bid.adm && bid.adm.indexOf('"native"') !== -1) {
bidResponse.mediaType = NATIVE;
bidResponse.native = parseNativeResponse(bid.adm);
} else {
bidResponse.mediaType = BANNER;
bidResponse.ad = bid.adm || '';
}
// Handle advertiser domains
if (bid.adomain && isArray(bid.adomain)) {
bidResponse.meta.advertiserDomains = bid.adomain;
} else {
bidResponse.meta.advertiserDomains = bid.bundle;
}
if (bid.attr && isArray(bid.attr)) {
bidResponse.meta.attr = bid.attr;
} else {
bidResponse.meta.attr = [];
}
bidResponse.meta.primaryCatId = bid.cat;
bidResponse.meta.secondaryCatIds = bid.cat.slice(1);
// Include 'nurl' if provided
if (bid.nurl) {
bidResponse.nurl = bid.nurl;
}
bidResponses.push(bidResponse);
});
});
return bidResponses;
},
getUserSyncs: getUserSyncs(BIDDER_ENDPOINT),
};
/**
* Builds the OpenRTB 2.5 request payload.
*
* @param {Object} bid - The bid request object.
* @param {Object} bidderRequest - The bidder request object.
* @returns {Object|null} The OpenRTB 2.5 request payload or null if missing mandatory parameters.
*/
function buildOpenRtbRequest(bid, bidderRequest) {
if (!bid || !bidderRequest) {
printLog('error', 'Missing required parameters for OpenRTB request.');
return null;
}
const imp = [];
// Handle different media types (Banner, Video, Native)
if (bid.mediaTypes.banner) {
imp.push({
id: bid.bidId,
banner: {
format: bid.sizes.map(size => ({ w: size[0], h: size[1] })), // List of size objects
w: bid.sizes[0][0],
h: bid.sizes[0][1],
},
secure: window.location.protocol === 'https:' ? 1 : 0, // Indicates whether the request is secure (HTTPS)
});
}
if (bid.mediaTypes.video) {
imp.push({
id: bid.bidId,
video: {
w: bid.sizes[0][0],
h: bid.sizes[0][1],
mimes: bid.mediaTypes.video.mimes || ['video/mp4'], // Default to video/mp4 if not specified
protocols: bid.mediaTypes.video.protocols || [2, 3, 5, 6], // RTB video ad serving protocols
maxduration: bid.mediaTypes.video.maxduration || 30,
linearity: bid.mediaTypes.video.linearity || 1,
playbackmethod: bid.mediaTypes.video.playbackmethod || [2],
},
secure: window.location.protocol === 'https:' ? 1 : 0, // Indicates whether the request is secure (HTTPS)
});
}
if (bid.mediaTypes.native) {
imp.push({
id: bid.bidId,
native: {
request: JSON.stringify(bid.mediaTypes.native), // Convert native request to JSON string
},
secure: window.location.protocol === 'https:' ? 1 : 0, // Indicates whether the request is secure (HTTPS)
});
}
// Construct the OpenRTB request object
const openRtbRequest = {
id: bidderRequest.auctionId,
imp: imp,
site: {
page: bidderRequest.refererInfo.page,
domain: bidderRequest.refererInfo.domain,
ref: bidderRequest.refererInfo.ref || '', // Referrer URL
},
device: {
ua: navigator.userAgent,
devicetype: getDeviceType(), // 1 = Mobile/Tablet, 2 = Desktop
os: getOS(),
osv: getOsVersion(),
make: navigator.vendor || '',
model: getDeviceModel(),
connectiontype: getConnectionType(), // Include connection type
geo: {
lat: bid.params.geoLat || 0,
lon: bid.params.geoLon || 0,
},
language: navigator.language || DEFAULT_LANG,
dnt: navigator.doNotTrack === '1' ? 1 : 0, // Do Not Track flag
},
user: {
id: getUid(storage),
buyeruid: bidderRequest.userId || '', // User ID or Buyer ID
ext: {
consent: bidderRequest.gdprConsent ? bidderRequest.gdprConsent.consentString : null, // GDPR consent string
},
},
regs: {
ext: {
gdpr: bidderRequest.gdprConsent ? (bidderRequest.gdprConsent.gdprApplies ? 1 : 0) : 0,
},
},
ext: {
prebid: {
auctiontimestamp: bidderRequest.auctionStart,
},
},
};
// Add app object if the request comes from a mobile app
if (bidderRequest.app) {
openRtbRequest.app = {
id: bidderRequest.app.id,
name: bidderRequest.app.name,
bundle: bidderRequest.app.bundle,
domain: bidderRequest.app.domain,
storeurl: bidderRequest.app.storeUrl,
cat: bidderRequest.app.cat || [],
};
}
// Add additional fields related to GDPR, US Privacy, CCPA
if (bidderRequest.uspConsent) {
openRtbRequest.regs.ext.us_privacy = bidderRequest.uspConsent;
}
return openRtbRequest;
}
registerBidder(spec);