-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
bridBidAdapter.js
229 lines (194 loc) · 5.83 KB
/
bridBidAdapter.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
import {createTrackPixelHtml, _each, deepAccess, getDefinedParams, parseGPTSingleSizeArrayToRtbSize} from '../src/utils.js';
import {VIDEO} from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getRefererInfo} from '../src/refererDetection.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').BidderRequest} BidderRequest
*/
const SOURCE = 'pbjs';
const BIDDER_CODE = 'brid';
const ENDPOINT_URL = 'https://pbs.prebrid.tv/openrtb2/auction';
const GVLID = 934;
const TIME_TO_LIVE = 300;
const VIDEO_PARAMS = [
'api', 'linearity', 'maxduration', 'mimes', 'minduration', 'plcmt',
'playbackmethod', 'protocols', 'startdelay'
];
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [VIDEO],
/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
return !!(bid && bid.params && bid.params.placementId);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @param {BidderRequest} bidderRequest bidder request object.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(bidRequests, bidderRequest) {
const requests = [];
_each(bidRequests, function(bid) {
const placementId = bid.params.placementId;
const bidId = bid.bidId;
let sizes = bid.sizes;
if (sizes && !Array.isArray(sizes[0])) sizes = [sizes];
const site = getSiteObj();
const postBody = {
sdk: {
source: SOURCE,
version: '$prebid.version$'
},
id: bidderRequest.bidderRequestId,
site,
imp: []
};
const imp = {
ext: {
prebid: {
storedrequest: {'id': placementId}
}
}
};
const video = deepAccess(bid, 'mediaTypes.video');
if (video) {
imp.video = getDefinedParams(video, VIDEO_PARAMS);
if (video.playerSize) {
imp.video = Object.assign(
imp.video, parseGPTSingleSizeArrayToRtbSize(video.playerSize[0]) || {}
);
} else if (video.w && video.h) {
imp.video.w = video.w;
imp.video.h = video.h;
};
};
postBody.imp.push(imp);
const gdprConsent = bidderRequest && bidderRequest.gdprConsent;
const uspConsent = bidderRequest && bidderRequest.uspConsent;
if (gdprConsent || uspConsent) {
postBody.regs = { ext: {} };
if (uspConsent) {
postBody.regs.ext.us_privacy = uspConsent;
};
if (gdprConsent) {
if (typeof gdprConsent.gdprApplies !== 'undefined') {
postBody.regs.ext.gdpr = gdprConsent.gdprApplies ? 1 : 0;
};
if (typeof gdprConsent.consentString !== 'undefined') {
postBody.user = {
ext: { consent: gdprConsent.consentString }
};
};
};
};
if (bidRequests[0].schain) {
postBody.schain = bidRequests[0].schain;
}
const params = bid.params;
requests.push({
method: 'POST',
url: ENDPOINT_URL,
data: JSON.stringify(postBody),
options: {
withCredentials: true
},
bidId,
params
});
});
return requests;
},
/**
* 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.
*/
interpretResponse: function(serverResponse, bidRequest) {
const response = serverResponse.body;
const bidResponses = [];
_each(response.seatbid, (resp) => {
_each(resp.bid, (bid) => {
const requestId = bidRequest.bidId;
const params = bidRequest.params;
const {ad, adUrl, vastUrl, vastXml} = getAd(bid);
const bidResponse = {
requestId,
params,
cpm: bid.price,
width: bid.w,
height: bid.h,
creativeId: bid.adid,
currency: response.cur,
netRevenue: false,
ttl: TIME_TO_LIVE,
meta: {
advertiserDomains: bid.adomain || []
}
};
if (vastUrl || vastXml) {
bidResponse.mediaType = VIDEO;
if (vastUrl) bidResponse.vastUrl = vastUrl;
if (vastXml) bidResponse.vastXml = vastXml;
} else {
bidResponse.ad = ad;
bidResponse.adUrl = adUrl;
};
bidResponses.push(bidResponse);
});
});
return bidResponses;
},
}
/**
* Helper function to get ad
*
* @param {object} bid The bid.
* @return {object} ad object.
*/
function getAd(bid) {
let ad, adUrl, vastXml, vastUrl;
switch (deepAccess(bid, 'ext.prebid.type')) {
case VIDEO:
if (bid.adm.substr(0, 4) === 'http') {
vastUrl = bid.adm;
} else {
vastXml = bid.adm;
};
break;
default:
if (bid.adm && bid.nurl) {
ad = bid.adm;
ad += createTrackPixelHtml(decodeURIComponent(bid.nurl));
} else if (bid.adm) {
ad = bid.adm;
} else if (bid.nurl) {
adUrl = bid.nurl;
};
}
return {ad, adUrl, vastXml, vastUrl};
}
/**
* Helper function to get site object
*
* @return {object} siteObj.
*/
function getSiteObj() {
const refInfo = (getRefererInfo && getRefererInfo()) || {};
return {
page: refInfo.page,
ref: refInfo.ref,
domain: refInfo.domain
};
}
registerBidder(spec);