-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathbraveBidAdapter.js
120 lines (97 loc) · 3.93 KB
/
braveBidAdapter.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
import { isEmpty, parseUrl, isStr, triggerPixel } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { config } from '../src/config.js';
import { createNativeRequest, createBannerRequest, createVideoRequest, parseNative } from '../libraries/braveUtils/index.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
*/
const BIDDER_CODE = 'brave';
const DEFAULT_CUR = 'USD';
const ENDPOINT_URL = `https://point.bravegroup.tv/?t=2&partner=hash`;
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
isBidRequestValid: (bid) => {
return !!(bid.params.placementId && bid.params.placementId.toString().length === 32);
},
buildRequests: (validBidRequests, bidderRequest) => {
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
if (!validBidRequests.length || !bidderRequest) return [];
const endpointURL = ENDPOINT_URL.replace('hash', validBidRequests[0].params.placementId);
let imp = validBidRequests.map((br) => {
let impObject = { id: br.bidId, secure: 1 };
if (br.mediaTypes.banner) impObject.banner = createBannerRequest(br);
else if (br.mediaTypes.video) impObject.video = createVideoRequest(br);
else if (br.mediaTypes.native) impObject.native = { id: br.transactionId, ver: '1.2', request: createNativeRequest(br) };
return impObject;
});
let page = bidderRequest.refererInfo.page || bidderRequest.refererInfo.topmostLocation;
let data = {
id: bidderRequest.bidderRequestId,
cur: [DEFAULT_CUR],
device: { w: screen.width, h: screen.height, language: navigator.language?.split('-')[0], ua: navigator.userAgent },
site: { domain: parseUrl(page).hostname, page: page },
tmax: bidderRequest.timeout,
imp,
};
if (bidderRequest.refererInfo.ref) data.site.ref = bidderRequest.refererInfo.ref;
if (bidderRequest.gdprConsent) {
data['regs'] = {'ext': {'gdpr': bidderRequest.gdprConsent.gdprApplies ? 1 : 0}};
data['user'] = {'ext': {'consent': bidderRequest.gdprConsent.consentString ? bidderRequest.gdprConsent.consentString : ''}};
}
if (bidderRequest.uspConsent !== undefined) {
if (!data['regs'])data['regs'] = {'ext': {}};
data['regs']['ext']['us_privacy'] = bidderRequest.uspConsent;
}
if (config.getConfig('coppa') === true) {
if (!data['regs'])data['regs'] = {'coppa': 1};
else data['regs']['coppa'] = 1;
}
if (validBidRequests[0].schain) {
data['source'] = {'ext': {'schain': validBidRequests[0].schain}};
}
return { method: 'POST', url: endpointURL, data: data };
},
interpretResponse: (serverResponse) => {
if (!serverResponse || isEmpty(serverResponse.body)) return [];
let bids = [];
serverResponse.body.seatbid.forEach(response => {
response.bid.forEach(bid => {
let mediaType = bid.ext && bid.ext.mediaType ? bid.ext.mediaType : 'banner';
let bidObj = {
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
ttl: 1200,
currency: DEFAULT_CUR,
netRevenue: true,
creativeId: bid.crid,
dealId: bid.dealid || null,
mediaType: mediaType
};
switch (mediaType) {
case 'video':
bidObj.vastUrl = bid.adm;
break;
case 'native':
bidObj.native = parseNative(bid.adm);
break;
default:
bidObj.ad = bid.adm;
}
bids.push(bidObj);
});
});
return bids;
},
onBidWon: (bid) => {
if (isStr(bid.nurl) && bid.nurl !== '') {
triggerPixel(bid.nurl);
}
}
};
registerBidder(spec);