forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
codefuelBidAdapter.js
176 lines (157 loc) · 5.03 KB
/
codefuelBidAdapter.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
import {isArray, setOnAny} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').validBidRequests} validBidRequests
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').SyncOptions} SyncOptions
* @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync
*/
const BIDDER_CODE = 'codefuel';
const CURRENCY = 'USD';
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [ BANNER ],
aliases: ['ex'], // 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) {
if (bid.nativeParams) {
return false;
}
return !!(bid.params.placementId || (bid.params.member && bid.params.invCode));
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests} validBidRequests - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests, bidderRequest) {
const page = bidderRequest.refererInfo.page;
const domain = bidderRequest.refererInfo.domain;
const ua = navigator.userAgent;
const devicetype = getDeviceType()
const publisher = setOnAny(validBidRequests, 'params.publisher');
const cur = CURRENCY;
const endpointUrl = 'https://ai-p-codefuel-ds-rtb-us-east-1-k8s.seccint.com/prebid'
const timeout = bidderRequest.timeout;
validBidRequests.forEach(bid => bid.netRevenue = 'net');
const imps = validBidRequests.map((bid, idx) => {
const imp = {
id: idx + 1 + ''
}
if (bid.params.tagid) {
imp.tagid = bid.params.tagid
}
if (bid.sizes) {
imp.banner = {
format: transformSizes(bid.sizes)
}
}
return imp;
});
const request = {
id: bidderRequest.bidderRequestId,
site: { page, domain, publisher },
device: { ua, devicetype },
source: { fd: 1 },
cur: [cur],
tmax: timeout,
imp: imps,
};
return {
method: 'POST',
url: endpointUrl,
data: request,
bids: validBidRequests,
options: {
withCredentials: false
}
};
},
/**
* 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: (serverResponse, { bids }) => {
if (!serverResponse.body) {
return [];
}
const { seatbid, cur } = serverResponse.body;
const bidResponses = flatten(seatbid.map(seat => seat.bid)).reduce((result, bid) => {
result[bid.impid - 1] = bid;
return result;
}, []);
return bids.map((bid, id) => {
const bidResponse = bidResponses[id];
if (bidResponse) {
const bidObject = {
requestId: bid.bidId,
cpm: bidResponse.price,
creativeId: bidResponse.crid,
ttl: 360,
netRevenue: true,
currency: cur,
mediaType: BANNER,
ad: bidResponse.adm,
width: bidResponse.w,
height: bidResponse.h,
meta: { advertiserDomains: bid.adomain ? bid.adomain : [] }
};
return bidObject;
}
}).filter(Boolean);
},
/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {
return [];
}
}
registerBidder(spec);
function getDeviceType() {
if ((/ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(navigator.userAgent.toLowerCase()))) {
return 5; // 'tablet'
}
if ((/iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(navigator.userAgent.toLowerCase()))) {
return 4; // 'mobile'
}
return 2; // 'desktop'
}
function flatten(arr) {
return [].concat(...arr);
}
/* Turn bid request sizes into ut-compatible format */
function transformSizes(requestSizes) {
if (!isArray(requestSizes)) {
return [];
}
if (requestSizes.length === 2 && !isArray(requestSizes[0])) {
return [{
w: parseInt(requestSizes[0], 10),
h: parseInt(requestSizes[1], 10)
}];
} else if (isArray(requestSizes[0])) {
return requestSizes.map(item =>
({
w: parseInt(item[0], 10),
h: parseInt(item[1], 10)
})
);
}
return [];
}