forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreenbidsBidAdapter.js
238 lines (221 loc) · 8.37 KB
/
greenbidsBidAdapter.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { getValue, logError, deepAccess, parseSizesInput, getBidIdParameter, logInfo } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';
import { getHLen } from '../libraries/navigatorData/navigatorData.js';
import { getTimeToFirstByte } from '../libraries/timeToFirstBytesUtils/timeToFirstBytesUtils.js';
import { getReferrerInfo, getPageTitle, getPageDescription, getConnectionDownLink } from '../libraries/pageInfosUtils/pageInfosUtils.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
*/
const BIDDER_CODE = 'greenbids';
const GVL_ID = 1232;
const ENDPOINT_URL = 'https://hb.greenbids.ai';
export const storage = getStorageManager({ bidderCode: BIDDER_CODE });
export const spec = {
code: BIDDER_CODE,
gvlid: GVL_ID,
supportedMediaTypes: ['banner', 'video'],
/**
* 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 (typeof bid.params !== 'undefined' && parseInt(getValue(bid.params, 'placementId')) > 0) {
logInfo('Greenbids bidder adapter valid bid request');
return true;
} else {
logError('Greenbids bidder adapter requires placementId to be defined and a positive number');
return false;
}
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} validBidRequests array of bids
* @param bidderRequest bidder request object
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
const bids = validBidRequests.map(bids => {
const reqObj = {};
let placementId = getValue(bids.params, 'placementId');
const gpid = deepAccess(bids, 'ortb2Imp.ext.gpid');
reqObj.sizes = getSizes(bids);
reqObj.bidId = getBidIdParameter('bidId', bids);
reqObj.bidderRequestId = getBidIdParameter('bidderRequestId', bids);
reqObj.placementId = parseInt(placementId, 10);
reqObj.adUnitCode = getBidIdParameter('adUnitCode', bids);
reqObj.transactionId = bids.ortb2Imp?.ext?.tid || '';
if (gpid) { reqObj.gpid = gpid; }
});
const topWindow = window.top;
const payload = {
referrer: getReferrerInfo(bidderRequest),
pageReferrer: document.referrer,
pageTitle: getPageTitle().slice(0, 300),
pageDescription: getPageDescription().slice(0, 300),
networkBandwidth: getConnectionDownLink(window.navigator),
timeToFirstByte: getTimeToFirstByte(window),
data: bids,
device: bidderRequest?.ortb2?.device || {},
deviceWidth: screen.width,
deviceHeight: screen.height,
devicePixelRatio: topWindow.devicePixelRatio,
screenOrientation: screen.orientation?.type,
historyLength: getHLen(),
viewportHeight: topWindow.visualViewport?.height,
viewportWidth: topWindow.visualViewport?.width,
prebid_version: '$prebid.version$',
};
const firstBidRequest = validBidRequests[0];
if (firstBidRequest.schain) {
payload.schain = firstBidRequest.schain;
}
hydratePayloadWithGppConsentData(payload, bidderRequest.gppConsent);
hydratePayloadWithGdprConsentData(payload, bidderRequest.gdprConsent);
hydratePayloadWithUspConsentData(payload, bidderRequest.uspConsent);
const userAgentClientHints = deepAccess(firstBidRequest, 'ortb2.device.sua');
if (userAgentClientHints) {
payload.userAgentClientHints = userAgentClientHints;
}
const dsa = deepAccess(bidderRequest, 'ortb2.regs.ext.dsa');
if (dsa) {
payload.dsa = dsa;
}
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: ENDPOINT_URL,
data: payloadString,
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server response.
*/
interpretResponse: function (serverResponse) {
serverResponse = serverResponse.body;
if (!serverResponse.responses) {
return [];
}
return serverResponse.responses.map((bid) => {
const bidResponse = {
cpm: bid.cpm,
width: bid.width,
height: bid.height,
currency: bid.currency,
netRevenue: true,
size: bid.size,
ttl: bid.ttl,
meta: {
advertiserDomains: bid && bid.adomain ? bid.adomain : [],
},
ad: bid.ad,
requestId: bid.bidId,
creativeId: bid.creativeId,
placementId: bid.placementId,
};
if (bid.dealId) {
bidResponse.dealId = bid.dealId
}
if (bid?.ext?.dsa) {
bidResponse.meta.dsa = bid.ext.dsa;
}
return bidResponse;
});
}
};
registerBidder(spec);
/**
* Converts the sizes from the bid object to the required format.
*
* @param {Object} bid - The bid object containing size information.
* @param {Array} bid.sizes - The sizes array from the bid object.
* @returns {Array} - The parsed sizes in the required format.
*/
function getSizes(bid) {
return parseSizesInput(bid.sizes);
}
// Privacy handling
/**
* Hydrates the given payload with GPP consent data if available.
*
* @param {Object} payload - The payload object to be hydrated.
* @param {Object} gppData - The GPP consent data object.
* @param {string} gppData.gppString - The GPP consent string.
* @param {number[]} gppData.applicableSections - An array of applicable section IDs.
*/
function hydratePayloadWithGppConsentData(payload, gppData) {
if (!gppData) { return; }
let isValidConsentString = typeof gppData.gppString === 'string';
let validateApplicableSections =
Array.isArray(gppData.applicableSections) &&
gppData.applicableSections.every((section) => typeof (section) === 'number')
payload.gpp = {
consentString: isValidConsentString ? gppData.gppString : '',
applicableSectionIds: validateApplicableSections ? gppData.applicableSections : [],
};
}
/**
* Hydrates the given payload with GDPR consent data if available.
*
* @param {Object} payload - The payload object to be hydrated with GDPR consent data.
* @param {Object} gdprData - The GDPR data object containing consent information.
* @param {boolean} gdprData.gdprApplies - Indicates if GDPR applies.
* @param {string} gdprData.consentString - The GDPR consent string.
* @param {number} gdprData.apiVersion - The version of the GDPR API being used.
* @param {Object} gdprData.vendorData - Additional vendor data related to GDPR.
*/
function hydratePayloadWithGdprConsentData(payload, gdprData) {
if (!gdprData) { return; }
let isCmp = typeof gdprData.gdprApplies === 'boolean';
let isConsentString = typeof gdprData.consentString === 'string';
let status = isCmp
? findGdprStatus(gdprData.gdprApplies, gdprData.vendorData)
: gdprStatus.CMP_NOT_FOUND_OR_ERROR;
payload.gdpr_iab = {
consent: isConsentString ? gdprData.consentString : '',
status: status,
apiVersion: gdprData.apiVersion
};
}
/**
* Adds USP (CCPA) consent data to the payload if available.
*
* @param {Object} payload - The payload object to be hydrated with USP consent data.
* @param {string} uspConsentData - The USP consent string to be added to the payload.
*/
function hydratePayloadWithUspConsentData(payload, uspConsentData) {
if (!uspConsentData) { return; }
payload.us_privacy = uspConsentData;
}
const gdprStatus = {
GDPR_APPLIES_PUBLISHER: 12,
GDPR_APPLIES_GLOBAL: 11,
GDPR_DOESNT_APPLY: 0,
CMP_NOT_FOUND_OR_ERROR: 22
};
/**
* Determines the GDPR status based on whether GDPR applies and the provided GDPR data.
*
* @param {boolean} gdprApplies - Indicates if GDPR applies.
* @param {Object} gdprData - The GDPR data object.
* @param {boolean} gdprData.isServiceSpecific - Indicates if the GDPR data is service-specific.
* @returns {string} The GDPR status.
*/
function findGdprStatus(gdprApplies, gdprData) {
let status = gdprStatus.GDPR_APPLIES_PUBLISHER;
if (gdprApplies) {
if (gdprData && !gdprData.isServiceSpecific) {
status = gdprStatus.GDPR_APPLIES_GLOBAL;
}
} else {
status = gdprStatus.GDPR_DOESNT_APPLY;
}
return status;
}