-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
driftpixelBidAdapter.js
222 lines (197 loc) · 6.57 KB
/
driftpixelBidAdapter.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
import {config} from '../src/config.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {parseSizesInput, isFn, deepAccess, getBidIdParameter, logError, isArray} from '../src/utils.js';
import {getAdUnitSizes} from '../libraries/sizeUtils/sizeUtils.js';
import {findIndex} from '../src/polyfill.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
*/
const CUR = 'USD';
const BIDDER_CODE = 'driftpixel';
const ENDPOINT = 'https://pbjs.driftpixel.live';
/**
* 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 && typeof bid.params !== 'object') {
logError('Params is not defined or is incorrect in the bidder settings');
return false;
}
if (!getBidIdParameter('env', bid.params) || !getBidIdParameter('pid', bid.params)) {
logError('Env or pid is not present in bidder params');
return false;
}
if (deepAccess(bid, 'mediaTypes.video') && !isArray(deepAccess(bid, 'mediaTypes.video.playerSize'))) {
logError('mediaTypes.video.playerSize is required for video');
return false;
}
return true;
}
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequest?pbjs_debug=trues[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
function buildRequests(validBidRequests, bidderRequest) {
const {refererInfo = {}, gdprConsent = {}, uspConsent} = bidderRequest;
const requests = validBidRequests.map(req => {
const request = {};
request.bidId = req.bidId;
request.banner = deepAccess(req, 'mediaTypes.banner');
request.auctionId = req.ortb2?.source?.tid;
request.transactionId = req.ortb2Imp?.ext?.tid;
request.sizes = parseSizesInput(getAdUnitSizes(req));
request.schain = req.schain;
request.location = {
page: refererInfo.page,
location: refererInfo.location,
domain: refererInfo.domain,
whost: window.location.host,
ref: refererInfo.ref,
isAmp: refererInfo.isAmp
};
request.device = {
ua: navigator.userAgent,
lang: navigator.language
};
request.env = {
env: req.params.env,
pid: req.params.pid
};
request.ortb2 = req.ortb2;
request.ortb2Imp = req.ortb2Imp;
request.tz = new Date().getTimezoneOffset();
request.ext = req.params.ext;
request.bc = req.bidRequestsCount;
request.floor = getBidFloor(req);
if (req.userIdAsEids && req.userIdAsEids.length !== 0) {
request.userEids = req.userIdAsEids;
} else {
request.userEids = [];
}
if (gdprConsent.gdprApplies) {
request.gdprApplies = Number(gdprConsent.gdprApplies);
request.consentString = gdprConsent.consentString;
} else {
request.gdprApplies = 0;
request.consentString = '';
}
if (uspConsent) {
request.usPrivacy = uspConsent;
} else {
request.usPrivacy = '';
}
if (config.getConfig('coppa')) {
request.coppa = 1;
} else {
request.coppa = 0;
}
const video = deepAccess(req, 'mediaTypes.video');
if (video) {
request.sizes = parseSizesInput(deepAccess(req, 'mediaTypes.video.playerSize'));
request.video = video;
}
return request;
});
return {
method: 'POST',
url: ENDPOINT + '/bid',
data: JSON.stringify(requests),
withCredentials: true,
bidderRequest,
options: {
contentType: 'application/json',
}
};
}
/**
* 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, {bidderRequest}) {
const response = [];
if (!isArray(deepAccess(serverResponse, 'body.data'))) {
return response;
}
serverResponse.body.data.forEach(serverBid => {
const bidIndex = findIndex(bidderRequest.bids, (bidRequest) => {
return bidRequest.bidId === serverBid.requestId;
});
if (bidIndex !== -1) {
const bid = {
requestId: serverBid.requestId,
dealId: bidderRequest.dealId || null,
...serverBid
};
response.push(bid);
}
});
return response;
}
/**
* 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 = '') {
const syncs = [];
const pixels = deepAccess(serverResponses, '0.body.data.0.ext.pixels');
if ((syncOptions.iframeEnabled || syncOptions.pixelEnabled) && isArray(pixels) && pixels.length !== 0) {
const gdprFlag = `&gdpr=${gdprConsent.gdprApplies ? 1 : 0}`;
const gdprString = `&gdpr_consent=${encodeURIComponent((gdprConsent.consentString || ''))}`;
const usPrivacy = `us_privacy=${encodeURIComponent(uspConsent)}`;
pixels.forEach(pixel => {
const [type, url] = pixel;
const sync = {type, url: `${url}&${usPrivacy}${gdprFlag}${gdprString}`};
if (type === 'iframe' && syncOptions.iframeEnabled) {
syncs.push(sync)
} else if (type === 'image' && syncOptions.pixelEnabled) {
syncs.push(sync)
}
});
}
return syncs;
}
/**
* Get valid floor value from getFloor fuction.
*
* @param {Object} bid Current bid request.
* @return {null|Number} Returns floor value when bid.getFloor is function and returns valid floor object with USD currency, otherwise returns null.
*/
export function getBidFloor(bid) {
if (!isFn(bid.getFloor)) {
return null;
}
let floor = bid.getFloor({
currency: CUR,
mediaType: '*',
size: '*'
});
if (typeof floor === 'object' && !isNaN(floor.floor) && floor.currency === CUR) {
return floor.floor;
}
return null;
}
export const spec = {
code: BIDDER_CODE,
aliases: ['driftpixel'],
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs
}
registerBidder(spec);