forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviouslyBidAdapter.js
240 lines (196 loc) · 6.84 KB
/
viouslyBidAdapter.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
import { deepAccess, logError, parseUrl, parseSizesInput, triggerPixel } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import {find} from '../src/polyfill.js';
const BIDDER_CODE = 'viously';
const GVLID = 1028;
const CURRENCY = 'EUR';
const TTL = 60;
const HTTP_METHOD = 'POST';
const REQUEST_URL = 'https://bidder.viously.com/bid';
const REQUIRED_VIDEO_PARAMS = ['context', 'playbackmethod', 'playerSize'];
const REQUIRED_VIOUSLY_PARAMS = ['pid'];
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER, 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) {
let videoParams = deepAccess(bid, 'mediaTypes.video');
let bannerParams = deepAccess(bid, 'mediaTypes.banner');
if (!bid.params) {
logError('The bid params are missing');
return false;
}
if (!bannerParams && !videoParams) {
logError('The placement must be of banner or video type');
return false;
}
/**
* BANNER checks
*/
if (bannerParams) {
let sizes = bannerParams.sizes;
if (!sizes || parseSizesInput(sizes).length == 0) {
logError('mediaTypes.banner.sizes must be set for banner placement at the right format.');
return false;
}
}
/**
* VIDEO checks
*/
let areParamsValid = true;
if (videoParams) {
REQUIRED_VIDEO_PARAMS.forEach(function(videoParam) {
if (typeof videoParams[videoParam] === 'undefined') {
logError('mediaTypes.video.' + videoParam + ' must be set for video placement.');
areParamsValid = false;
}
});
if (parseSizesInput(videoParams.playerSize).length === 0) {
logError('mediaTypes.video.playerSize must be set for video placement at the right format.');
areParamsValid = false;
}
}
/**
* Viously checks
*/
REQUIRED_VIOUSLY_PARAMS.forEach(function(viouslyParam) {
if (typeof bid.params[viouslyParam] === 'undefined') {
logError('The ' + viouslyParam + ' is missing.');
areParamsValid = false;
}
});
if (!areParamsValid) {
return false;
}
return true;
},
buildRequests: function(validBidRequests, bidderRequest) {
let payload = {};
/** Viously Publisher ID */
if (validBidRequests[0].params.pid) {
payload.pid = validBidRequests[0].params.pid;
}
// Referer Info
if (config.getConfig('pageUrl')) {
let parsedUrl = parseUrl(config.getConfig('pageUrl'));
payload.domain = parsedUrl.hostname;
payload.page_domain = config.getConfig('pageUrl');
} else if (bidderRequest && bidderRequest.refererInfo) {
let parsedUrl = parseUrl(bidderRequest.refererInfo.page);
payload.domain = parsedUrl.hostname;
payload.page_domain = bidderRequest.refererInfo.page;
}
if (payload.domain) {
/** Make sur that the scheme is not part of the domain */
payload.domain = payload.domain.replace(/(^\w+:|^)\/\//, '');
payload.domain = payload.domain.replace(/\/$/, '');
}
// Handle GDPR
if (bidderRequest && bidderRequest.gdprConsent) {
payload.gdpr = bidderRequest.gdprConsent.gdprApplies;
payload.gdpr_consent = bidderRequest.gdprConsent.consentString;
if (bidderRequest.gdprConsent.addtlConsent && bidderRequest.gdprConsent.addtlConsent.indexOf('~') !== -1) {
payload.addtl_consent = bidderRequest.gdprConsent.addtlConsent;
}
}
// US Privacy
if (bidderRequest && bidderRequest.uspConsent) {
payload.us_privacy = bidderRequest.uspConsent;
}
// Schain
if (validBidRequests[0].schain) {
payload.schain = validBidRequests[0].schain;
}
// Currency
payload.currency_code = CURRENCY;
// User IDs
if (validBidRequests[0].userIdAsEids) {
payload.users_uid = validBidRequests[0].userIdAsEids;
}
// Placements
payload.placements = validBidRequests.map(bidRequest => {
let request = {
id: bidRequest.adUnitCode,
bid_id: bidRequest.bidId
};
if (deepAccess(bidRequest, 'mediaTypes.banner')) {
let position = deepAccess(bidRequest, 'mediaTypes.banner.pos');
request.type = BANNER;
request.sizes = parseSizesInput(deepAccess(bidRequest, 'mediaTypes.banner.sizes'));
request.position = position || 0;
} else {
request.type = VIDEO;
request.video_params = {
context: deepAccess(bidRequest, 'mediaTypes.video.context'),
playbackmethod: deepAccess(bidRequest, 'mediaTypes.video.playbackmethod'),
size: parseSizesInput(deepAccess(bidRequest, 'mediaTypes.video.playerSize'))
};
}
return request;
});
return {
method: HTTP_METHOD,
url: validBidRequests[0].params.endpoint ? validBidRequests[0].params.endpoint : REQUEST_URL,
data: payload
};
},
interpretResponse: function(serverResponse, requests) {
const bidResponses = [];
const responseBody = serverResponse.body;
if (responseBody.ads && responseBody.ads.length > 0) {
responseBody.ads.forEach(function(bidResponse) {
if (bidResponse.bid) {
let bidRequest = find(requests.data.placements, bid => bid.bid_id === bidResponse.bid_id);
if (bidRequest) {
let sizes = bidResponse.size.split('x');
const bid = {
requestId: bidRequest.bid_id,
id: bidResponse.id,
cpm: bidResponse.cpm,
width: sizes[0],
height: sizes[1],
creativeId: bidResponse.creative_id || '',
currency: CURRENCY,
netRevenue: true,
ttl: TTL,
mediaType: bidResponse.type,
meta: {},
// Tracking data
nurl: bidResponse.nurl ? bidResponse.nurl : []
};
if (bidResponse.type == VIDEO) {
if (bidResponse.ad_url) {
bid.vastUrl = bidResponse.ad_url;
} else {
bid.vastXml = bidResponse.ad;
}
} else {
bid.ad = bidResponse.ad;
}
bidResponses.push(bid);
}
}
});
}
return bidResponses;
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {},
onTimeout: function(timeoutData) {},
onBidWon: function(bid) {
if (bid && bid.nurl && bid.nurl.length > 0) {
bid.nurl.forEach(function(winUrl) {
triggerPixel(winUrl, null);
});
}
},
onSetTargeting: function(bid) {}
};
registerBidder(spec);