forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalkimiBidAdapter.js
218 lines (186 loc) · 6.25 KB
/
alkimiBidAdapter.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
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {deepAccess, deepClone, getDNT, generateUUID, replaceAuctionPrice} from '../src/utils.js';
import {ajax} from '../src/ajax.js';
import {getStorageManager} from '../src/storageManager.js';
import {VIDEO, BANNER} from '../src/mediaTypes.js';
import {config} from '../src/config.js';
const BIDDER_CODE = 'alkimi';
const GVLID = 1169;
const USER_ID_KEY = 'alkimiUserID';
export const ENDPOINT = 'https://exchange.alkimi-onboarding.com/bid?prebid=true';
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: ['banner', 'video'],
isBidRequestValid: function (bid) {
return !!(bid.params && bid.params.token);
},
buildRequests: function (validBidRequests, bidderRequest) {
let bids = [];
let bidIds = [];
let eids;
validBidRequests.forEach(bidRequest => {
let formatTypes = getFormatType(bidRequest)
if (bidRequest.userIdAsEids) {
eids = eids || bidRequest.userIdAsEids
}
bids.push({
token: bidRequest.params.token,
instl: bidRequest.params.instl,
exp: bidRequest.params.exp,
bidFloor: getBidFloor(bidRequest, formatTypes),
sizes: prepareSizes(deepAccess(bidRequest, 'mediaTypes.banner.sizes')),
playerSizes: prepareSizes(deepAccess(bidRequest, 'mediaTypes.video.playerSize')),
impMediaTypes: formatTypes,
adUnitCode: bidRequest.adUnitCode,
video: deepAccess(bidRequest, 'mediaTypes.video'),
banner: deepAccess(bidRequest, 'mediaTypes.banner')
})
bidIds.push(bidRequest.bidId)
})
const ortb2 = bidderRequest.ortb2
const site = ortb2?.site
const id = getUserId()
const alkimiConfig = config.getConfig('alkimi')
const fpa = ortb2?.source?.ext?.fpa
const source = fpa != undefined ? { ext: { fpa } } : undefined
const walletID = alkimiConfig && alkimiConfig.walletID
const userParams = alkimiConfig && alkimiConfig.userParams
const user = (walletID != undefined || userParams != undefined || id != undefined) ? { id, ext: { walletID, userParams } } : undefined
let payload = {
requestId: generateUUID(),
signRequest: {bids, randomUUID: alkimiConfig && alkimiConfig.randomUUID},
bidIds,
referer: bidderRequest.refererInfo.page,
signature: alkimiConfig && alkimiConfig.signature,
schain: validBidRequests[0].schain,
cpp: config.getConfig('coppa') ? 1 : 0,
device: {
dnt: getDNT() ? 1 : 0,
w: screen.width,
h: screen.height
},
ortb2: {
source,
user,
site: {
keywords: site?.keywords,
sectioncat: site?.sectioncat,
pagecat: site?.pagecat,
cat: site?.cat
},
at: ortb2?.at,
bcat: ortb2?.bcat,
wseat: ortb2?.wseat
}
}
if (bidderRequest && bidderRequest.gdprConsent) {
payload.gdprConsent = {
consentRequired: (typeof bidderRequest.gdprConsent.gdprApplies === 'boolean') ? bidderRequest.gdprConsent.gdprApplies : false,
consentString: bidderRequest.gdprConsent.consentString
}
}
if (bidderRequest.uspConsent) {
payload.uspConsent = bidderRequest.uspConsent;
}
if (eids) {
payload.eids = eids
}
const options = {
contentType: 'application/json',
customHeaders: {
'Rtb-Direct': true
}
}
return {
method: 'POST',
url: ENDPOINT,
data: payload,
options
};
},
interpretResponse: function (serverResponse, request) {
const serverBody = serverResponse.body;
if (!serverBody || typeof serverBody !== 'object') {
return [];
}
const {prebidResponse} = serverBody;
if (!Array.isArray(prebidResponse)) {
return [];
}
let bids = [];
prebidResponse.forEach(bidResponse => {
let bid = deepClone(bidResponse);
bid.cpm = parseFloat(bidResponse.cpm);
// banner or video
if (VIDEO === bid.mediaType) {
bid.vastUrl = replaceAuctionPrice(bid.winUrl, bid.cpm);
}
bid.meta = {};
bid.meta.advertiserDomains = bid.adomain || [];
bids.push(bid);
})
return bids;
},
onBidWon: function (bid) {
if (BANNER == bid.mediaType && bid.winUrl) {
const winUrl = replaceAuctionPrice(bid.winUrl, bid.cpm);
ajax(winUrl, null);
return true;
}
return false;
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent) {
if (syncOptions.iframeEnabled && serverResponses.length > 0) {
const serverBody = serverResponses[0].body;
if (!serverBody || typeof serverBody !== 'object') return [];
const { iframeList } = serverBody;
if (!Array.isArray(iframeList)) return [];
const urls = [];
iframeList.forEach(url => {
urls.push({type: 'iframe', url});
})
return urls;
}
return [];
}
}
function prepareSizes(sizes) {
return sizes ? sizes.map(size => ({width: size[0], height: size[1]})) : []
}
function prepareBidFloorSize(sizes) {
return sizes && sizes.length === 1 ? sizes : ['*'];
}
function getBidFloor(bidRequest, formatTypes) {
let minFloor
if (typeof bidRequest.getFloor === 'function') {
const bidFloorSizes = prepareBidFloorSize(bidRequest.sizes)
formatTypes.forEach(formatType => {
bidFloorSizes.forEach(bidFloorSize => {
const floor = bidRequest.getFloor({currency: 'USD', mediaType: formatType.toLowerCase(), size: bidFloorSize});
if (floor && !isNaN(floor.floor) && (floor.currency === 'USD')) {
minFloor = !minFloor || floor.floor < minFloor ? floor.floor : minFloor
}
})
})
}
return minFloor || bidRequest.params.bidFloor;
}
const getFormatType = bidRequest => {
let formats = []
if (deepAccess(bidRequest, 'mediaTypes.banner')) formats.push('Banner')
if (deepAccess(bidRequest, 'mediaTypes.video')) formats.push('Video')
return formats
}
const getUserId = () => {
if (storage.localStorageIsEnabled()) {
let userId = storage.getDataFromLocalStorage(USER_ID_KEY)
if (!userId) {
userId = generateUUID()
storage.setDataInLocalStorage(USER_ID_KEY, userId)
}
return userId
}
}
registerBidder(spec);