-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtripleliftBidAdapter.js
253 lines (215 loc) · 6.83 KB
/
tripleliftBidAdapter.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
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import * as utils from '../src/utils.js';
import { config } from '../src/config.js';
const BIDDER_CODE = 'triplelift';
const STR_ENDPOINT = 'https://tlx.3lift.com/header/auction?';
let gdprApplies = true;
let consentString = null;
export const tripleliftAdapterSpec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid: function (bid) {
if (bid.mediaTypes.video) {
let video = _getORTBVideo(bid);
if (!video.w || !video.h) return false;
}
return typeof bid.params.inventoryCode !== 'undefined';
},
buildRequests: function(bidRequests, bidderRequest) {
let tlCall = STR_ENDPOINT;
let data = _buildPostBody(bidRequests);
tlCall = utils.tryAppendQueryString(tlCall, 'lib', 'prebid');
tlCall = utils.tryAppendQueryString(tlCall, 'v', '$prebid.version$');
if (bidderRequest && bidderRequest.refererInfo) {
let referrer = bidderRequest.refererInfo.referer;
tlCall = utils.tryAppendQueryString(tlCall, 'referrer', referrer);
}
if (bidderRequest && bidderRequest.timeout) {
tlCall = utils.tryAppendQueryString(tlCall, 'tmax', bidderRequest.timeout);
}
if (bidderRequest && bidderRequest.gdprConsent) {
if (typeof bidderRequest.gdprConsent.gdprApplies !== 'undefined') {
gdprApplies = bidderRequest.gdprConsent.gdprApplies;
tlCall = utils.tryAppendQueryString(tlCall, 'gdpr', gdprApplies.toString());
}
if (typeof bidderRequest.gdprConsent.consentString !== 'undefined') {
consentString = bidderRequest.gdprConsent.consentString;
tlCall = utils.tryAppendQueryString(tlCall, 'cmp_cs', consentString);
}
}
if (bidderRequest && bidderRequest.uspConsent) {
tlCall = utils.tryAppendQueryString(tlCall, 'us_privacy', bidderRequest.uspConsent);
}
if (config.getConfig('coppa') === true) {
tlCall = utils.tryAppendQueryString(tlCall, 'coppa', true);
}
if (tlCall.lastIndexOf('&') === tlCall.length - 1) {
tlCall = tlCall.substring(0, tlCall.length - 1);
}
utils.logMessage('tlCall request built: ' + tlCall);
return {
method: 'POST',
url: tlCall,
data,
bidderRequest
};
},
interpretResponse: function(serverResponse, {bidderRequest}) {
let bids = serverResponse.body.bids || [];
return bids.map(function(bid) {
return _buildResponseObject(bidderRequest, bid);
});
},
getUserSyncs: function(syncOptions, responses, gdprConsent, usPrivacy) {
let syncType = _getSyncType(syncOptions);
if (!syncType) return;
let syncEndpoint = 'https://eb2.3lift.com/sync?';
if (syncType === 'image') {
syncEndpoint = utils.tryAppendQueryString(syncEndpoint, 'px', 1);
syncEndpoint = utils.tryAppendQueryString(syncEndpoint, 'src', 'prebid');
}
if (consentString !== null) {
syncEndpoint = utils.tryAppendQueryString(syncEndpoint, 'gdpr', gdprApplies);
syncEndpoint = utils.tryAppendQueryString(syncEndpoint, 'cmp_cs', consentString);
}
if (usPrivacy) {
syncEndpoint = utils.tryAppendQueryString(syncEndpoint, 'us_privacy', usPrivacy);
}
return [{
type: syncType,
url: syncEndpoint
}];
}
}
function _getSyncType(syncOptions) {
if (!syncOptions) return;
if (syncOptions.iframeEnabled) return 'iframe';
if (syncOptions.pixelEnabled) return 'image';
}
function _buildPostBody(bidRequests) {
let data = {};
let { schain } = bidRequests[0];
data.imp = bidRequests.map(function(bidRequest, index) {
let imp = {
id: index,
tagid: bidRequest.params.inventoryCode,
floor: _getFloor(bidRequest)
};
if (bidRequest.mediaTypes.video) {
imp.video = _getORTBVideo(bidRequest);
} else if (bidRequest.mediaTypes.banner) {
imp.banner = { format: _sizes(bidRequest.sizes) };
};
return imp;
});
let eids = [
...getUnifiedIdEids(bidRequests),
...getIdentityLinkEids(bidRequests),
...getCriteoEids(bidRequests)
];
if (eids.length > 0) {
data.user = {
ext: {eids}
};
}
if (schain) {
data.ext = {
schain
}
}
return data;
}
function _getORTBVideo(bidRequest) {
// give precedent to mediaTypes.video
let video = { ...bidRequest.params.video, ...bidRequest.mediaTypes.video };
if (!video.w) video.w = video.playerSize[0][0];
if (!video.h) video.h = video.playerSize[0][1];
if (video.context === 'instream') video.placement = 1;
// clean up oRTB object
delete video.playerSize;
return video;
}
function _getFloor (bid) {
let floor = null;
if (typeof bid.getFloor === 'function') {
const floorInfo = bid.getFloor({
currency: 'USD',
mediaType: 'banner',
size: _sizes(bid.sizes)
});
if (typeof floorInfo === 'object' &&
floorInfo.currency === 'USD' && !isNaN(parseFloat(floorInfo.floor))) {
floor = parseFloat(floorInfo.floor);
}
}
return floor !== null ? floor : bid.params.floor;
}
function getUnifiedIdEids(bidRequests) {
return getEids(bidRequests, 'tdid', 'adserver.org', 'TDID');
}
function getIdentityLinkEids(bidRequests) {
return getEids(bidRequests, 'idl_env', 'liveramp.com', 'idl');
}
function getCriteoEids(bidRequests) {
return getEids(bidRequests, 'criteoId', 'criteo.com', 'criteoId');
}
function getEids(bidRequests, type, source, rtiPartner) {
return bidRequests
.map(getUserId(type)) // bids -> userIds of a certain type
.filter((x) => !!x) // filter out null userIds
.map(formatEid(source, rtiPartner)); // userIds -> eid objects
}
function getUserId(type) {
return (bid) => (bid && bid.userId && bid.userId[type]);
}
function formatEid(source, rtiPartner) {
return (id) => ({
source,
uids: [{
id,
ext: { rtiPartner }
}]
});
}
function _sizes(sizeArray) {
let sizes = sizeArray.filter(_isValidSize);
return sizes.map(function(size) {
return {
w: size[0],
h: size[1]
};
});
}
function _isValidSize(size) {
return (size.length === 2 && typeof size[0] === 'number' && typeof size[1] === 'number');
}
function _buildResponseObject(bidderRequest, bid) {
let bidResponse = {};
let width = bid.width || 1;
let height = bid.height || 1;
let dealId = bid.deal_id || '';
let creativeId = bid.crid || '';
let breq = bidderRequest.bids[bid.imp_id];
if (bid.cpm != 0 && bid.ad) {
bidResponse = {
requestId: breq.bidId,
cpm: bid.cpm,
width: width,
height: height,
netRevenue: true,
ad: bid.ad,
creativeId: creativeId,
dealId: dealId,
currency: 'USD',
ttl: 300,
tl_source: bid.tl_source
};
if (breq.mediaTypes.video) {
bidResponse.vastXml = bid.ad;
bidResponse.mediaType = 'video';
};
};
return bidResponse;
}
registerBidder(tripleliftAdapterSpec);