forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediasquareBidAdapter.js
246 lines (235 loc) · 9.57 KB
/
mediasquareBidAdapter.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {ajax} from '../src/ajax.js';
import {config} from '../src/config.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
import {Renderer} from '../src/Renderer.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').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').SyncOptions} SyncOptions
* @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync
* @typedef {import('../src/adapters/bidderFactory.js').validBidRequests} validBidRequests
*/
const BIDDER_CODE = 'mediasquare';
const BIDDER_URL_PROD = 'https://pbs-front.mediasquare.fr/'
const BIDDER_URL_TEST = 'https://bidder-test.mediasquare.fr/'
const BIDDER_ENDPOINT_AUCTION = 'msq_prebid';
const BIDDER_ENDPOINT_WINNING = 'winning';
const OUTSTREAM_RENDERER_URL = 'https://acdn.adnxs.com/video/outstream/ANOutstreamVideo.js';
export const spec = {
code: BIDDER_CODE,
gvlid: 791,
aliases: ['msq'], // short code
supportedMediaTypes: [BANNER, NATIVE, VIDEO],
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
return !!(bid.params.owner && bid.params.code);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests, bidderRequest) {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
let codes = [];
let endpoint = document.location.search.match(/msq_test=true/) ? BIDDER_URL_TEST : BIDDER_URL_PROD;
let floor = {};
const test = config.getConfig('debug') ? 1 : 0;
let adunitValue = null;
Object.keys(validBidRequests).forEach(key => {
floor = {};
adunitValue = validBidRequests[key];
if (typeof adunitValue.getFloor === 'function') {
if (Array.isArray(adunitValue.sizes)) {
adunitValue.sizes.forEach(value => {
let tmpFloor = adunitValue.getFloor({currency: 'USD', mediaType: '*', size: value});
if (tmpFloor != {}) { floor[value.join('x')] = tmpFloor; }
});
}
let tmpFloor = adunitValue.getFloor({currency: 'USD', mediaType: '*', size: '*'});
if (tmpFloor != {}) { floor['*'] = tmpFloor; }
}
codes.push({
owner: adunitValue.params.owner,
code: adunitValue.params.code,
adunit: adunitValue.adUnitCode,
bidId: adunitValue.bidId,
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
auctionId: adunitValue.auctionId,
transactionId: adunitValue.ortb2Imp?.ext?.tid,
mediatypes: adunitValue.mediaTypes,
floor: floor
});
});
const payload = {
codes: codes,
// TODO: is 'page' the right value here?
referer: encodeURIComponent(bidderRequest.refererInfo.page || bidderRequest.refererInfo.topmostLocation),
pbjs: '$prebid.version$'
};
if (bidderRequest) { // modules informations (gdpr, ccpa, schain, userId)
if (bidderRequest.gdprConsent) {
payload.gdpr = {
consent_string: bidderRequest.gdprConsent.consentString,
consent_required: bidderRequest.gdprConsent.gdprApplies
};
}
if (bidderRequest.uspConsent) { payload.uspConsent = bidderRequest.uspConsent; }
if (bidderRequest.schain) { payload.schain = bidderRequest.schain; }
if (bidderRequest.userId) {
payload.userId = bidderRequest.userId;
} else if (bidderRequest.hasOwnProperty('bids') && typeof bidderRequest.bids == 'object' && bidderRequest.bids.length > 0 && bidderRequest.bids[0].hasOwnProperty('userId')) {
payload.userId = bidderRequest.bids[0].userId;
}
if (bidderRequest.ortb2?.regs?.ext?.dsa) { payload.dsa = bidderRequest.ortb2.regs.ext.dsa }
};
if (test) { payload.debug = true; }
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: endpoint + BIDDER_ENDPOINT_AUCTION,
data: payloadString,
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, bidRequest) {
const serverBody = serverResponse.body;
// const headerValue = serverResponse.headers.get('some-response-header');
const bidResponses = [];
let bidResponse = null;
let value = null;
if (serverBody.hasOwnProperty('responses')) {
Object.keys(serverBody['responses']).forEach(key => {
value = serverBody['responses'][key];
bidResponse = {
requestId: value['bid_id'],
cpm: value['cpm'],
width: value['width'],
height: value['height'],
creativeId: value['creative_id'],
currency: value['currency'],
netRevenue: value['net_revenue'],
ttl: value['ttl'],
ad: value['ad'],
mediasquare: {},
meta: {
'advertiserDomains': value['adomain']
}
};
if ('dsa' in value) { bidResponse.meta.dsa = value['dsa']; }
let paramsToSearchFor = ['bidder', 'code', 'match', 'hasConsent', 'context', 'increment', 'ova'];
paramsToSearchFor.forEach(param => {
if (param in value) {
bidResponse['mediasquare'][param] = value[param];
}
});
if ('native' in value) {
bidResponse['native'] = value['native'];
bidResponse['mediaType'] = 'native';
} else if ('video' in value) {
if ('url' in value['video']) { bidResponse['vastUrl'] = value['video']['url'] }
if ('xml' in value['video']) { bidResponse['vastXml'] = value['video']['xml'] }
bidResponse['mediaType'] = 'video';
bidResponse['renderer'] = createRenderer(value, OUTSTREAM_RENDERER_URL);
}
if (value.hasOwnProperty('deal_id')) { bidResponse['dealId'] = value['deal_id']; }
bidResponses.push(bidResponse);
});
}
return bidResponses;
},
/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {
if (typeof serverResponses === 'object' && serverResponses != null && serverResponses.length > 0 && serverResponses[0].hasOwnProperty('body') &&
serverResponses[0].body.hasOwnProperty('cookies') && typeof serverResponses[0].body.cookies === 'object') {
return serverResponses[0].body.cookies;
} else {
return [];
}
},
/**
* Register bidder specific code, which will execute if a bid from this bidder won the auction
* @param {Bid} The bid that won the auction
*/
onBidWon: function(bid) {
// fires a pixel to confirm a winning bid
if (bid.hasOwnProperty('mediaType') && bid.mediaType == 'video') {
return;
}
let params = { pbjs: '$prebid.version$', referer: encodeURIComponent(getRefererInfo().page || getRefererInfo().topmostLocation) };
let endpoint = document.location.search.match(/msq_test=true/) ? BIDDER_URL_TEST : BIDDER_URL_PROD;
let paramsToSearchFor = ['bidder', 'code', 'match', 'hasConsent', 'context', 'increment', 'ova'];
if (bid.hasOwnProperty('mediasquare')) {
paramsToSearchFor.forEach(param => {
if (bid['mediasquare'].hasOwnProperty(param)) {
params[param] = bid['mediasquare'][param];
if (typeof params[param] == 'number') {
params[param] = params[param].toString();
}
}
});
};
paramsToSearchFor = ['cpm', 'size', 'mediaType', 'currency', 'creativeId', 'adUnitCode', 'timeToRespond', 'requestId', 'auctionId', 'originalCpm', 'originalCurrency'];
paramsToSearchFor.forEach(param => {
if (bid.hasOwnProperty(param)) {
params[param] = bid[param];
if (typeof params[param] == 'number') {
params[param] = params[param].toString();
}
}
});
ajax(endpoint + BIDDER_ENDPOINT_WINNING, null, JSON.stringify(params), {method: 'POST', withCredentials: true});
return true;
}
}
function outstreamRender(bid) {
bid.renderer.push(() => {
window.ANOutstreamVideo.renderAd({
sizes: [bid.width, bid.height],
targetId: bid.adUnitCode,
adResponse: bid.adResponse,
rendererOptions: {
showBigPlayButton: false,
showProgressBar: 'bar',
showVolume: false,
allowFullscreen: true,
skippable: false,
content: bid.vastXml
}
});
});
}
function createRenderer(bid, url) {
const renderer = Renderer.install({
id: bid.bidId,
url: url,
loaded: false,
adUnitCode: bid.adUnitCode,
targetId: bid.adUnitCode
});
renderer.setRender(outstreamRender);
return renderer;
}
registerBidder(spec);