forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexx360BidAdapter.js
323 lines (297 loc) · 10.7 KB
/
nexx360BidAdapter.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
import { deepAccess, deepSetValue, generateUUID, logError, logInfo } from '../src/utils.js';
import {Renderer} from '../src/Renderer.js';
import {getStorageManager} from '../src/storageManager.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {getGlobal} from '../src/prebidGlobal.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.js'
import { INSTREAM, OUTSTREAM } from '../src/video.js';
import { getCurrencyFromBidderRequest } from '../libraries/ortb2Utils/currency.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').SyncOptions} SyncOptions
* @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync
* @typedef {import('../src/adapters/bidderFactory.js').validBidRequests} validBidRequests
*/
const OUTSTREAM_RENDERER_URL = 'https://acdn.adnxs.com/video/outstream/ANOutstreamVideo.js';
const BIDDER_CODE = 'nexx360';
const REQUEST_URL = 'https://fast.nexx360.io/booster';
const PAGE_VIEW_ID = generateUUID();
const BIDDER_VERSION = '4.2';
const GVLID = 965;
const NEXXID_KEY = 'nexx360_storage';
const ALIASES = [
{ code: 'revenuemaker' },
{ code: 'first-id', gvlid: 1178 },
{ code: 'adwebone' },
{ code: 'league-m', gvlid: 965 },
{ code: 'prjads' },
{ code: 'pubtech' },
{ code: '1accord', gvlid: 965 },
{ code: 'easybid', gvlid: 1068 },
];
export const storage = getStorageManager({
bidderCode: BIDDER_CODE,
});
/**
* Get the NexxId
* @param
* @return {object | false } false if localstorageNotEnabled
*/
export function getNexx360LocalStorage() {
if (!storage.localStorageIsEnabled()) {
logInfo(`localstorage not enabled for Nexx360`);
return false;
}
const output = storage.getDataFromLocalStorage(NEXXID_KEY);
if (output === null) {
const nexx360Storage = { nexx360Id: generateUUID() };
storage.setDataInLocalStorage(NEXXID_KEY, JSON.stringify(nexx360Storage));
return nexx360Storage;
}
try {
return JSON.parse(output)
} catch (e) {
return false;
}
}
function getAdContainer(container) {
if (document.getElementById(container)) {
return document.getElementById(container);
}
}
/**
* Get the AMX ID
* @return {string | false } false if localstorageNotEnabled
*/
export function getAmxId() {
if (!storage.localStorageIsEnabled()) {
logInfo(`localstorage not enabled for Nexx360`);
return false;
}
const amxId = storage.getDataFromLocalStorage('__amuidpb');
return amxId || false;
}
const converter = ortbConverter({
context: {
netRevenue: true, // or false if your adapter should set bidResponse.netRevenue = false
ttl: 90, // default bidResponse.ttl (when not specified in ORTB response.seatbid[].bid[].exp)
},
imp(buildImp, bidRequest, context) {
// console.log(bidRequest, context);
const imp = buildImp(bidRequest, context);
const tagid = bidRequest.params.adUnitName ? bidRequest.params.adUnitName : bidRequest.adUnitCode;
deepSetValue(imp, 'tagid', tagid);
deepSetValue(imp, 'ext.adUnitCode', bidRequest.adUnitCode);
const divId = bidRequest.params.divId ? bidRequest.params.divId : bidRequest.adUnitCode;
deepSetValue(imp, 'ext.divId', divId);
const slotEl = getAdContainer(divId);
if (slotEl) {
deepSetValue(imp, 'ext.dimensions.slotW', slotEl.offsetWidth);
deepSetValue(imp, 'ext.dimensions.slotH', slotEl.offsetHeight);
deepSetValue(imp, 'ext.dimensions.cssMaxW', slotEl.style?.maxWidth);
deepSetValue(imp, 'ext.dimensions.cssMaxH', slotEl.style?.maxHeight);
}
deepSetValue(imp, 'ext.nexx360', bidRequest.params.tagId);
deepSetValue(imp, 'ext.nexx360.tagId', bidRequest.params.tagId);
deepSetValue(imp, 'ext.nexx360.videoTagId', bidRequest.params.videoTagId);
deepSetValue(imp, 'ext.nexx360.allBids', bidRequest.params.allBids);
if (imp.video) {
const playerSize = deepAccess(bidRequest, 'mediaTypes.video.playerSize');
const videoContext = deepAccess(bidRequest, 'mediaTypes.video.context');
deepSetValue(imp, 'video.ext.playerSize', playerSize);
deepSetValue(imp, 'video.ext.context', videoContext);
}
if (bidRequest.params.adUnitName) deepSetValue(imp, 'ext.adUnitName', bidRequest.params.adUnitName);
if (bidRequest.params.adUnitPath) deepSetValue(imp, 'ext.adUnitPath', bidRequest.params.adUnitPath);
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const request = buildRequest(imps, bidderRequest, context);
const nexx360LocalStorage = getNexx360LocalStorage();
if (nexx360LocalStorage) {
deepSetValue(request, 'ext.nexx360Id', nexx360LocalStorage.nexx360Id);
deepSetValue(request, 'ext.localStorage.nexx360Id', nexx360LocalStorage.nexx360Id);
}
const amxId = getAmxId();
if (amxId) deepSetValue(request, 'ext.localStorage.amxId', amxId);
deepSetValue(request, 'ext.version', '$prebid.version$');
deepSetValue(request, 'ext.source', 'prebid.js');
deepSetValue(request, 'ext.pageViewId', PAGE_VIEW_ID);
deepSetValue(request, 'ext.bidderVersion', BIDDER_VERSION);
deepSetValue(request, 'cur', [getCurrencyFromBidderRequest(bidderRequest) || 'USD']);
if (!request.user) request.user = {};
if (getAmxId()) {
if (!request.user.ext) request.user.ext = {};
if (!request.user.ext.eids) request.user.ext.eids = [];
request.user.ext.eids.push({
source: 'amxdt.net',
uids: [{
id: `${getAmxId()}`,
atype: 1
}]
});
}
return request;
},
});
/**
* 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.
*/
function isBidRequestValid(bid) {
if (bid.params.adUnitName && (typeof bid.params.adUnitName !== 'string' || bid.params.adUnitName === '')) {
logError('bid.params.adUnitName needs to be a string');
return false;
}
if (bid.params.adUnitPath && (typeof bid.params.adUnitPath !== 'string' || bid.params.adUnitPath === '')) {
logError('bid.params.adUnitPath needs to be a string');
return false;
}
if (bid.params.divId && (typeof bid.params.divId !== 'string' || bid.params.divId === '')) {
logError('bid.params.divId needs to be a string');
return false;
}
if (bid.params.allBids && typeof bid.params.allBids !== 'boolean') {
logError('bid.params.allBids needs to be a boolean');
return false;
}
if (!bid.params.tagId && !bid.params.videoTagId && !bid.params.nativeTagId) {
logError('bid.params.tagId or bid.params.videoTagId or bid.params.nativeTagId must be defined');
return false;
}
return true;
};
/**
* Make a server request from the list of BidRequests.
*
* @return ServerRequest Info describing the request to the server.
*/
function buildRequests(bidRequests, bidderRequest) {
const data = converter.toORTB({bidRequests, bidderRequest})
return {
method: 'POST',
url: REQUEST_URL,
data,
}
}
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
function interpretResponse(serverResponse) {
const respBody = serverResponse.body;
if (!respBody || !Array.isArray(respBody.seatbid)) {
return [];
}
const { bidderSettings } = getGlobal();
const allowAlternateBidderCodes = bidderSettings && bidderSettings.standard ? bidderSettings.standard.allowAlternateBidderCodes : false;
let bids = [];
respBody.seatbid.forEach(seatbid => {
bids = [...bids, ...seatbid.bid.map(bid => {
const response = {
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
creativeId: bid.crid,
dealId: bid.dealid,
currency: respBody.cur,
netRevenue: true,
ttl: 120,
mediaType: [OUTSTREAM, INSTREAM].includes(bid.ext.mediaType) ? 'video' : bid.ext.mediaType,
meta: {
advertiserDomains: bid.adomain,
demandSource: bid.ext.ssp,
},
};
if (allowAlternateBidderCodes) response.bidderCode = `n360_${bid.ext.ssp}`;
if (bid.ext.mediaType === BANNER) {
if (bid.adm) {
response.ad = bid.adm;
} else {
response.adUrl = bid.ext.adUrl;
}
}
if ([INSTREAM, OUTSTREAM].includes(bid.ext.mediaType)) response.vastXml = bid.adm;
if (bid.ext.mediaType === OUTSTREAM) {
response.renderer = createRenderer(bid, OUTSTREAM_RENDERER_URL);
response.divId = bid.ext.divId
};
if (bid.ext.mediaType === NATIVE) {
try {
response.native = {
ortb: JSON.parse(bid.adm)
}
} catch (e) {}
}
return response;
})];
});
return bids;
}
/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
function getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
if (typeof serverResponses === 'object' &&
serverResponses != null &&
serverResponses.length > 0 &&
serverResponses[0].hasOwnProperty('body') &&
serverResponses[0].body.hasOwnProperty('ext') &&
serverResponses[0].body.ext.hasOwnProperty('cookies') &&
typeof serverResponses[0].body.ext.cookies === 'object') {
return serverResponses[0].body.ext.cookies.slice(0, 5);
} else {
return [];
}
};
function outstreamRender(response) {
response.renderer.push(() => {
window.ANOutstreamVideo.renderAd({
sizes: [response.width, response.height],
targetId: response.divId,
adResponse: response.vastXml,
rendererOptions: {
showBigPlayButton: false,
showProgressBar: 'bar',
showVolume: false,
allowFullscreen: true,
skippable: false,
content: response.vastXml
}
});
});
}
function createRenderer(bid, url) {
const renderer = Renderer.install({
id: bid.id,
url: url,
loaded: false,
adUnitCode: bid.ext.adUnitCode,
targetId: bid.ext.divId,
});
renderer.setRender(outstreamRender);
return renderer;
}
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
aliases: ALIASES,
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs,
};
registerBidder(spec);