forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhythmoneBidAdapter.js
266 lines (237 loc) · 7.71 KB
/
rhythmoneBidAdapter.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'use strict';
import { deepAccess, getDNT, parseSizesInput, isArray } from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
function RhythmOneBidAdapter() {
this.code = 'rhythmone';
this.supportedMediaTypes = [VIDEO, BANNER];
this.gvlid = 36;
let SUPPORTED_VIDEO_PROTOCOLS = [2, 3, 5, 6];
let SUPPORTED_VIDEO_MIMES = ['video/mp4'];
let SUPPORTED_VIDEO_PLAYBACK_METHODS = [1, 2, 3, 4];
let SUPPORTED_VIDEO_DELIVERY = [1];
let SUPPORTED_VIDEO_API = [1, 2, 5];
let slotsToBids = {};
let version = '2.1';
this.isBidRequestValid = function (bid) {
return !!(bid.params && bid.params.placementId);
};
this.getUserSyncs = function (syncOptions, responses, gdprConsent) {
return [];
};
function frameImp(BRs, bidderRequest) {
var impList = [];
var isSecure = 0;
if (bidderRequest && bidderRequest.refererInfo && bidderRequest.refererInfo.stack.length) {
// clever trick to get the protocol
var el = document.createElement('a');
el.href = bidderRequest.refererInfo.stack[0];
isSecure = (el.protocol == 'https:') ? 1 : 0;
}
for (var i = 0; i < BRs.length; i++) {
slotsToBids[BRs[i].adUnitCode] = BRs[i];
var impObj = {};
impObj.id = BRs[i].adUnitCode;
impObj.bidfloor = 0;
impObj.secure = isSecure;
if (deepAccess(BRs[i], 'mediaTypes.banner') || deepAccess(BRs[i], 'mediaType') === 'banner') {
let banner = frameBanner(BRs[i]);
if (banner) {
impObj.banner = banner;
}
}
if (deepAccess(BRs[i], 'mediaTypes.video') || deepAccess(BRs[i], 'mediaType') === 'video') {
impObj.video = frameVideo(BRs[i]);
}
if (!(impObj.banner || impObj.video)) {
continue;
}
impObj.ext = frameExt(BRs[i]);
impList.push(impObj);
}
return impList;
}
function frameSite(bidderRequest) {
return {
domain: bidderRequest?.refererInfo?.domain || '',
page: bidderRequest?.refererInfo?.page || '',
ref: bidderRequest?.refererInfo?.ref || ''
}
}
function frameDevice() {
return {
ua: navigator.userAgent,
ip: '', // Empty Ip string is required, server gets the ip from HTTP header
dnt: getDNT() ? 1 : 0,
}
}
function getValidSizeSet(dimensionList) {
let w = parseInt(dimensionList[0]);
let h = parseInt(dimensionList[1]);
// clever check for NaN
if (! (w !== w || h !== h)) { // eslint-disable-line
return [w, h];
}
return false;
}
function frameBanner(adUnit) {
// adUnit.sizes is scheduled to be deprecated, continue its support but prefer adUnit.mediaTypes.banner
var sizeList = adUnit.sizes;
if (adUnit.mediaTypes && adUnit.mediaTypes.banner) {
sizeList = adUnit.mediaTypes.banner.sizes;
}
var sizeStringList = parseSizesInput(sizeList);
var format = [];
sizeStringList.forEach(function(size) {
if (size) {
var dimensionList = getValidSizeSet(size.split('x'));
if (dimensionList) {
format.push({
'w': dimensionList[0],
'h': dimensionList[1],
});
}
}
});
if (format.length) {
return {
'format': format
};
}
return false;
}
function frameVideo(bid) {
var size = [];
if (deepAccess(bid, 'mediaTypes.video.playerSize')) {
var dimensionSet = bid.mediaTypes.video.playerSize;
if (isArray(bid.mediaTypes.video.playerSize[0])) {
dimensionSet = bid.mediaTypes.video.playerSize[0];
}
var validSize = getValidSizeSet(dimensionSet)
if (validSize) {
size = validSize;
}
}
return {
mimes: deepAccess(bid, 'mediaTypes.video.mimes') || SUPPORTED_VIDEO_MIMES,
protocols: deepAccess(bid, 'mediaTypes.video.protocols') || SUPPORTED_VIDEO_PROTOCOLS,
w: size[0],
h: size[1],
startdelay: deepAccess(bid, 'mediaTypes.video.startdelay') || 0,
skip: deepAccess(bid, 'mediaTypes.video.skip') || 0,
playbackmethod: deepAccess(bid, 'mediaTypes.video.playbackmethod') || SUPPORTED_VIDEO_PLAYBACK_METHODS,
delivery: deepAccess(bid, 'mediaTypes.video.delivery') || SUPPORTED_VIDEO_DELIVERY,
api: deepAccess(bid, 'mediaTypes.video.api') || SUPPORTED_VIDEO_API,
}
}
function frameExt(bid) {
return {
bidder: {
placementId: bid.params['placementId'],
zone: (bid.params && bid.params['zone']) ? bid.params['zone'] : '1r',
path: (bid.params && bid.params['path']) ? bid.params['path'] : 'mvo'
}
}
}
function frameBid(BRs, bidderRequest) {
let bid = {
id: BRs[0].bidderRequestId,
imp: frameImp(BRs, bidderRequest),
site: frameSite(bidderRequest),
device: frameDevice(),
user: {
ext: {
consent: deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? bidderRequest.gdprConsent.consentString : ''
}
},
at: 1,
tmax: Math.min(1000, bidderRequest.timeout),
regs: {
ext: {
gdpr: deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? Boolean(bidderRequest.gdprConsent.gdprApplies & 1) : false
}
}
};
if (BRs[0].schain) {
bid.source = {
'ext': {
'schain': BRs[0].schain
}
}
}
return bid;
}
function getFirstParam(key, validBidRequests) {
for (let i = 0; i < validBidRequests.length; i++) {
if (validBidRequests[i].params && validBidRequests[i].params[key]) {
return validBidRequests[i].params[key];
}
}
}
this.buildRequests = function (BRs, bidderRequest) {
let fallbackPlacementId = getFirstParam('placementId', BRs);
if (fallbackPlacementId === undefined || BRs.length < 1) {
return [];
}
var rmpUrl = getFirstParam('endpoint', BRs) || 'https://tag.1rx.io/rmp/{placementId}/0/{path}?z={zone}';
var defaultZone = getFirstParam('zone', BRs) || '1r';
var defaultPath = getFirstParam('path', BRs) || 'mvo';
rmpUrl = rmpUrl.replace(/\{placementId\}/i, fallbackPlacementId);
rmpUrl = rmpUrl.replace(/\{zone\}/i, defaultZone);
rmpUrl = rmpUrl.replace(/\{path\}/i, defaultPath);
var fat = /(^v|(\.0)+$)/gi;
var prebidVersion = '$prebid.version$';
rmpUrl += '&hbv=' + prebidVersion.replace(fat, '') + ',' + version.replace(fat, '');
var bidRequest = frameBid(BRs, bidderRequest);
if (!bidRequest.imp.length) {
return {};
}
return {
method: 'POST',
url: rmpUrl,
data: JSON.stringify(bidRequest)
};
};
this.interpretResponse = function (serverResponse) {
let responses = serverResponse.body || [];
let bids = [];
let i = 0;
if (responses.seatbid) {
let temp = [];
for (i = 0; i < responses.seatbid.length; i++) {
for (let j = 0; j < responses.seatbid[i].bid.length; j++) {
temp.push(responses.seatbid[i].bid[j]);
}
}
responses = temp;
}
for (i = 0; i < responses.length; i++) {
let bid = responses[i];
let bidRequest = slotsToBids[bid.impid];
let bidResponse = {
requestId: bidRequest.bidId,
cpm: parseFloat(bid.price),
width: bid.w,
height: bid.h,
meta: {
advertiserDomains: bid.adomain
},
creativeId: bid.crid,
currency: 'USD',
netRevenue: true,
ttl: 350
};
if (bidRequest.mediaTypes && bidRequest.mediaTypes.video) {
bidResponse.vastUrl = bid.nurl;
bidResponse.mediaType = 'video';
bidResponse.ttl = 600;
} else {
bidResponse.ad = bid.adm;
}
bids.push(bidResponse);
}
return bids;
};
}
export const spec = new RhythmOneBidAdapter();
registerBidder(spec);