forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelayBidAdapter.js
99 lines (91 loc) · 3.17 KB
/
relayBidAdapter.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
import { isNumber, logMessage } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { BANNER, VIDEO, NATIVE } from '../src/mediaTypes.js';
import { ortbConverter } from '../libraries/ortbConverter/converter.js'
const BIDDER_CODE = 'relay';
const METHOD = 'POST';
const ENDPOINT_URL = 'https://e.relay.bid/p/openrtb2';
// The default impl from the prebid docs.
const CONVERTER =
ortbConverter({
context: {
netRevenue: true,
ttl: 30
}
});
function buildRequests(bidRequests, bidderRequest) {
const prebidVersion = config.getConfig('prebid_version') || 'v8.1.0';
// Group bids by accountId param
const groupedByAccountId = bidRequests.reduce((accu, item) => {
const accountId = ((item || {}).params || {}).accountId;
if (!accu[accountId]) { accu[accountId] = []; };
accu[accountId].push(item);
return accu;
}, {});
// Send one overall request with all grouped bids per accountId
let reqs = [];
for (const [accountId, accountBidRequests] of Object.entries(groupedByAccountId)) {
const url = `${ENDPOINT_URL}?a=${accountId}&pb=1&pbv=${prebidVersion}`;
const data = CONVERTER.toORTB({ bidRequests: accountBidRequests, bidderRequest })
const req = {
method: METHOD,
url,
data
};
reqs.push(req);
}
return reqs;
};
function interpretResponse(response, request) {
return CONVERTER.fromORTB({ response: response.body, request: request.data }).bids;
};
function isBidRequestValid(bid) {
return isNumber((bid.params || {}).accountId);
};
function getUserSyncs(syncOptions, serverResponses, gdprConsent, uspConsent) {
let syncs = []
for (const response of serverResponses) {
const responseSyncs = ((((response || {}).body || {}).ext || {}).user_syncs || [])
// Relay returns user_syncs in the format expected by prebid. If for any
// reason the request/response failed to properly capture the GDPR settings
// -- fallback to those identified by Prebid.
for (const sync of responseSyncs) {
const syncUrl = new URL(sync.url);
const missingGdpr = !syncUrl.searchParams.has('gdpr');
const missingGdprConsent = !syncUrl.searchParams.has('gdpr_consent');
if (missingGdpr) {
syncUrl.searchParams.set('gdpr', Number(gdprConsent.gdprApplies))
sync.url = syncUrl.toString();
}
if (missingGdprConsent) {
syncUrl.searchParams.set('gdpr_consent', gdprConsent.consentString);
sync.url = syncUrl.toString();
}
if (syncOptions.iframeEnabled && sync.type === 'iframe') {
syncs.push(sync);
} else if (syncOptions.pixelEnabled && sync.type === 'image') {
syncs.push(sync);
}
}
}
return syncs;
}
export const spec = {
code: BIDDER_CODE,
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs,
onTimeout: function (timeoutData) {
logMessage('Timeout: ', timeoutData);
},
onBidWon: function (bid) {
logMessage('Bid won: ', bid);
},
onBidderError: function ({ error, bidderRequest }) {
logMessage('Error: ', error, bidderRequest);
},
supportedMediaTypes: [BANNER, VIDEO, NATIVE]
}
registerBidder(spec);