forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpilotxBidAdapter.js
155 lines (150 loc) · 5.17 KB
/
pilotxBidAdapter.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
import { registerBidder } from '../src/adapters/bidderFactory.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').validBidRequests} validBidRequests
*/
const BIDDER_CODE = 'pilotx';
const ENDPOINT_URL = '//adn.pilotx.tv/hb'
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: ['banner', 'video'],
aliases: ['pilotx'], // short code
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
let sizesCheck = !!bid.sizes
let paramSizesCheck = !!bid.params.sizes
var sizeConfirmed = false
if (sizesCheck) {
if (bid.sizes.length < 1) {
return false
} else {
sizeConfirmed = true
}
}
if (paramSizesCheck) {
if (bid.params.sizes.length < 1 && !sizeConfirmed) {
return false
} else {
sizeConfirmed = true
}
}
if (!sizeConfirmed) {
return false
}
return !!(bid.params.placementId);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
let payloadItems = {};
validBidRequests.forEach(bidRequest => {
let sizes = [];
let placementId = this.setPlacementID(bidRequest.params.placementId)
payloadItems[placementId] = {}
if (bidRequest.sizes.length > 0) {
if (Array.isArray(bidRequest.sizes[0])) {
for (let i = 0; i < bidRequest.sizes.length; i++) {
sizes[i] = [(bidRequest.sizes[i])[0], (bidRequest.sizes[i])[1]]
}
} else {
sizes[0] = [bidRequest.sizes[0], bidRequest.sizes[1]]
}
payloadItems[placementId]['sizes'] = sizes
}
if (bidRequest.mediaTypes != null) {
for (let i in bidRequest.mediaTypes) {
payloadItems[placementId][i] = {
...bidRequest.mediaTypes[i]
}
}
}
let consentTemp = ''
let consentRequiredTemp = false
if (bidderRequest && bidderRequest.gdprConsent) {
consentTemp = bidderRequest.gdprConsent.consentString
// will check if the gdprApplies field was populated with a boolean value (ie from page config). If it's undefined, then default to true
consentRequiredTemp = (typeof bidderRequest.gdprConsent.gdprApplies === 'boolean') ? bidderRequest.gdprConsent.gdprApplies : true
}
payloadItems[placementId]['gdprConsentString'] = consentTemp
payloadItems[placementId]['gdprConsentRequired'] = consentRequiredTemp
payloadItems[placementId]['bidId'] = bidRequest.bidId
});
const payload = payloadItems;
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: ENDPOINT_URL,
data: payloadString,
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequest) {
const serverBody = serverResponse.body;
const bidResponses = [];
if (serverBody.mediaType == 'banner') {
const bidResponse = {
requestId: serverBody.requestId,
cpm: serverBody.cpm,
width: serverBody.width,
height: serverBody.height,
creativeId: serverBody.creativeId,
currency: serverBody.currency,
netRevenue: false,
ttl: serverBody.ttl,
ad: serverBody.ad,
mediaType: 'banner',
meta: {
mediaType: 'banner',
advertiserDomains: serverBody.advertiserDomains
}
}
bidResponses.push(bidResponse)
} else if (serverBody.mediaType == 'video') {
const bidResponse = {
requestId: serverBody.requestId,
cpm: serverBody.cpm,
width: serverBody.width,
height: serverBody.height,
creativeId: serverBody.creativeId,
currency: serverBody.currency,
netRevenue: false,
ttl: serverBody.ttl,
vastUrl: serverBody.vastUrl,
mediaType: 'video',
meta: {
mediaType: 'video',
advertiserDomains: serverBody.advertiserDomains
}
}
bidResponses.push(bidResponse)
}
return bidResponses;
},
/**
* Formats placement ids for adserver ingestion purposes
* @param {string[]} placementId the placement ID/s in an array
*/
setPlacementID: function (placementId) {
if (Array.isArray(placementId)) {
return placementId.join('#')
}
return placementId
},
}
registerBidder(spec);