forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
relevatehealthBidAdapter.js
161 lines (159 loc) · 4.64 KB
/
relevatehealthBidAdapter.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
import {
registerBidder
} from '../src/adapters/bidderFactory.js';
import {
BANNER
} from '../src/mediaTypes.js';
import {
deepAccess,
generateUUID,
isArray,
logError
} from '../src/utils.js';
const BIDDER_CODE = 'relevatehealth';
const ENDPOINT_URL = 'https://rtb.relevate.health/prebid/relevate';
function buildRequests(bidRequests, bidderRequest) {
const requests = [];
// Loop through each bid request
bidRequests.forEach(bid => {
// Construct the bid request object
const request = {
id: generateUUID(),
placementId: bid.params.placement_id,
imp: [{
id: bid.bidId,
banner: getBanner(bid),
bidfloor: getFloor(bid)
}],
site: getSite(bidderRequest),
user: buildUser(bid)
};
// Get uspConsent from bidderRequest
if (bidderRequest && bidderRequest.uspConsent) {
request.us_privacy = bidderRequest.uspConsent;
}
// Get GPP Consent from bidderRequest
if (bidderRequest?.gppConsent?.gppString) {
request.gpp = bidderRequest.gppConsent.gppString;
request.gpp_sid = bidderRequest.gppConsent.applicableSections;
} else if (bidderRequest?.ortb2?.regs?.gpp) {
request.gpp = bidderRequest.ortb2.regs.gpp;
request.gpp_sid = bidderRequest.ortb2.regs.gpp_sid;
}
// Get coppa compliance from bidderRequest
if (bidderRequest?.ortb2?.regs?.coppa) {
request.coppa = 1;
}
// Push the constructed bid request to the requests array
requests.push(request);
});
// Return the array of bid requests
return {
method: 'POST',
url: ENDPOINT_URL,
data: JSON.stringify(requests),
options: {
contentType: 'application/json',
}
};
}
// Format the response as per the standards
function interpretResponse(bidResponse, bidRequest) {
let resp = [];
if (bidResponse && bidResponse.body) {
try {
let bids = bidResponse.body.seatbid && bidResponse.body.seatbid[0] ? bidResponse.body.seatbid[0].bid : [];
if (bids) {
bids.forEach(bidObj => {
let newBid = formatResponse(bidObj);
newBid.mediaType = BANNER;
resp.push(newBid);
});
}
} catch (err) {
logError(err);
}
}
return resp;
}
// Function to check if Bid is valid
function isBidRequestValid(bid) {
return !!(bid.params.placement_id && bid.params.user_id);
}
// Function to get banner details
function getBanner(bid) {
if (deepAccess(bid, 'mediaTypes.banner')) {
// Fetch width and height from MediaTypes object, if not provided in bid params
if (deepAccess(bid, 'mediaTypes.banner.sizes') && !bid.params.height && !bid.params.width) {
let sizes = deepAccess(bid, 'mediaTypes.banner.sizes');
if (isArray(sizes) && sizes.length > 0) {
return {
h: sizes[0][1],
w: sizes[0][0]
};
}
} else {
return {
h: bid.params.height,
w: bid.params.width
};
}
}
}
// Function to get bid_floor
function getFloor(bid) {
if (bid.params && bid.params.bid_floor) {
return bid.params.bid_floor;
} else {
return 0;
}
}
// Function to get site details
function getSite(bidderRequest) {
let site = {};
if (bidderRequest && bidderRequest.refererInfo && bidderRequest.refererInfo.page) {
site.name = bidderRequest.refererInfo.domain;
} else {
site.name = '';
}
return site;
}
// Function to format response
function formatResponse(bid) {
return {
requestId: bid && bid.impid ? bid.impid : undefined,
cpm: bid && bid.price ? bid.price : 0.0,
width: bid && bid.w ? bid.w : 0,
height: bid && bid.h ? bid.h : 0,
ad: bid && bid.adm ? bid.adm : '',
meta: {
advertiserDomains: bid && bid.adomain ? bid.adomain : []
},
creativeId: bid && bid.crid ? bid.crid : undefined,
netRevenue: false,
currency: bid && bid.cur ? bid.cur : 'USD',
ttl: 300,
dealId: bid && bid.dealId ? bid.dealId : undefined
};
}
// Function to build the user object
function buildUser(bid) {
if (bid && bid.params) {
return {
id: bid.params.user_id && typeof bid.params.user_id == 'string' ? bid.params.user_id : '',
// TODO: commented out because of rule violations
buyeruid: '', // localStorage.getItem('adx_profile_guid') ? localStorage.getItem('adx_profile_guid') : '',
keywords: bid.params.keywords && typeof bid.params.keywords == 'string' ? bid.params.keywords : '',
customdata: bid.params.customdata && typeof bid.params.customdata == 'string' ? bid.params.customdata : ''
};
}
}
// Export const spec
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: BANNER,
isBidRequestValid,
buildRequests,
interpretResponse
}
registerBidder(spec);