-
Notifications
You must be signed in to change notification settings - Fork 2k
/
yieldlabBidAdapter.js
215 lines (191 loc) · 6.3 KB
/
yieldlabBidAdapter.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
import * as utils from '../src/utils.js'
import { registerBidder } from '../src/adapters/bidderFactory.js'
import find from 'core-js-pure/features/array/find.js'
import { VIDEO, BANNER } from '../src/mediaTypes.js'
import { Renderer } from '../src/Renderer.js'
const ENDPOINT = 'https://ad.yieldlab.net'
const BIDDER_CODE = 'yieldlab'
const BID_RESPONSE_TTL_SEC = 300
const CURRENCY_CODE = 'EUR'
const OUTSTREAMPLAYER_URL = 'https://ad2.movad.net/dynamic.ad?a=o193092&ma_loadEvent=ma-start-event'
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [VIDEO, BANNER],
isBidRequestValid: function (bid) {
if (bid && bid.params && bid.params.adslotId && bid.params.supplyId) {
return true
}
return false
},
/**
* This method should build correct URL
* @param validBidRequests
* @returns {{method: string, url: string}}
*/
buildRequests: function (validBidRequests, bidderRequest) {
const adslotIds = []
const timestamp = Date.now()
const query = {
ts: timestamp,
json: true
}
utils._each(validBidRequests, function (bid) {
adslotIds.push(bid.params.adslotId)
if (bid.params.targeting) {
query.t = createQueryString(bid.params.targeting)
}
if (bid.userIdAsEids && Array.isArray(bid.userIdAsEids)) {
query.ids = createUserIdString(bid.userIdAsEids)
}
if (bid.params.customParams && utils.isPlainObject(bid.params.customParams)) {
for (let prop in bid.params.customParams) {
query[prop] = bid.params.customParams[prop]
}
}
})
if (bidderRequest && bidderRequest.gdprConsent) {
query.gdpr = (typeof bidderRequest.gdprConsent.gdprApplies === 'boolean') ? bidderRequest.gdprConsent.gdprApplies : true
if (query.gdpr) {
query.consent = bidderRequest.gdprConsent.consentString
}
}
const adslots = adslotIds.join(',')
const queryString = createQueryString(query)
return {
method: 'GET',
url: `${ENDPOINT}/yp/${adslots}?${queryString}`,
validBidRequests: validBidRequests
}
},
/**
* Map ad values and pricing and stuff
* @param serverResponse
* @param originalBidRequest
*/
interpretResponse: function (serverResponse, originalBidRequest) {
const bidResponses = []
const timestamp = Date.now()
originalBidRequest.validBidRequests.forEach(function (bidRequest) {
if (!serverResponse.body) {
return
}
let matchedBid = find(serverResponse.body, function (bidResponse) {
return bidRequest.params.adslotId == bidResponse.id
})
if (matchedBid) {
const primarysize = bidRequest.sizes.length === 2 && !utils.isArray(bidRequest.sizes[0]) ? bidRequest.sizes : bidRequest.sizes[0]
const customsize = bidRequest.params.adSize !== undefined ? parseSize(bidRequest.params.adSize) : primarysize
const extId = bidRequest.params.extId !== undefined ? '&id=' + bidRequest.params.extId : ''
const adType = matchedBid.adtype !== undefined ? matchedBid.adtype : ''
const bidResponse = {
requestId: bidRequest.bidId,
cpm: matchedBid.price / 100,
width: customsize[0],
height: customsize[1],
creativeId: '' + matchedBid.id,
dealId: (matchedBid['c.dealid']) ? matchedBid['c.dealid'] : matchedBid.pid,
currency: CURRENCY_CODE,
netRevenue: false,
ttl: BID_RESPONSE_TTL_SEC,
referrer: '',
ad: `<script src="${ENDPOINT}/d/${matchedBid.id}/${bidRequest.params.supplyId}/${customsize[0]}x${customsize[1]}?ts=${timestamp}${extId}"></script>`
}
if (isVideo(bidRequest, adType)) {
const playersize = getPlayerSize(bidRequest)
if (playersize) {
bidResponse.width = playersize[0]
bidResponse.height = playersize[1]
}
bidResponse.mediaType = VIDEO
bidResponse.vastUrl = `${ENDPOINT}/d/${matchedBid.id}/${bidRequest.params.supplyId}/${customsize[0]}x${customsize[1]}?ts=${timestamp}${extId}`
if (isOutstream(bidRequest)) {
const renderer = Renderer.install({
id: bidRequest.bidId,
url: OUTSTREAMPLAYER_URL,
loaded: false
})
renderer.setRender(outstreamRender)
bidResponse.renderer = renderer
}
}
bidResponses.push(bidResponse)
}
})
return bidResponses
}
};
/**
* Is this a video format?
* @param {Object} format
* @param {String} adtype
* @returns {Boolean}
*/
function isVideo (format, adtype) {
return utils.deepAccess(format, 'mediaTypes.video') && adtype.toLowerCase() === 'video'
}
/**
* Is this an outstream context?
* @param {Object} format
* @returns {Boolean}
*/
function isOutstream (format) {
let context = utils.deepAccess(format, 'mediaTypes.video.context')
return (context === 'outstream')
}
/**
* Gets optional player size
* @param {Object} format
* @returns {Array}
*/
function getPlayerSize (format) {
let playerSize = utils.deepAccess(format, 'mediaTypes.video.playerSize')
return (playerSize && utils.isArray(playerSize[0])) ? playerSize[0] : playerSize
}
/**
* Expands a 'WxH' string as a 2-element [W, H] array
* @param {String} size
* @returns {Array}
*/
function parseSize (size) {
return size.split('x').map(Number)
}
/**
* Creates a string out of an array of eids with source and uid
* @param {Array} eids
* @returns {String}
*/
function createUserIdString (eids) {
let str = []
for (let i = 0; i < eids.length; i++) {
str.push(eids[i].source + ':' + eids[i].uids[0].id)
}
return str.join(',')
}
/**
* Creates a querystring out of an object with key-values
* @param {Object} obj
* @returns {String}
*/
function createQueryString (obj) {
let str = []
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]))
}
}
return str.join('&')
}
/**
* Handles an outstream response after the library is loaded
* @param {Object} bid
*/
function outstreamRender(bid) {
bid.renderer.push(() => {
window.ma_width = bid.width
window.ma_height = bid.height
window.ma_vastUrl = bid.vastUrl
window.ma_container = bid.adUnitCode
window.document.dispatchEvent(new Event('ma-start-event'))
});
}
registerBidder(spec)