forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxonixBidAdapter.js
184 lines (150 loc) · 4.68 KB
/
axonixBidAdapter.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
import {deepAccess, isArray, isEmpty, logError, replaceAuctionPrice, triggerPixel} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {config} from '../src/config.js';
import {ajax} from '../src/ajax.js';
const BIDDER_CODE = 'axonix';
const BIDDER_VERSION = '1.0.2';
const CURRENCY = 'USD';
const DEFAULT_REGION = 'us-east-1';
function getBidFloor(bidRequest) {
let floorInfo = {};
if (typeof bidRequest.getFloor === 'function') {
floorInfo = bidRequest.getFloor({
currency: CURRENCY,
mediaType: '*',
size: '*'
});
}
return floorInfo?.floor || 0;
}
function getPageUrl(bidRequest, bidderRequest) {
let pageUrl;
if (bidRequest.params.referrer) {
pageUrl = bidRequest.params.referrer;
} else {
pageUrl = bidderRequest.refererInfo.page;
}
return bidRequest.params.secure ? pageUrl.replace(/^http:/i, 'https:') : pageUrl;
}
function isMobile() {
return (/(ios|ipod|ipad|iphone|android)/i).test(navigator.userAgent);
}
function isConnectedTV() {
return (/(smart[-]?tv|hbbtv|appletv|googletv|hdmi|netcast\.tv|viera|nettv|roku|\bdtv\b|sonydtv|inettvbrowser|\btv\b)/i).test(navigator.userAgent);
}
function getURL(params, path) {
let { supplyId, region, endpoint } = params;
let url;
if (endpoint) {
url = endpoint;
} else if (region) {
url = `https://openrtb-${region}.axonix.com/supply/${path}/${supplyId}`;
} else {
url = `https://openrtb-${DEFAULT_REGION}.axonix.com/supply/${path}/${supplyId}`
}
return url;
}
export const spec = {
code: BIDDER_CODE,
version: BIDDER_VERSION,
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid: function(bid) {
// video bid request validation
if (bid.hasOwnProperty('mediaTypes') && bid.mediaTypes.hasOwnProperty(VIDEO)) {
if (!bid.mediaTypes[VIDEO].hasOwnProperty('mimes') ||
!isArray(bid.mediaTypes[VIDEO].mimes) ||
bid.mediaTypes[VIDEO].mimes.length === 0) {
logError('mimes are mandatory for video bid request. Ad Unit: ', JSON.stringify(bid));
return false;
}
}
return !!(bid.params && bid.params.supplyId);
},
buildRequests: function(validBidRequests, bidderRequest) {
// device.connectiontype
let connection = window.navigator && (window.navigator.connection || window.navigator.mozConnection || window.navigator.webkitConnection)
let connectionType = 'unknown';
let effectiveType = '';
if (connection) {
if (connection.type) {
connectionType = connection.type;
}
if (connection.effectiveType) {
effectiveType = connection.effectiveType;
}
}
const requests = validBidRequests.map(validBidRequest => {
// app/site
let app;
let site;
if (typeof config.getConfig('app') === 'object') {
app = config.getConfig('app');
} else {
site = {
page: getPageUrl(validBidRequest, bidderRequest)
}
}
const data = {
app,
site,
validBidRequest,
connectionType,
effectiveType,
devicetype: isMobile() ? 1 : isConnectedTV() ? 3 : 2,
bidfloor: getBidFloor(validBidRequest),
dnt: (navigator.doNotTrack === 'yes' || navigator.doNotTrack === '1' || navigator.msDoNotTrack === '1') ? 1 : 0,
language: navigator.language,
prebidVersion: '$prebid.version$',
screenHeight: screen.height,
screenWidth: screen.width,
tmax: bidderRequest.timeout,
ua: navigator.userAgent,
};
return {
method: 'POST',
url: getURL(validBidRequest.params, 'prebid'),
options: {
withCredentials: false,
contentType: 'application/json'
},
data
};
});
return requests;
},
interpretResponse: function(serverResponse) {
const response = serverResponse ? serverResponse.body : [];
if (!isArray(response)) {
return [];
}
const responses = [];
for (const resp of response) {
if (resp.requestId) {
responses.push(Object.assign(resp, {
ttl: 60
}));
}
}
return responses;
},
onTimeout: function(timeoutData) {
const params = deepAccess(timeoutData, '0.params.0');
if (!isEmpty(params)) {
ajax(getURL(params, 'prebid/timeout'), null, timeoutData[0], {
method: 'POST',
options: {
withCredentials: false,
contentType: 'application/json'
}
});
}
},
onBidWon: function(bid) {
const { nurl } = bid || {};
if (bid.nurl) {
triggerPixel(replaceAuctionPrice(nurl, bid.originalCpm || bid.cpm));
};
}
}
registerBidder(spec);