forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediaConsortiumBidAdapter.js
273 lines (224 loc) · 6.71 KB
/
mediaConsortiumBidAdapter.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
import {BANNER, VIDEO} from '../src/mediaTypes.js'
import {registerBidder} from '../src/adapters/bidderFactory.js'
import {generateUUID, isPlainObject, isArray, logWarn, deepClone} from '../src/utils.js'
import {Renderer} from '../src/Renderer.js'
import {OUTSTREAM} from '../src/video.js'
import {config} from '../src/config.js';
import { getStorageManager } from '../src/storageManager.js';
const BIDDER_CODE = 'mediaConsortium'
const PROFILE_API_USAGE_CONFIG_KEY = 'useProfileApi'
const ONE_PLUS_X_ID_USAGE_CONFIG_KEY = 'readOnePlusXId'
const SYNC_ENDPOINT = 'https://relay.hubvisor.io/v1/sync/big'
const AUCTION_ENDPOINT = 'https://relay.hubvisor.io/v1/auction/big'
const XANDR_OUTSTREAM_RENDERER_URL = 'https://acdn.adnxs.com/video/outstream/ANOutstreamVideo.js';
export const OPTIMIZATIONS_STORAGE_KEY = 'media_consortium_optimizations'
const SYNC_TYPES = {
image: 'image',
redirect: 'image',
iframe: 'iframe'
}
const storageManager = getStorageManager({ bidderCode: BIDDER_CODE });
export const spec = {
version: '0.0.1',
code: BIDDER_CODE,
gvlid: 1112,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid(bid) {
return true
},
buildRequests(bidRequests, bidderRequest) {
const useProfileApi = config.getConfig(PROFILE_API_USAGE_CONFIG_KEY) ?? false
const readOnePlusXId = config.getConfig(ONE_PLUS_X_ID_USAGE_CONFIG_KEY) ?? false
const {
auctionId,
bids,
gdprConsent: {gdprApplies = false, consentString} = {},
ortb2: {device, site}
} = bidderRequest
const currentTimestamp = Date.now()
const optimizations = getOptimizationsFromLocalStorage()
const impressions = bids.reduce((acc, bidRequest) => {
const {bidId, adUnitCode, mediaTypes} = bidRequest
const optimization = optimizations[adUnitCode]
if (optimization) {
const {expiresAt, isEnabled} = optimization
if (expiresAt >= currentTimestamp && !isEnabled) {
return acc
}
}
let finalizedMediatypes = deepClone(mediaTypes)
if (mediaTypes.video && mediaTypes.video.context !== OUTSTREAM) {
logWarn(`Filtering video request for adUnitCode ${adUnitCode} because context is not ${OUTSTREAM}`)
if (Object.keys(finalizedMediatypes).length > 1) {
delete finalizedMediatypes.video
} else {
return acc
}
}
return acc.concat({id: bidId, adUnitCode, mediaTypes: finalizedMediatypes})
}, [])
if (!impressions.length) {
return
}
const request = {
id: auctionId ?? generateUUID(),
impressions,
device,
site,
user: {
ids: {}
},
regulations: {
gdpr: {
applies: gdprApplies,
consentString
}
},
timeout: 3600,
options: {
useProfileApi
}
}
if (readOnePlusXId) {
const fpId = getFpIdFromLocalStorage()
if (fpId) {
request.user.ids['1plusX'] = fpId
}
}
const syncData = {
gdpr: gdprApplies,
ad_unit_codes: impressions.map(({adUnitCode}) => adUnitCode).join(',')
}
if (consentString) {
syncData.gdpr_consent = consentString
}
return [
{
method: 'GET',
url: SYNC_ENDPOINT,
data: syncData
},
{
method: 'POST',
url: AUCTION_ENDPOINT,
data: request
}
]
},
interpretResponse(serverResponse, params) {
if (!isValidResponse(serverResponse)) return []
const {body: {bids, optimizations}} = serverResponse
if (optimizations && isArray(optimizations)) {
const currentTimestamp = Date.now()
const optimizationsToStore = optimizations.reduce((acc, optimization) => {
const {adUnitCode, isEnabled, ttl} = optimization
return {
...acc,
[adUnitCode]: {isEnabled, expiresAt: currentTimestamp + ttl}
}
}, getOptimizationsFromLocalStorage())
storageManager.setDataInLocalStorage(OPTIMIZATIONS_STORAGE_KEY, JSON.stringify(optimizationsToStore))
}
return bids.map((bid) => {
const {
impressionId,
price: {cpm, currency},
dealId,
ad: {
creative: {id, mediaType, size: {width, height}, markup}
},
ttl = 360
} = bid
const formattedBid = {
requestId: impressionId,
cpm,
currency,
dealId,
ttl,
netRevenue: true,
creativeId: id,
mediaType,
width,
height,
ad: markup,
adUrl: null
}
if (mediaType === VIDEO) {
const impressionRequest = params.data.impressions.find(({id}) => id === impressionId)
formattedBid.vastXml = markup
if (impressionRequest) {
formattedBid.renderer = buildXandrOutstreamRenderer(impressionId, impressionRequest.adUnitCode)
} else {
logWarn(`Could not find adUnitCode matching the impressionId ${impressionId} to setup the renderer`)
}
}
return formattedBid
})
},
getUserSyncs(syncOptions, serverResponses) {
if (serverResponses.length !== 2) {
return
}
const [sync] = serverResponses
return sync.body?.bidders?.reduce((acc, {type, url}) => {
const syncType = SYNC_TYPES[type]
if (!syncType || !url) {
return acc
}
return acc.concat({type: syncType, url})
}, [])
}
}
registerBidder(spec)
export function getOptimizationsFromLocalStorage() {
try {
const storedOptimizations = storageManager.getDataFromLocalStorage(OPTIMIZATIONS_STORAGE_KEY)
return storedOptimizations ? JSON.parse(storedOptimizations) : {}
} catch (err) {
return {}
}
}
function getFpIdFromLocalStorage() {
try {
return storageManager.getDataFromLocalStorage('ope_fpid')
} catch (err) {
return null
}
}
function isValidResponse(response) {
return isPlainObject(response) &&
isPlainObject(response.body) &&
isArray(response.body.bids)
}
function buildXandrOutstreamRenderer(bidId, adUnitCode) {
const renderer = Renderer.install({
id: bidId,
url: XANDR_OUTSTREAM_RENDERER_URL,
loaded: false,
adUnitCode,
targetId: adUnitCode
});
try {
renderer.setRender(xandrOutstreamRenderer);
} catch (err) {
logWarn('Prebid Error calling setRender on renderer', err);
}
return renderer;
}
function xandrOutstreamRenderer(bid) {
const {width, height, adUnitCode, vastXml} = bid
bid.renderer.push(() => {
window.ANOutstreamVideo.renderAd({
sizes: [width, height],
targetId: adUnitCode,
rendererOptions: {
showBigPlayButton: false,
showProgressBar: 'bar',
content: vastXml,
showVolume: false,
allowFullscreen: true,
skippable: false
}
});
});
}