forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridBidAdapter.js
186 lines (157 loc) · 5.52 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
import {_each, deepAccess, getDefinedParams, parseGPTSingleSizeArrayToRtbSize} from '../src/utils.js';
import {VIDEO} from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getAd, getSiteObj, getSyncResponse} from '../libraries/targetVideoUtils/bidderUtils.js'
import {GVLID, SOURCE, TIME_TO_LIVE, VIDEO_ENDPOINT_URL, VIDEO_PARAMS} from '../libraries/targetVideoUtils/constants.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
*/
export const spec = {
code: 'brid',
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: VIDEO_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;
let highestBid = null;
if (response && response.seatbid && response.seatbid.length && response.seatbid[0].bid && response.seatbid[0].bid.length) {
_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.crid || 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;
};
if (!highestBid || highestBid.cpm < bidResponse.cpm) {
highestBid = bidResponse;
}
});
});
}
return highestBid ? [highestBid] : [];
},
/**
* Determine the user sync type (either 'iframe' or 'image') based on syncOptions.
* Construct the sync URL by appending required query parameters such as gdpr, ccpa, and coppa consents.
* Return an array containing an object with the sync type and the constructed URL.
*/
getUserSyncs: (syncOptions, serverResponses, gdprConsent, uspConsent, gppConsent) => {
return getSyncResponse(syncOptions, gdprConsent, uspConsent, gppConsent, 'brid');
}
}
registerBidder(spec);