forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadriverBidAdapter.js
213 lines (189 loc) · 5.83 KB
/
adriverBidAdapter.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
// ADRIVER BID ADAPTER for Prebid 1.13
import {logInfo, getWindowLocation, _each, getBidIdParameter, isPlainObject} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';
const BIDDER_CODE = 'adriver';
const ADRIVER_BID_URL = 'https://pb.adriver.ru/cgi-bin/bid.cgi';
const TIME_TO_LIVE = 3000;
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
export const spec = {
code: BIDDER_CODE,
/**
* 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.params.siteid;
},
buildRequests: function (validBidRequests, bidderRequest) {
let win = getWindowLocation();
let customID = Math.round(Math.random() * 999999999) + '-' + Math.round(new Date() / 1000) + '-1-46-';
let siteId = getBidIdParameter('siteid', validBidRequests[0].params) + '';
let currency = getBidIdParameter('currency', validBidRequests[0].params);
currency = 'RUB';
let timeout = null;
if (bidderRequest) {
timeout = bidderRequest.timeout;
}
const payload = {
'at': 1,
'cur': [currency],
'tmax': timeout,
'site': {
'name': win.origin,
'domain': win.hostname,
'id': siteId,
'page': win.href
},
'id': customID,
'user': {
'buyerid': 0,
'ext': {
'eids': getUserIdAsEids(validBidRequests)
}
},
'device': {
'ip': '195.209.111.14',
'ua': window.navigator.userAgent
},
'imp': []
};
_each(validBidRequests, (bid) => {
_each(bid.sizes, (sizes) => {
let width;
let height;
let par;
let floorAndCurrency = _getFloor(bid, currency, sizes);
let bidFloor = floorAndCurrency.floor;
let dealId = getBidIdParameter('dealid', bid.params);
if (typeof sizes[0] === 'number' && typeof sizes[1] === 'number') {
width = sizes[0];
height = sizes[1];
}
par = {
'id': bid.params.placementId,
'ext': {'query': 'bn=15&custom=111=' + bid.bidId},
'banner': {
'w': width || undefined,
'h': height || undefined
},
'bidfloor': bidFloor || 0,
'bidfloorcur': floorAndCurrency.currency,
'secure': 0
};
if (dealId) {
par.pmp = {
'private_auction': 1,
'deals': [{
'id': dealId,
'bidfloor': bidFloor || 0,
'bidfloorcur': currency
}]
};
}
logInfo('par', par);
payload.imp.push(par);
});
});
let adrcidCookie = storage.getDataFromLocalStorage('adrcid') || validBidRequests[0].userId?.adrcid;
if (adrcidCookie) {
payload.user.buyerid = adrcidCookie;
}
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: ADRIVER_BID_URL,
data: payloadString
};
},
interpretResponse: function (serverResponse, bidRequest) {
logInfo('serverResponse.body.seatbid', serverResponse.body.seatbid);
const bidResponses = [];
let nurl = 0;
_each(serverResponse.body.seatbid, (seatbid) => {
logInfo('_each', seatbid);
var bid = seatbid.bid[0];
if (bid.nurl !== undefined) {
nurl = bid.nurl.split('://');
nurl = window.location.protocol + '//' + nurl[1];
nurl = nurl.replace(/\$\{AUCTION_PRICE\}/, bid.price);
}
if (bid.price >= 0 && bid.impid !== undefined && nurl !== 0 && bid.dealid === undefined) {
let bidResponse = {
requestId: bid.ext || undefined,
cpm: bid.price,
width: bid.w,
height: bid.h,
creativeId: bid.impid || undefined,
currency: serverResponse.body.cur,
netRevenue: true,
ttl: TIME_TO_LIVE,
meta: {
advertiserDomains: bid.adomain
},
ad: '<IFRAME SRC="' + bid.nurl + '" FRAMEBORDER="0" SCROLLING="no" MARGINHEIGHT="0" MARGINWIDTH="0" TOPMARGIN="0" LEFTMARGIN="0" ALLOWTRANSPARENCY="true" STYLE ="WIDTH:' + bid.w + 'px; HEIGHT:' + bid.h + 'px"></IFRAME>'
};
logInfo('bidResponse', bidResponse);
bidResponses.push(bidResponse);
}
});
return bidResponses;
}
};
registerBidder(spec);
/**
* get first userId from validBidRequests
* @param validBidRequests
* @returns {Array|*} userIdAsEids
*/
function getUserIdAsEids(validBidRequests) {
if (validBidRequests && validBidRequests.length > 0 && validBidRequests[0].userIdAsEids &&
validBidRequests[0].userIdAsEids.length > 0) {
return validBidRequests[0].userIdAsEids;
} else {
return [];
}
}
/**
* Gets bidfloor
* @param {Object} bid
* @param currencyPar
* @param sizes
* @returns {Object} floor
*/
function _getFloor(bid, currencyPar, sizes) {
const curMediaType = bid.mediaTypes && bid.mediaTypes.video ? 'video' : 'banner';
let floor = 0;
const currency = currencyPar || 'RUB';
let currencyResult = '';
let isSize = false;
if (typeof sizes[0] === 'number' && typeof sizes[1] === 'number') {
isSize = true;
}
if (typeof bid.getFloor === 'function') {
const floorInfo = bid.getFloor({
currency: currency,
mediaType: curMediaType,
size: isSize ? sizes : '*'
});
if (isPlainObject(floorInfo) &&
!isNaN(parseFloat(floorInfo?.floor))) {
floor = floorInfo.floor;
}
if (isPlainObject(floorInfo) && floorInfo.currency) {
currencyResult = floorInfo.currency;
}
}
if (!currencyResult) {
currencyResult = currency;
}
if (floor == null) {
floor = 0;
}
return {
floor: floor,
currency: currencyResult
};
}