forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupadBidAdapter.js
194 lines (162 loc) · 5.26 KB
/
setupadBidAdapter.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
import {
_each,
isStr,
getBidIdParameter,
triggerPixel,
logWarn,
deepSetValue,
} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
const BIDDER_CODE = 'setupad';
const ENDPOINT = 'https://prebid.setupad.io/openrtb2/auction';
const SYNC_ENDPOINT = 'https://cookie.stpd.cloud/sync?';
const REPORT_ENDPOINT = 'https://adapter-analytics.setupad.io/api/adapter-analytics?';
const GVLID = 1241;
const TIME_TO_LIVE = 360;
export const biddersCreativeIds = {}; // export only for tests
const NET_REVENUE = true;
const TEST_REQUEST = 0; // used only for testing
const converter = ortbConverter({
context: {
netRevenue: NET_REVENUE,
ttl: TIME_TO_LIVE,
},
imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
deepSetValue(
imp,
'ext.prebid.storedrequest.id',
getBidIdParameter('placement_id', bidRequest.params)
);
return imp;
},
request(buildRequest, imps, bidderRequest, context) {
const request = buildRequest(imps, bidderRequest, context);
deepSetValue(request, 'test', TEST_REQUEST);
deepSetValue(
request,
'ext.prebid.storedrequest.id',
getBidIdParameter(
'account_id',
bidderRequest.bids.find((bid) => bid.hasOwnProperty('params')).params
)
);
deepSetValue(request, 'setupad', 'adapter');
return request;
},
});
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
gvlid: GVLID,
isBidRequestValid: function (bid) {
return !!(
bid.params.placement_id &&
isStr(bid.params.placement_id) &&
bid.params.account_id &&
isStr(bid.params.account_id)
);
},
buildRequests: function (validBidRequests, bidderRequest) {
const data = converter.toORTB({ validBidRequests, bidderRequest });
return {
method: 'POST',
url: ENDPOINT,
data,
options: {
contentType: 'text/plain',
withCredentials: true,
},
};
},
interpretResponse: function (serverResponse, bidRequest) {
if (
!serverResponse ||
!serverResponse.body ||
typeof serverResponse.body != 'object' ||
Object.keys(serverResponse.body).length === 0
) {
logWarn('no response or body is malformed');
return [];
}
// set a seat for creativeId for triggerPixel url
_each(serverResponse.body.seatbid, (res) => {
_each(res.bid, (bid) => {
biddersCreativeIds[bid.crid] = res.seat;
});
});
// used for a test case "should update biddersCreativeIds correctly" to return early and not throw ORTB error
if (serverResponse.testCase === 1) return;
const bids = converter.fromORTB({
response: serverResponse.body,
request: bidRequest.data,
}).bids;
return bids;
},
getUserSyncs: function (syncOptions, responses, gdprConsent, uspConsent) {
if (!responses?.length) return [];
const syncs = [];
const bidders = getBidders(responses);
if (syncOptions.iframeEnabled && bidders) {
const queryParams = [];
queryParams.push(`bidders=${bidders}`);
queryParams.push('gdpr=' + +gdprConsent.gdprApplies);
queryParams.push('gdpr_consent=' + gdprConsent.consentString);
queryParams.push('usp_consent=' + (uspConsent || ''));
const strQueryParams = queryParams.join('&');
syncs.push({
type: 'iframe',
url: SYNC_ENDPOINT + strQueryParams + '&type=iframe',
});
return syncs;
}
return [];
},
onBidWon: function (bid) {
let bidder = bid.bidder || bid.bidderCode;
const auctionId = bid.auctionId;
if (bidder !== BIDDER_CODE) return;
let params;
if (bid.params) {
params = Array.isArray(bid.params) ? bid.params : [bid.params];
} else {
if (Array.isArray(bid.bids)) {
params = bid.bids.map((singleBid) => singleBid.params);
}
}
if (!params?.length) return;
const placementIdsArray = [];
params.forEach((param) => {
if (!param.placement_id) return;
placementIdsArray.push(param.placement_id);
});
const placementIds = (placementIdsArray.length && placementIdsArray.join(';')) || '';
if (!placementIds) return;
// find the winning bidder by using creativeId as identification
if (biddersCreativeIds.hasOwnProperty(bid.creativeId) && biddersCreativeIds[bid.creativeId]) {
bidder = biddersCreativeIds[bid.creativeId];
}
const queryParams = [];
queryParams.push(`event=bidWon`);
queryParams.push('bidder=' + bidder);
queryParams.push('placementIds=' + placementIds);
queryParams.push('auctionId=' + auctionId);
queryParams.push('cpm=' + bid.originalCpm);
queryParams.push('currency=' + bid.originalCurrency);
queryParams.push('timestamp=' + Date.now());
const strQueryParams = queryParams.join('&');
const url = REPORT_ENDPOINT + strQueryParams;
triggerPixel(url);
},
};
function getBidders(serverResponse) {
const bidders = serverResponse
.map((res) => Object.keys(res.body.ext.responsetimemillis || []))
.flat(1);
if (bidders.length) {
return encodeURIComponent(JSON.stringify([...new Set(bidders)]));
}
}
registerBidder(spec);