forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotmBidAdapter.js
151 lines (135 loc) · 3.68 KB
/
otmBidAdapter.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
import { registerBidder } from '../src/adapters/bidderFactory.js';
import {
logInfo,
logError,
_each,
getValue,
isFn,
isPlainObject,
isArray,
isStr,
isNumber, getBidIdParameter,
} from '../src/utils.js';
import { BANNER } from '../src/mediaTypes.js';
const BIDDER_CODE = 'otm';
const OTM_BID_URL = 'https://ssp.otm-r.com/adjson';
const DEFAULT_CURRENCY = 'RUB'
export const spec = {
code: BIDDER_CODE,
url: OTM_BID_URL,
supportedMediaTypes: [ BANNER ],
/**
* 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 Boolean(bid.params.tid);
},
/**
* Build bidder requests.
*
* @param validBidRequests
* @param bidderRequest
* @returns {[]}
*/
buildRequests: function (validBidRequests, bidderRequest) {
logInfo('validBidRequests', validBidRequests);
const bidRequests = [];
const tz = new Date().getTimezoneOffset()
// TODO: are these the right referer values?
const referrer = bidderRequest?.refererInfo?.page || '';
const topOrigin = bidderRequest?.refererInfo?.domain || '';
_each(validBidRequests, (bid) => {
const domain = isStr(bid.params.domain) ? bid.params.domain : topOrigin
const cur = getValue(bid.params, 'currency') || DEFAULT_CURRENCY
const bidid = getBidIdParameter('bidId', bid)
const transactionid = bid.ortb2Imp?.ext?.tid || '';
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
const auctionid = getBidIdParameter('auctionId', bid)
const bidfloor = _getBidFloor(bid)
_each(bid.sizes, size => {
const hasSizes = isArray(size) && isNumber(size[0]) && isNumber(size[1])
const width = hasSizes ? size[0] : 0;
const height = hasSizes ? size[1] : 0;
bidRequests.push({
method: 'GET',
url: OTM_BID_URL,
data: {
tz,
w: width,
h: height,
domain,
l: referrer,
s: bid.params.tid,
cur,
bidid,
transactionid,
auctionid,
bidfloor,
},
})
})
})
return bidRequests;
},
/**
* Generate response.
*
* @param serverResponse
* @returns {[]|*[]}
*/
interpretResponse: function (serverResponse) {
logInfo('serverResponse', serverResponse.body);
const responsesBody = serverResponse ? serverResponse.body : {};
const bidResponses = [];
try {
if (responsesBody.length === 0) {
return [];
}
_each(responsesBody, (bid) => {
if (bid.ad) {
bidResponses.push({
requestId: bid.bidid,
cpm: bid.cpm,
width: bid.w,
height: bid.h,
creativeId: bid.creativeid,
currency: bid.currency || DEFAULT_CURRENCY,
netRevenue: true,
ad: bid.ad,
ttl: bid.ttl,
meta: {
advertiserDomains: bid.adDomain ? [bid.adDomain] : []
}
});
}
});
} catch (error) {
logError(error);
}
return bidResponses;
}
};
/**
* Get floor value
* @param bid
* @returns {null|*}
* @private
*/
function _getBidFloor(bid) {
if (!isFn(bid.getFloor)) {
return bid.params.bidfloor ? bid.params.bidfloor : 0;
}
const floor = bid.getFloor({
currency: DEFAULT_CURRENCY,
mediaType: '*',
size: '*'
});
if (isPlainObject(floor) && !isNaN(floor.floor) && floor.currency === DEFAULT_CURRENCY) {
return floor.floor;
}
return 0;
}
registerBidder(spec);