forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueBidAdapter.js
122 lines (104 loc) · 3.29 KB
/
blueBidAdapter.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
import { ortbConverter } from '../libraries/ortbConverter/converter.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import { getStorageManager } from '../src/storageManager.js';
import { deepSetValue, isFn, isPlainObject } from '../src/utils.js';
const BIDDER_CODE = 'blue';
const ENDPOINT_URL = 'https://bidder-us-east-1.getblue.io/engine/?src=prebid';
const GVLID = 620; // GVLID for your bidder
const COOKIE_NAME = 'ckid'; // Cookie name for identifying users
const CURRENCY = 'USD'; // Currency used in bid floors
export const storage = getStorageManager({ bidderCode: BIDDER_CODE });
const converter = ortbConverter({
context: {
netRevenue: true, // Default netRevenue setting
ttl: 100, // Default time-to-live for bid responses
},
imp,
request,
});
function request(buildRequest, imps, bidderRequest, context) {
let request = buildRequest(imps, bidderRequest, context);
deepSetValue(request, 'site.publisher.id', context.publisherId);
return request;
}
function imp(buildImp, bidRequest, context) {
const imp = buildImp(bidRequest, context);
const floor = getBidFloor(bidRequest);
if (floor) {
imp.bidfloor = floor;
imp.bidfloorcur = CURRENCY;
}
return imp;
}
function getBidFloor(bid) {
if (isFn(bid.getFloor)) {
let floor = bid.getFloor({
currency: CURRENCY,
mediaType: BANNER,
size: '*',
});
if (
isPlainObject(floor) &&
!isNaN(floor.floor) &&
floor.currency === CURRENCY
) {
return floor.floor;
}
}
return null;
}
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER], // Supported ad types
// Validate bid request
isBidRequestValid: function (bid) {
return !!bid.params.placementId && !!bid.params.publisherId;
},
// Build OpenRTB requests using `ortbConverter`
buildRequests: function (validBidRequests, bidderRequest) {
const context = {
publisherId: validBidRequests.find(
(bidRequest) => bidRequest.params?.publisherId
)?.params.publisherId,
};
const ortbRequest = converter.toORTB({
bidRequests: validBidRequests,
bidderRequest,
context,
});
// Add GVLID and cookie ID to the request
ortbRequest.ext = ortbRequest.ext || {};
deepSetValue(ortbRequest, 'ext.gvlid', GVLID);
// Include user cookie if available
const ckid = storage.getDataFromLocalStorage('blueID') || storage.getCookie(COOKIE_NAME) || null;
if (ckid) {
deepSetValue(ortbRequest, 'user.ext.buyerid', ckid);
}
return {
method: 'POST',
url: ENDPOINT_URL,
data: JSON.stringify(ortbRequest),
options: {
contentType: 'text/plain',
},
};
},
// Interpret OpenRTB responses using `ortbConverter`
interpretResponse: function (serverResponse, request) {
const ortbResponse = serverResponse.body;
// Parse the OpenRTB response into Prebid bid responses
const prebidResponses = converter.fromORTB({
response: ortbResponse,
request: request.data,
}).bids;
// Example: Modify bid responses if needed
prebidResponses.forEach((bid) => {
bid.meta = bid.meta || {};
bid.meta.adapterVersion = '1.0.0';
});
return prebidResponses;
},
};
registerBidder(spec);