-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
viantOrtbBidAdapter.js
139 lines (125 loc) · 4.34 KB
/
viantOrtbBidAdapter.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
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import * as utils from '../src/utils.js';
import {ortbConverter} from '../libraries/ortbConverter/converter.js'
import {deepAccess, getBidIdParameter, logError} from '../src/utils.js';
const BIDDER_CODE = 'viant';
const ENDPOINT = 'https://bidders-us-east-1.adelphic.net/d/rtb/v25/prebid/bidder'
const ADAPTER_VERSION = '2.0.0';
const DEFAULT_BID_TTL = 300;
const DEFAULT_CURRENCY = 'USD';
const DEFAULT_NET_REVENUE = true;
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
isBidRequestValid: function (bid) {
if (bid && typeof bid.params !== 'object') {
logError(BIDDER_CODE + ': params is not defined or is incorrect in the bidder settings.');
return false;
}
if (!getBidIdParameter('publisherId', bid.params)) {
logError(BIDDER_CODE + ': publisherId is not present in bidder params.');
return false;
}
const mediaTypesBanner = deepAccess(bid, 'mediaTypes.banner');
const mediaTypesVideo = deepAccess(bid, 'mediaTypes.video');
const mediaTypesNative = deepAccess(bid, 'mediaTypes.native');
if (!mediaTypesBanner && !mediaTypesVideo && !mediaTypesNative) {
utils.logWarn(BIDDER_CODE + ': one of mediaTypes.banner or mediaTypes.video or mediaTypes.native must be passed');
return false;
}
return true;
},
buildRequests,
interpretResponse(response, request) {
if (!response.body) {
response.body = {nbr: 0};
}
const bids = converter.fromORTB({request: request.data, response: response.body}).bids;
return bids;
},
/**
* Register bidder specific code, which will execute if a bid from this bidder won the auction
* @param {Bid} bid The bid that won the auction
*/
onBidWon: function (bid) {
if (bid.burl) {
utils.triggerPixel(bid.burl);
utils.triggerPixel(utils.replaceAuctionPrice(bid.burl, bid.originalCpm || bid.cpm));
} else if (bid.nurl) {
utils.triggerPixel(bid.nurl);
utils.triggerPixel(utils.replaceAuctionPrice(bid.nurl, bid.originalCpm || bid.cpm));
}
}
}
function buildRequests(bids, bidderRequest) {
let videoBids = bids.filter(bid => isVideoBid(bid));
let bannerBids = bids.filter(bid => isBannerBid(bid));
let nativeBids = bids.filter(bid => isNativeBid(bid));
let requests = bannerBids.length ? [createRequest(bannerBids, bidderRequest, BANNER)] : [];
videoBids.forEach(bid => {
requests.push(createRequest([bid], bidderRequest, VIDEO));
});
nativeBids.forEach(bid => {
requests.push(createRequest([bid], bidderRequest, NATIVE));
});
return requests;
}
function createRequest(bidRequests, bidderRequest, mediaType) {
const data = converter.toORTB({bidRequests, bidderRequest, context: {mediaType}});
if (bidderRequest.gdprConsent && typeof bidderRequest.gdprConsent.gdprApplies === 'boolean') {
if (!data.regs) data.regs = {};
if (!data.regs.ext) data.regs.ext = {};
data.regs.ext.gdpr = bidderRequest.gdprConsent.gdprApplies ? 1 : 0;
}
if (bidderRequest.uspConsent) {
if (!data.regs) data.regs = {};
if (!data.regs.ext) data.regs.ext = {};
data.regs.ext.us_privacy = bidderRequest.uspConsent;
}
let imp = data.imp || [];
let dealsMap = new Map();
let privateAuctions = new Map();
if (bidderRequest.bids) {
bidderRequest.bids.forEach(bid => {
if (bid.params && bid.params.pmp) {
dealsMap.set(bid.bidId, bid.params.pmp);
privateAuctions.set(bid.bidId, bid.params.private_auction || 0);
}
});
}
imp.forEach((element) => {
let deals = dealsMap.get(element.id);
if (deals) {
element.pmp = deals;
}
element.pmp = element.pmp || {};
element.pmp.private_auction = privateAuctions.get(element.id);
});
data.ext = data.ext || {};
data.ext.viant = {
adapterVersion: ADAPTER_VERSION
};
return {
method: 'POST',
url: ENDPOINT,
data: data
}
}
function isVideoBid(bid) {
return deepAccess(bid, 'mediaTypes.video');
}
function isBannerBid(bid) {
return deepAccess(bid, 'mediaTypes.banner');
}
function isNativeBid(bid) {
return deepAccess(bid, 'mediaTypes.native');
}
const converter = ortbConverter({
context: {
netRevenue: DEFAULT_NET_REVENUE,
ttl: DEFAULT_BID_TTL,
currency: DEFAULT_CURRENCY
}
});
registerBidder(spec);