forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobkoiBidAdapter.js
198 lines (174 loc) · 6.73 KB
/
mobkoiBidAdapter.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
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import { deepAccess, deepSetValue, logError } from '../src/utils.js';
const BIDDER_CODE = 'mobkoi';
const GVL_ID = 898;
export const DEFAULT_AD_SERVER_BASE_URL = 'https://adserver.maximus.mobkoi.com';
const PUBLISHER_PARAMS = {
/**
* !IMPORTANT: This value must match the value in mobkoiAnalyticsAdapter.js
* The name of the parameter that the publisher can use to specify the ad server endpoint.
*/
PARAM_NAME_AD_SERVER_BASE_URL: 'adServerBaseUrl',
PARAM_NAME_PLACEMENT_ID: 'placementId',
}
export const converter = ortbConverter({
context: {
netRevenue: true,
ttl: 30,
},
request(buildRequest, imps, bidderRequest, context) {
const ortbRequest = buildRequest(imps, bidderRequest, context);
const prebidBidRequest = context.bidRequests[0];
ortbRequest.id = utils.getOrtbId(prebidBidRequest);
deepSetValue(ortbRequest, 'site.publisher.ext.adServerBaseUrl', utils.getAdServerEndpointBaseUrl(prebidBidRequest));
// We only support one impression per request.
deepSetValue(ortbRequest, 'imp.0.tagid', utils.getPlacementId(prebidBidRequest));
deepSetValue(ortbRequest, 'user.id', context.bidRequests[0].userId?.mobkoiId || null);
return ortbRequest;
},
bidResponse(buildPrebidBidResponse, ortbBidResponse, context) {
const prebidBid = buildPrebidBidResponse(ortbBidResponse, context);
utils.addCustomFieldsToPrebidBidResponse(prebidBid, ortbBidResponse);
return prebidBid;
},
});
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
gvlid: GVL_ID,
/**
* Determines whether or not the given bid request is valid.
*/
isBidRequestValid(bid) {
if (
!deepAccess(bid, `params.${PUBLISHER_PARAMS.PARAM_NAME_PLACEMENT_ID}`)
) {
logError(`The ${PUBLISHER_PARAMS.PARAM_NAME_PLACEMENT_ID} field is required in the bid request. ` +
'Please follow the setup guideline to set the placement ID field.')
return false;
}
return true;
},
/**
* Make a server request from the list of BidRequests.
*/
buildRequests(prebidBidRequests, prebidBidderRequest) {
const adServerEndpoint = utils.getAdServerEndpointBaseUrl(prebidBidderRequest) + '/bid';
return {
method: 'POST',
url: adServerEndpoint,
options: {
contentType: 'application/json',
},
data: converter.toORTB({
bidRequests: prebidBidRequests,
bidderRequest: prebidBidderRequest
}),
};
},
/**
* Unpack the response from the server into a list of bids.
*/
interpretResponse(serverResponse, customBidRequest) {
if (!serverResponse.body) return [];
const responseBody = {...serverResponse.body, seatbid: serverResponse.body.seatbid};
const prebidBidResponse = converter.fromORTB({
request: customBidRequest.data,
response: responseBody,
});
return prebidBidResponse.bids;
},
};
registerBidder(spec);
export const utils = {
/**
* !IMPORTANT: Make sure the implementation of this function matches getAdServerEndpointBaseUrl
* in both adapters.
* Obtain the Ad Server Base URL from the given Prebid object.
* @param {*} bid Prebid Bidder Request Object or Prebid Bid Response/Request
* or ORTB Request/Response Object
* @returns {string} The Ad Server Base URL
*/
getAdServerEndpointBaseUrl (bid) {
// Fields that would be automatically set if the publisher set it via pbjs.setBidderConfig.
const ortbPath = `site.publisher.ext.${PUBLISHER_PARAMS.PARAM_NAME_AD_SERVER_BASE_URL}`;
const prebidPath = `ortb2.${ortbPath}`;
// Fields that would be set by the publisher in the bid
// configuration object in ad unit.
const paramPath = `params.${PUBLISHER_PARAMS.PARAM_NAME_AD_SERVER_BASE_URL}`;
const bidRequestFirstBidParam = `bids.0.${paramPath}`;
const adServerBaseUrl =
deepAccess(bid, paramPath) ||
deepAccess(bid, bidRequestFirstBidParam) ||
deepAccess(bid, prebidPath) ||
deepAccess(bid, ortbPath) ||
DEFAULT_AD_SERVER_BASE_URL;
return adServerBaseUrl;
},
/**
* Extract the placement ID from the given object.
* @param {*} prebidBidRequestOrOrtbBidRequest
* @returns string
* @throws {Error} If the placement ID is not found in the given object.
*/
getPlacementId: function (prebidBidRequestOrOrtbBidRequest) {
// Fields that would be set by the publisher in the bid configuration object in ad unit.
const paramPath = 'params.placementId';
const bidRequestFirstBidParam = `bids.0.${paramPath}`;
// ORTB path for placement ID
const ortbPath = 'imp.0.tagid';
const placementId =
deepAccess(prebidBidRequestOrOrtbBidRequest, paramPath) ||
deepAccess(prebidBidRequestOrOrtbBidRequest, bidRequestFirstBidParam) ||
deepAccess(prebidBidRequestOrOrtbBidRequest, ortbPath);
if (!placementId) {
throw new Error(
'Failed to obtain placement ID from the given object. ' +
`Please set it via the "${paramPath}" field in the bid configuration.\n` +
'Given object:\n' +
JSON.stringify({functionParam: prebidBidRequestOrOrtbBidRequest}, null, 3)
);
}
return placementId;
},
/**
* !IMPORTANT: Make sure the implementation of this function matches utils.getOrtbId in
* mobkoiAnalyticsAdapter.js.
* We use the bidderRequestId as the ortbId. We could do so because we only
* make one ORTB request per Prebid Bidder Request.
* The ID field named differently when the value passed on to different contexts.
* @param {*} bid Prebid Bidder Request Object or Prebid Bid Response/Request
* or ORTB Request/Response Object
* @returns {string} The ORTB ID
* @throws {Error} If the ORTB ID cannot be found in the given object.
*/
getOrtbId(bid) {
const ortbId =
// called bidderRequestId in Prebid Request
bid.bidderRequestId ||
// called seatBidId in Prebid Bid Response Object
bid.seatBidId ||
// called ortbId in Interpreted Prebid Response Object
bid.ortbId ||
// called id in ORTB object
(Object.hasOwn(bid, 'imp') && bid.id);
if (!ortbId) {
throw new Error('Unable to find the ORTB ID in the bid object. Given Object:\n' +
JSON.stringify(bid, null, 2)
);
}
return ortbId;
},
/**
* Append custom fields to the prebid bid response. so that they can be accessed
* in various event handlers.
* @param {*} prebidBidResponse
* @param {*} ortbBidResponse
*/
addCustomFieldsToPrebidBidResponse(prebidBidResponse, ortbBidResponse) {
prebidBidResponse.ortbBidResponse = ortbBidResponse;
prebidBidResponse.ortbId = ortbBidResponse.id;
},
}