forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc1xBidAdapter.js
213 lines (184 loc) · 5.73 KB
/
c1xBidAdapter.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
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { logInfo, logError } from '../src/utils.js';
import { BANNER } from '../src/mediaTypes.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
*/
const BIDDER_CODE = 'c1x';
const URL = 'https://hb-stg.c1exchange.com/ht';
// const PIXEL_ENDPOINT = '//px.c1exchange.com/pubpixel/';
const LOG_MSG = {
invalidBid: 'C1X: [ERROR] bidder returns an invalid bid',
noSite: 'C1X: [ERROR] no site id supplied',
noBid: 'C1X: [INFO] creating a NO bid for Adunit: ',
bidWin: 'C1X: [INFO] creating a bid for Adunit: '
};
/**
* Adapter for requesting bids from C1X header tag server.
* v3.1 (c) C1X Inc., 2018
*/
export const c1xAdapter = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
// check the bids sent to c1x bidder
isBidRequestValid: function (bid) {
if (bid.bidder !== BIDDER_CODE || typeof bid.params === 'undefined') {
return false;
}
if (typeof bid.params.placementId === 'undefined') {
return false;
}
return true;
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} validBidRequests A non-empty list of valid bid requests that should be sent to the Server.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
let payload = {};
let tagObj = {};
let bidRequest = [];
const adunits = validBidRequests.length;
const rnd = new Date().getTime();
const c1xTags = validBidRequests.map(bidToTag);
const bidIdTags = validBidRequests.map(bidToShortTag); // include only adUnitCode and bidId from request obj
// flattened tags in a tag object
tagObj = c1xTags.reduce((current, next) => Object.assign(current, next));
payload = {
adunits: adunits.toString(),
rnd: rnd.toString(),
response: 'json',
compress: 'gzip'
};
// for GDPR support
if (bidderRequest && bidderRequest.gdprConsent) {
payload['consent_string'] = bidderRequest.gdprConsent.consentString;
payload['consent_required'] = (typeof bidderRequest.gdprConsent.gdprApplies === 'boolean') ? bidderRequest.gdprConsent.gdprApplies.toString() : 'true'
;
}
Object.assign(payload, tagObj);
let payloadString = stringifyPayload(payload);
// ServerRequest object
bidRequest.push({
method: 'GET',
url: URL,
data: payloadString,
bids: bidIdTags
});
return bidRequest;
},
interpretResponse: function (serverResponse, requests) {
serverResponse = serverResponse.body;
requests = requests.bids || [];
const currency = 'USD';
const bidResponses = [];
let netRevenue = false;
if (!serverResponse || serverResponse.error) {
let errorMessage = serverResponse.error;
logError(LOG_MSG.invalidBid + errorMessage);
return bidResponses;
} else {
serverResponse.forEach(bid => {
logInfo(bid)
if (bid.bid) {
if (bid.bidType === 'NET_BID') {
netRevenue = !netRevenue;
}
const curBid = {
requestId: bid.bidId,
width: bid.width,
height: bid.height,
cpm: bid.cpm,
ad: bid.ad,
creativeId: bid.crid,
currency: currency,
ttl: 300,
netRevenue: netRevenue
};
if (bid.dealId) {
curBid['dealId'] = bid.dealId
}
for (let i = 0; i < requests.length; i++) {
if (bid.adId === requests[i].adUnitCode) {
curBid.requestId = requests[i].bidId;
}
}
logInfo(LOG_MSG.bidWin + bid.adId + ' size: ' + curBid.width + 'x' + curBid.height);
bidResponses.push(curBid);
} else {
// no bid
logInfo(LOG_MSG.noBid + bid.adId);
}
});
}
return bidResponses;
}
}
function bidToTag(bid, index) {
const tag = {};
const adIndex = 'a' + (index + 1).toString(); // ad unit id for c1x
const sizeKey = adIndex + 's';
const priceKey = adIndex + 'p';
const dealKey = adIndex + 'd';
// TODO: Multiple Floor Prices
const sizesArr = bid.sizes;
const floorPriceMap = getBidFloor(bid);
const dealId = bid.params.dealId || '';
if (dealId) {
tag[dealKey] = dealId;
}
tag[adIndex] = bid.adUnitCode;
tag[sizeKey] = sizesArr.reduce((prev, current) => prev + (prev === '' ? '' : ',') + current.join('x'), '');
const newSizeArr = tag[sizeKey].split(',');
if (floorPriceMap) {
newSizeArr.forEach(size => {
if (size in floorPriceMap) {
tag[priceKey] = floorPriceMap[size].toString();
} // we only accept one cpm price in floorPriceMap
});
}
if (bid.params.pageurl) {
tag['pageurl'] = bid.params.pageurl;
}
return tag;
}
function getBidFloor(bidRequest) {
let floorInfo = {};
if (typeof bidRequest.getFloor === 'function') {
floorInfo = bidRequest.getFloor({
currency: 'USD',
mediaType: 'banner',
size: '*',
});
}
let floor =
floorInfo?.floor ||
bidRequest.params.bidfloor ||
bidRequest.params.floorPriceMap ||
0;
return floor;
}
function bidToShortTag(bid) {
const tag = {};
tag.adUnitCode = bid.adUnitCode;
tag.bidId = bid.bidId;
return tag;
}
function stringifyPayload(payload) {
let payloadString = [];
for (var key in payload) {
if (payload.hasOwnProperty(key)) {
payloadString.push(key + '=' + payload[key]);
}
}
return payloadString.join('&');
}
registerBidder(c1xAdapter);