forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pubgeniusBidAdapter.js
298 lines (246 loc) · 6.5 KB
/
pubgeniusBidAdapter.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
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {ajax} from '../src/ajax.js';
import {config} from '../src/config.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {
deepAccess,
deepSetValue,
inIframe,
isArrayOfNums,
isFn,
isInteger,
isStr,
logError,
parseQueryStringParameters,
pick,
} from '../src/utils.js';
const BIDDER_VERSION = '1.1.0';
const BASE_URL = 'https://auction.adpearl.io';
export const spec = {
code: 'pubgenius',
supportedMediaTypes: [ BANNER, VIDEO ],
isBidRequestValid(bid) {
const adUnitId = bid.params.adUnitId;
if (!isStr(adUnitId) && !isInteger(adUnitId)) {
logError('pubgenius bidder params: adUnitId must be a string or integer.');
return false;
}
const { mediaTypes } = bid;
if (mediaTypes.banner) {
return isValidBanner(mediaTypes.banner);
}
return isValidVideo(mediaTypes.video, bid.params.video);
},
buildRequests: function (bidRequests, bidderRequest) {
const data = {
id: bidderRequest.bidderRequestId,
imp: bidRequests.map(buildImp),
tmax: bidderRequest.timeout,
ext: {
pbadapter: {
version: BIDDER_VERSION,
},
},
};
const site = buildSite(bidderRequest);
if (site) {
data.site = site;
}
const gdpr = bidderRequest.gdprConsent;
if (gdpr) {
const applies = gdpr.gdprApplies;
const consent = gdpr.consentString;
deepSetValue(data, 'regs.ext.gdpr', numericBoolean(applies));
if (applies && consent) {
deepSetValue(data, 'user.ext.consent', consent);
}
}
const usp = bidderRequest.uspConsent;
if (usp) {
deepSetValue(data, 'regs.ext.us_privacy', usp);
}
const schain = bidRequests[0].schain;
if (schain) {
deepSetValue(data, 'source.ext.schain', schain);
}
if (config.getConfig('coppa')) {
deepSetValue(data, 'regs.coppa', 1);
}
const bidUserIdAsEids = deepAccess(bidRequests, '0.userIdAsEids');
if (bidUserIdAsEids && bidUserIdAsEids.length) {
const eids = bidUserIdAsEids.filter(eid => eid.source === 'adserver.org');
if (eids.length) {
deepSetValue(data, 'user.ext.eids', eids);
}
}
return {
method: 'POST',
url: `${getBaseUrl()}/prebid/auction`,
data,
};
},
interpretResponse({ body }) {
const bidResponses = [];
const currency = body.cur || 'USD';
const seatbids = body.seatbid;
if (seatbids) {
seatbids.forEach(seatbid => {
seatbid.bid.forEach(bid => {
const bidResponse = interpretBid(bid);
bidResponse.currency = currency;
bidResponses.push(bidResponse);
});
});
}
return bidResponses;
},
getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
const syncs = []
if (syncOptions.iframeEnabled) {
let params = {};
if (gdprConsent) {
params.gdpr = numericBoolean(gdprConsent.gdprApplies);
if (gdprConsent.consentString) {
params.consent = gdprConsent.consentString;
}
}
if (uspConsent) {
params.us_privacy = uspConsent;
}
const qs = parseQueryStringParameters(params);
syncs.push({
type: 'iframe',
url: `${getBaseUrl()}/usersync/pixels.html?${qs}`,
});
}
return syncs;
},
onTimeout(data) {
ajax(`${getBaseUrl()}/prebid/events?type=timeout`, null, JSON.stringify(data), {
method: 'POST',
});
},
};
function buildVideoParams(videoMediaType, videoParams) {
videoMediaType = videoMediaType || {};
const params = pick(videoMediaType, [
'mimes',
'minduration',
'maxduration',
'protocols',
'startdelay',
'plcmt',
'skip',
'skipafter',
'minbitrate',
'maxbitrate',
'delivery',
'playbackmethod',
'api',
'linearity',
]);
if (videoMediaType.playerSize) {
params.w = videoMediaType.playerSize[0][0];
params.h = videoMediaType.playerSize[0][1];
}
return Object.assign(params, videoParams);
}
function buildImp(bid) {
const imp = {
id: bid.bidId,
tagid: String(bid.params.adUnitId),
};
if (bid.mediaTypes.banner) {
imp.banner = {
format: bid.mediaTypes.banner.sizes.map(size => ({ w: size[0], h: size[1] })),
topframe: numericBoolean(!inIframe()),
};
} else {
imp.video = buildVideoParams(bid.mediaTypes.video, bid.params.video);
}
if (isFn(bid.getFloor)) {
const { floor } = bid.getFloor({
mediaType: bid.mediaTypes.banner ? 'banner' : 'video',
size: '*',
currency: 'USD',
});
if (floor) {
imp.bidfloor = floor;
}
}
const pos = bid.params.position;
if (isInteger(pos)) {
imp.banner.pos = pos;
}
if (bid.params.test) {
deepSetValue(imp, 'ext.test', 1);
}
return imp;
}
function buildSite(bidderRequest) {
let site = null;
const { refererInfo } = bidderRequest;
const pageUrl = refererInfo.page;
if (pageUrl) {
site = site || {};
site.page = pageUrl;
}
if (refererInfo.ref) {
site = site || {};
site.ref = refererInfo.ref;
}
return site;
}
function interpretBid(bid) {
const bidResponse = {
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
ttl: bid.exp,
creativeId: bid.crid,
netRevenue: true,
};
if (bid.adomain && bid.adomain.length) {
bidResponse.meta = {
advertiserDomains: bid.adomain,
};
}
const pbadapter = deepAccess(bid, 'ext.pbadapter') || {};
switch (pbadapter.mediaType) {
case 'video':
if (bid.nurl) {
bidResponse.vastUrl = bid.nurl;
}
if (bid.adm) {
bidResponse.vastXml = bid.adm;
}
bidResponse.mediaType = VIDEO;
break;
default: // banner by default
bidResponse.ad = bid.adm;
break;
}
return bidResponse;
}
function numericBoolean(value) {
return value ? 1 : 0;
}
function getBaseUrl() {
const pubg = config.getConfig('pubgenius');
return (pubg && pubg.endpoint) || BASE_URL;
}
function isValidSize(size) {
return isArrayOfNums(size, 2) && size[0] > 0 && size[1] > 0;
}
function isValidBanner(banner) {
const sizes = banner.sizes;
return !!(sizes && sizes.length) && sizes.every(isValidSize);
}
function isValidVideo(videoMediaType, videoParams) {
const params = buildVideoParams(videoMediaType, videoParams);
return !!(isValidSize([params.w, params.h]) &&
params.mimes && params.mimes.length &&
isArrayOfNums(params.protocols) && params.protocols.length);
}
registerBidder(spec);