forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connatixBidAdapter.js
346 lines (292 loc) · 10.3 KB
/
connatixBidAdapter.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
import {
registerBidder
} from '../src/adapters/bidderFactory.js';
import {
deepAccess,
isFn,
logError,
isArray,
formatQS,
getWindowSelf,
getWindowTop,
isNumber,
isStr
} from '../src/utils.js';
import {
ADPOD,
BANNER,
VIDEO,
} from '../src/mediaTypes.js';
const BIDDER_CODE = 'connatix';
const AD_URL = 'https://capi.connatix.com/rtb/hba';
const DEFAULT_MAX_TTL = '3600';
const DEFAULT_CURRENCY = 'USD';
/*
* Get the bid floor value from the bid object, either using the getFloor function or by accessing the 'params.bidfloor' property.
* If the bid floor cannot be determined, return 0 as a fallback value.
*/
export function getBidFloor(bid) {
if (!isFn(bid.getFloor)) {
return deepAccess(bid, 'params.bidfloor', 0);
}
try {
const bidFloor = bid.getFloor({
currency: DEFAULT_CURRENCY,
mediaType: '*',
size: '*',
});
return bidFloor.floor;
} catch (err) {
logError(err);
return 0;
}
}
export function validateBanner(mediaTypes) {
if (!mediaTypes[BANNER]) {
return true;
}
const banner = deepAccess(mediaTypes, BANNER, {});
return (Boolean(banner.sizes) && isArray(mediaTypes[BANNER].sizes) && mediaTypes[BANNER].sizes.length > 0);
}
export function validateVideo(mediaTypes) {
const video = mediaTypes[VIDEO];
if (!video) {
return true;
}
return video.context !== ADPOD;
}
export function _getMinSize(sizes) {
if (!sizes || sizes.length === 0) return undefined;
return sizes.reduce((minSize, currentSize) => {
const minArea = minSize.w * minSize.h;
const currentArea = currentSize.w * currentSize.h;
return currentArea < minArea ? currentSize : minSize;
});
}
export function _isViewabilityMeasurable(element) {
if (!element) return false;
return !_isIframe(element);
}
export function _isIframe() {
try {
return getWindowSelf() !== getWindowTop();
} catch (e) {
return true;
}
}
export function _getViewability(element, topWin, { w, h } = {}) {
return topWin.document.visibilityState === 'visible'
? _getPercentInView(element, topWin, { w, h })
: 0;
}
export function _getPercentInView(element, topWin, { w, h } = {}) {
const elementBoundingBox = _getBoundingBox(element, { w, h });
const elementInViewBoundingBox = _getIntersectionOfRects([ {
left: 0,
top: 0,
right: topWin.innerWidth,
bottom: topWin.innerHeight
}, elementBoundingBox ]);
let elementInViewArea, elementTotalArea;
if (elementInViewBoundingBox !== null) {
elementInViewArea = elementInViewBoundingBox.width * elementInViewBoundingBox.height;
elementTotalArea = elementBoundingBox.width * elementBoundingBox.height;
return ((elementInViewArea / elementTotalArea) * 100);
}
return 0;
}
export function _getBoundingBox(element, { w, h } = {}) {
let { width, height, left, top, right, bottom } = element.getBoundingClientRect();
if ((width === 0 || height === 0) && w && h) {
width = w;
height = h;
right = left + w;
bottom = top + h;
}
return { width, height, left, top, right, bottom };
}
export function _getIntersectionOfRects(rects) {
const bbox = {
left: rects[0].left,
right: rects[0].right,
top: rects[0].top,
bottom: rects[0].bottom
};
for (let i = 1; i < rects.length; ++i) {
bbox.left = Math.max(bbox.left, rects[i].left);
bbox.right = Math.min(bbox.right, rects[i].right);
if (bbox.left >= bbox.right) {
return null;
}
bbox.top = Math.max(bbox.top, rects[i].top);
bbox.bottom = Math.min(bbox.bottom, rects[i].bottom);
if (bbox.top >= bbox.bottom) {
return null;
}
}
bbox.width = bbox.right - bbox.left;
bbox.height = bbox.bottom - bbox.top;
return bbox;
}
export const spec = {
code: BIDDER_CODE,
gvlid: 143,
supportedMediaTypes: [BANNER, VIDEO],
/*
* Validate the bid request.
* If the request is valid, Connatix is trying to obtain at least one bid.
* Otherwise, the request to the Connatix server is not made
*/
isBidRequestValid: (bid = {}) => {
const bidId = deepAccess(bid, 'bidId');
const mediaTypes = deepAccess(bid, 'mediaTypes', {});
const params = deepAccess(bid, 'params', {});
const bidder = deepAccess(bid, 'bidder');
const hasBidId = Boolean(bidId);
const isValidBidder = (bidder === BIDDER_CODE);
const hasMediaTypes = Boolean(mediaTypes) && (Boolean(mediaTypes[BANNER]) || Boolean(mediaTypes[VIDEO]));
const isValidBanner = validateBanner(mediaTypes);
const isValidVideo = validateVideo(mediaTypes);
const isValidViewability = typeof params.viewabilityPercentage === 'undefined' || (isNumber(params.viewabilityPercentage) && params.viewabilityPercentage >= 0 && params.viewabilityPercentage <= 1);
const hasRequiredBidParams = Boolean(params.placementId);
const isValid = isValidBidder && hasBidId && hasMediaTypes && isValidViewability && isValidBanner && isValidVideo && hasRequiredBidParams;
if (!isValid) {
logError(
`Invalid bid request:
isValidBidder: ${isValidBidder},
hasBidId: ${hasBidId},
hasMediaTypes: ${hasMediaTypes},
isValidBanner: ${isValidBanner},
isValidVideo: ${isValidVideo},
hasRequiredBidParams: ${hasRequiredBidParams}`
);
}
return isValid;
},
/*
* Build the request payload by processing valid bid requests and extracting the necessary information.
* Determine the host and page from the bidderRequest's refferUrl, and include ccpa and gdpr consents.
* Return an object containing the request method, url, and the constructed payload.
*/
buildRequests: (validBidRequests = [], bidderRequest = {}) => {
const bidRequests = validBidRequests.map(bid => {
const {
bidId,
mediaTypes,
params,
sizes,
adUnitCode
} = bid;
let detectedViewabilityPercentage = null;
const viewabilityContainerIdentifier = params.viewabilityContainerIdentifier;
let element = document.getElementById(adUnitCode);
// Get the sizes from the mediaTypes object if it exists, otherwise use the sizes array from the bid object
// ??? Should we get also from w, h or make them required in our docs to improve the viewailbity detection support ???
let bidParamSizes = bid.mediaTypes && bid.mediaTypes.banner && bid.mediaTypes.banner.sizes ? bid.mediaTypes.banner.sizes : bid.sizes;
bidParamSizes = typeof bidParamSizes === 'undefined' && bid.mediaType && bid.mediaType.video && bid.mediaType.video.playerSize ? bid.mediaType.video.playerSize : bidParamSizes;
bidParamSizes = typeof bidParamSizes === 'undefined' && bid.mediaType && bid.mediaType.video && isNumber(bid.mediaType.video.w) && isNumber(bid.mediaType.h) ? [bid.mediaType.video.w, bid.mediaType.video.h] : bidParamSizes;
if (isStr(viewabilityContainerIdentifier)) {
try {
element = document.querySelector(viewabilityContainerIdentifier) || window.top.document.querySelector(viewabilityContainerIdentifier);
if (element) {
bidParamSizes = [element.offsetWidth, element.offsetHeight];
}
} catch (e) {
logError(`Error while trying to find viewability container element: ${viewabilityContainerIdentifier}`);
}
}
let minSize = _getMinSize(bidParamSizes ?? [])
if (_isViewabilityMeasurable(element)) {
const minSizeObj = {
w: minSize[0],
h: minSize[1]
}
detectedViewabilityPercentage = Math.round(_getViewability(element, getWindowTop(), minSizeObj))
}
return {
bidId,
mediaTypes,
sizes,
detectedViewabilityPercentage,
declaredViewabilityPercentage: bid.params.viewabilityPercentage ?? null,
placementId: params.placementId,
floor: getBidFloor(bid),
};
});
const requestPayload = {
ortb2: bidderRequest.ortb2,
gdprConsent: bidderRequest.gdprConsent,
uspConsent: bidderRequest.uspConsent,
gppConsent: bidderRequest.gppConsent,
refererInfo: bidderRequest.refererInfo,
bidRequests,
};
return {
method: 'POST',
url: AD_URL,
data: requestPayload
};
},
/*
* Interpret the server response and create an array of bid responses by extracting and formatting
* relevant information such as requestId, cpm, ttl, width, height, creativeId, referrer and ad
* Returns an array of bid responses by extracting and formatting the server response
*/
interpretResponse: (serverResponse) => {
const responseBody = serverResponse.body;
const bids = responseBody.Bids;
if (!isArray(bids)) {
return [];
}
const referrer = responseBody.Referrer;
return bids.map(bidResponse => ({
requestId: bidResponse.RequestId,
cpm: bidResponse.Cpm,
ttl: bidResponse.Ttl || DEFAULT_MAX_TTL,
currency: 'USD',
mediaType: bidResponse.VastXml ? VIDEO : BANNER,
netRevenue: true,
width: bidResponse.Width,
height: bidResponse.Height,
creativeId: bidResponse.CreativeId,
ad: bidResponse.Ad,
vastXml: bidResponse.VastXml,
referrer: referrer,
}));
},
/*
* Determine the user sync type (either 'iframe' or 'image') based on syncOptions.
* Construct the sync URL by appending required query parameters such as gdpr, ccpa, and coppa consents.
* Return an array containing an object with the sync type and the constructed URL.
*/
getUserSyncs: (syncOptions, serverResponses, gdprConsent, uspConsent, gppConsent) => {
if (!syncOptions.iframeEnabled) {
return [];
}
if (!serverResponses || !serverResponses.length) {
return [];
}
const params = {};
if (gdprConsent) {
if (typeof gdprConsent.gdprApplies === 'boolean') {
params['gdpr'] = Number(gdprConsent.gdprApplies);
} else {
params['gdpr'] = 0;
}
if (typeof gdprConsent.consentString === 'string') {
params['gdpr_consent'] = encodeURIComponent(gdprConsent.consentString);
}
}
if (typeof uspConsent === 'string') {
params['us_privacy'] = encodeURIComponent(uspConsent);
}
const syncUrl = serverResponses[0].body.UserSyncEndpoint;
const queryParams = Object.keys(params).length > 0 ? formatQS(params) : '';
const url = queryParams ? `${syncUrl}?${queryParams}` : syncUrl;
return [{
type: 'iframe',
url
}];
}
};
registerBidder(spec);