forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadnowBidAdapter.js
186 lines (159 loc) · 4.88 KB
/
adnowBidAdapter.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
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE} from '../src/mediaTypes.js';
import {deepAccess, parseQueryStringParameters, parseSizesInput} from '../src/utils.js';
import {includes} from '../src/polyfill.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
const BIDDER_CODE = 'adnow';
const ENDPOINT = 'https://n.nnowa.com/a';
/**
* @typedef {object} CommonBidData
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerRequest} ServerRequest
* @typedef {import('../src/adapters/bidderFactory.js').BidderSpec} BidderSpec
*
* @property {string} requestId The specific BidRequest which this bid is aimed at.
* This should match the BidRequest.bidId which this Bid targets.
* @property {string} currency The currency code for the cpm value
* @property {number} cpm The bid price, in US cents per thousand impressions.
* @property {string} creativeId The id of ad content
* @property {number} ttl Time-to-live - how long (in seconds) Prebid can use this bid.
* @property {boolean} netRevenue Boolean defining whether the bid is Net or Gross. The default is true (Net).
* @property {object} [meta] Object for storing bid meta data
* @property {string} [meta.mediaType] banner or native
*/
/** @type {BidderSpec} */
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [ NATIVE, BANNER ],
/**
* @param {object} bid
* @return {boolean}
*/
isBidRequestValid(bid) {
if (!bid || !bid.params) return false;
const codeId = parseInt(bid.params.codeId, 10);
if (!codeId) {
return false;
}
const mediaType = bid.params.mediaType || NATIVE;
return includes(this.supportedMediaTypes, mediaType);
},
/**
* @param {BidRequest[]} validBidRequests
* @param {*} bidderRequest
* @return {ServerRequest}
*/
buildRequests(validBidRequests, bidderRequest) {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
return validBidRequests.map(req => {
const mediaType = this._isBannerRequest(req) ? BANNER : NATIVE;
const codeId = parseInt(req.params.codeId, 10);
const data = {
Id: codeId,
mediaType: mediaType,
out: 'prebid',
d_user_agent: navigator.userAgent,
requestid: req.bidId
};
if (mediaType === BANNER) {
data.sizes = parseSizesInput(
req.mediaTypes && req.mediaTypes.banner && req.mediaTypes.banner.sizes
).join('|');
} else {
data.width = data.height = 200;
let sizes = deepAccess(req, 'mediaTypes.native.image.sizes', []);
if (sizes.length > 0) {
const size = Array.isArray(sizes[0]) ? sizes[0] : sizes;
data.width = size[0] || data.width;
data.height = size[1] || data.height;
}
}
/** @type {ServerRequest} */
return {
method: 'GET',
url: ENDPOINT,
data: parseQueryStringParameters(data),
options: {
withCredentials: false,
crossOrigin: true
},
bidRequest: req
};
});
},
/**
* @param {*} response
* @param {ServerRequest} request
* @return {Bid[]}
*/
interpretResponse(response, request) {
const bidObj = request.bidRequest;
let bid = response.body;
if (!bid || !bid.currency || !bid.cpm) {
return [];
}
const mediaType = bid.meta.mediaType || NATIVE;
if (!includes(this.supportedMediaTypes, mediaType)) {
return [];
}
bid.requestId = bidObj.bidId;
if (mediaType === BANNER) {
return [ this._getBannerBid(bid) ];
}
if (mediaType === NATIVE) {
return [ this._getNativeBid(bid) ];
}
return [];
},
/**
* @private
* @param {object} bid
* @return {CommonBidData}
*/
_commonBidData(bid) {
return {
requestId: bid.requestId,
currency: bid.currency || 'USD',
cpm: bid.cpm || 0.00,
creativeId: bid.creativeId || 'undefined-creative',
netRevenue: bid.netRevenue || true,
ttl: bid.ttl || 360,
meta: bid.meta || {}
};
},
/**
* @param {BidRequest} req
* @return {boolean}
* @private
*/
_isBannerRequest(req) {
return !!(req.mediaTypes && req.mediaTypes.banner);
},
/**
* @private
* @param {object} bid
* @return {Bid}
*/
_getBannerBid(bid) {
return {
...this._commonBidData(bid),
width: bid.width || 300,
height: bid.height || 250,
ad: bid.ad || '<div>Empty Ad</div>'
};
},
/**
* @private
* @param {object} bid
* @return {Bid}
*/
_getNativeBid(bid) {
return {
...this._commonBidData(bid),
native: bid.native || {}
};
}
}
registerBidder(spec);