forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccxBidAdapter.js
245 lines (213 loc) · 7.12 KB
/
ccxBidAdapter.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
import {_each, deepAccess, isArray, isEmpty, logWarn} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getStorageManager} from '../src/storageManager.js';
const BIDDER_CODE = 'ccx'
const storage = getStorageManager({bidderCode: BIDDER_CODE});
const BID_URL = 'https://delivery.clickonometrics.pl/ortb/prebid/bid'
const GVLID = 773;
const SUPPORTED_VIDEO_PROTOCOLS = [2, 3, 5, 6]
const SUPPORTED_VIDEO_MIMES = ['video/mp4', 'video/x-flv']
const SUPPORTED_VIDEO_PLAYBACK_METHODS = [1, 2, 3, 4]
function _getDeviceObj () {
let device = {}
device.w = screen.width
device.y = screen.height
device.ua = navigator.userAgent
return device
}
function _getSiteObj (bidderRequest) {
let site = {}
let url = bidderRequest?.refererInfo?.page || ''
if (url.length > 0) {
url = url.split('?')[0]
}
site.page = url
return site
}
function _validateSizes (sizeObj, type) {
if (!isArray(sizeObj) || typeof sizeObj[0] === 'undefined') {
return false
}
if (type === 'video' && (!isArray(sizeObj[0]) || sizeObj[0].length !== 2)) {
return false
}
let result = true
if (type === 'banner') {
_each(sizeObj, function (size) {
if (!isArray(size) || (size.length !== 2)) {
result = false
}
})
return result
}
if (type === 'old') {
if (!isArray(sizeObj[0]) && sizeObj.length !== 2) {
result = false
} else if (isArray(sizeObj[0])) {
_each(sizeObj, function (size) {
if (!isArray(size) || (size.length !== 2)) {
result = false
}
})
}
return result;
}
return true
}
function _buildBid (bid, bidderRequest) {
let placement = {}
placement.id = bid.bidId
placement.secure = 1
let sizes = deepAccess(bid, 'mediaTypes.banner.sizes') || deepAccess(bid, 'mediaTypes.video.playerSize') || deepAccess(bid, 'sizes')
if (deepAccess(bid, 'mediaTypes.banner') || deepAccess(bid, 'mediaType') === 'banner' || (!deepAccess(bid, 'mediaTypes.video') && !deepAccess(bid, 'mediaType'))) {
placement.banner = {'format': []}
if (isArray(sizes[0])) {
_each(sizes, function (size) {
placement.banner.format.push({'w': size[0], 'h': size[1]})
})
} else {
placement.banner.format.push({'w': sizes[0], 'h': sizes[1]})
}
} else if (deepAccess(bid, 'mediaTypes.video') || deepAccess(bid, 'mediaType') === 'video') {
placement.video = {}
if (typeof sizes !== 'undefined') {
if (isArray(sizes[0])) {
placement.video.w = sizes[0][0]
placement.video.h = sizes[0][1]
} else {
placement.video.w = sizes[0]
placement.video.h = sizes[1]
}
}
placement.video.protocols = deepAccess(bid, 'mediaTypes.video.protocols') || deepAccess(bid, 'params.video.protocols') || SUPPORTED_VIDEO_PROTOCOLS
placement.video.mimes = deepAccess(bid, 'mediaTypes.video.mimes') || deepAccess(bid, 'params.video.mimes') || SUPPORTED_VIDEO_MIMES
placement.video.playbackmethod = deepAccess(bid, 'mediaTypes.video.playbackmethod') || deepAccess(bid, 'params.video.playbackmethod') || SUPPORTED_VIDEO_PLAYBACK_METHODS
placement.video.skip = deepAccess(bid, 'mediaTypes.video.skip') || deepAccess(bid, 'params.video.skip') || 0
if (placement.video.skip === 1 && (deepAccess(bid, 'mediaTypes.video.skipafter') || deepAccess(bid, 'params.video.skipafter'))) {
placement.video.skipafter = deepAccess(bid, 'mediaTypes.video.skipafter') || deepAccess(bid, 'params.video.skipafter')
}
}
placement.ext = {'pid': bid.params.placementId}
if (bidderRequest.paapi?.enabled) {
placement.ext.ae = bid?.ortb2Imp?.ext?.ae
}
return placement
}
function _buildResponse (bid, currency, ttl) {
let resp = {
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
creativeId: bid.crid,
netRevenue: false,
ttl: ttl,
currency: currency
}
resp.meta = {};
if (bid.adomain && bid.adomain.length > 0) {
resp.meta.advertiserDomains = bid.adomain;
}
if (bid.ext.type === 'video') {
resp.vastXml = bid.adm
} else {
resp.ad = bid.adm
}
if (deepAccess(bid, 'dealid')) {
resp.dealId = bid.dealid
}
return resp
}
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: ['banner', 'video'],
isBidRequestValid: function (bid) {
if (!deepAccess(bid, 'params.placementId')) {
logWarn('placementId param is reqeuired.')
return false
}
if (deepAccess(bid, 'mediaTypes.banner.sizes')) {
let isValid = _validateSizes(bid.mediaTypes.banner.sizes, 'banner')
if (!isValid) {
logWarn('Bid sizes are invalid.')
}
return isValid
} else if (deepAccess(bid, 'mediaTypes.video.playerSize')) {
let isValid = _validateSizes(bid.mediaTypes.video.playerSize, 'video')
if (!isValid) {
logWarn('Bid sizes are invalid.')
}
return isValid
} else if (deepAccess(bid, 'sizes')) {
let isValid = _validateSizes(bid.sizes, 'old')
if (!isValid) {
logWarn('Bid sizes are invalid.')
}
return isValid
} else {
logWarn('Bid sizes are required.')
return false
}
},
buildRequests: function (validBidRequests, bidderRequest) {
// check if validBidRequests is not empty
if (validBidRequests.length > 0) {
let requestBody = {}
requestBody.imp = []
requestBody.site = _getSiteObj(bidderRequest)
requestBody.device = _getDeviceObj()
requestBody.id = bidderRequest.bidderRequestId;
requestBody.ext = {'ce': (storage.cookiesAreEnabled() ? 1 : 0)}
// Attaching GDPR Consent Params
if (bidderRequest && bidderRequest.gdprConsent) {
requestBody.user = {
ext: {
consent: bidderRequest.gdprConsent.consentString
}
};
requestBody.regs = {
ext: {
gdpr: (bidderRequest.gdprConsent.gdprApplies ? 1 : 0)
}
};
}
_each(validBidRequests, function (bid) {
requestBody.imp.push(_buildBid(bid, bidderRequest))
})
// Return the server request
return {
'method': 'POST',
'url': BID_URL,
'data': JSON.stringify(requestBody)
}
}
},
interpretResponse: function (serverResponse, request) {
const bidResponses = []
// response is not empty (HTTP 204)
if (!isEmpty(serverResponse.body)) {
_each(serverResponse.body.seatbid, function (seatbid) {
_each(seatbid.bid, function (bid) {
bidResponses.push(_buildResponse(bid, serverResponse.body.cur, serverResponse.body.ext.ttl))
})
})
}
return bidResponses
},
getUserSyncs: function (syncOptions, serverResponses) {
const syncs = []
if (deepAccess(serverResponses[0], 'body.ext.usersync') && !isEmpty(serverResponses[0].body.ext.usersync)) {
_each(serverResponses[0].body.ext.usersync, function (match) {
if ((syncOptions.iframeEnabled && match.type === 'iframe') || (syncOptions.pixelEnabled && match.type === 'image')) {
syncs.push({
type: match.type,
url: match.url
})
}
})
}
return syncs
}
}
registerBidder(spec)