forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajaBidAdapter.js
214 lines (183 loc) · 5.93 KB
/
ajaBidAdapter.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
import {createTrackPixelHtml, logError, getBidIdParameter} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import {tryAppendQueryString} from '../libraries/urlUtils/urlUtils.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').ServerRequest} ServerRequest
*/
const BidderCode = 'aja';
const URL = 'https://ad.as.amanad.adtdp.com/v2/prebid';
const SDKType = 5;
const AdType = {
Banner: 1,
Native: 2,
Video: 3,
};
const BannerSizeMap = {
'970x250': 1,
'300x250': 2,
'320x50': 3,
'728x90': 4,
'320x100': 6,
'336x280': 31,
'300x600': 32,
}
export const spec = {
code: BidderCode,
supportedMediaTypes: [BANNER],
/**
* Determines whether or not the given bid has all the params needed to make a valid request.
*
* @param {BidRequest} bidRequest
* @returns {boolean}
*/
isBidRequestValid: function(bidRequest) {
return !!(bidRequest.params.asi);
},
/**
* Build the request to the Server which requests Bids for the given array of Requests.
* Each BidRequest in the argument array is guaranteed to have passed the isBidRequestValid() test.
*
* @param {BidRequest[]} validBidRequests
* @param {*} bidderRequest
* @returns {ServerRequest|ServerRequest[]}
*/
buildRequests: function(validBidRequests, bidderRequest) {
const bidRequests = [];
const pageUrl = bidderRequest?.refererInfo?.page || undefined;
for (let i = 0, len = validBidRequests.length; i < len; i++) {
const bidRequest = validBidRequests[i];
let queryString = '';
const asi = getBidIdParameter('asi', bidRequest.params);
queryString = tryAppendQueryString(queryString, 'asi', asi);
queryString = tryAppendQueryString(queryString, 'skt', SDKType);
queryString = tryAppendQueryString(queryString, 'gpid', bidRequest.ortb2Imp?.ext?.gpid)
queryString = tryAppendQueryString(queryString, 'tid', bidRequest.ortb2Imp?.ext?.tid)
queryString = tryAppendQueryString(queryString, 'cdep', bidRequest.ortb2?.device?.ext?.cdep)
queryString = tryAppendQueryString(queryString, 'prebid_id', bidRequest.bidId);
queryString = tryAppendQueryString(queryString, 'prebid_ver', '$prebid.version$');
queryString = tryAppendQueryString(queryString, 'page_url', pageUrl);
queryString = tryAppendQueryString(queryString, 'schain', spec.serializeSupplyChain(bidRequest.schain || []))
const adFormatIDs = pickAdFormats(bidRequest)
if (adFormatIDs && adFormatIDs.length > 0) {
queryString = tryAppendQueryString(queryString, 'ad_format_ids', adFormatIDs.join(','));
}
const eids = bidRequest.userIdAsEids;
if (eids && eids.length) {
queryString = tryAppendQueryString(queryString, 'eids', JSON.stringify({
'eids': eids,
}));
}
const sua = bidRequest.ortb2?.device?.sua
if (sua) {
queryString = tryAppendQueryString(queryString, 'sua', JSON.stringify(sua));
}
bidRequests.push({
method: 'GET',
url: URL,
data: queryString
});
}
return bidRequests;
},
interpretResponse: function(bidderResponse) {
const bidderResponseBody = bidderResponse.body;
if (!bidderResponseBody.is_ad_return) {
return [];
}
const ad = bidderResponseBody.ad;
if (AdType.Banner !== ad.ad_type) {
return []
}
const bannerAd = bidderResponseBody.ad.banner;
const bid = {
requestId: ad.prebid_id,
mediaType: BANNER,
ad: bannerAd.tag,
width: bannerAd.w,
height: bannerAd.h,
cpm: ad.price,
creativeId: ad.creative_id,
dealId: ad.deal_id,
currency: ad.currency || 'USD',
netRevenue: true,
ttl: 300, // 5 minutes
meta: {
advertiserDomains: bannerAd.adomain,
},
}
try {
bannerAd.imps.forEach(impTracker => {
const tracker = createTrackPixelHtml(impTracker);
bid.ad += tracker;
});
} catch (error) {
logError('Error appending tracking pixel', error);
}
return [bid];
},
getUserSyncs: function(syncOptions, serverResponses) {
const syncs = [];
if (!serverResponses.length) {
return syncs;
}
const bidderResponseBody = serverResponses[0].body;
if (syncOptions.pixelEnabled && bidderResponseBody.syncs) {
bidderResponseBody.syncs.forEach(sync => {
syncs.push({
type: 'image',
url: sync
});
});
}
if (syncOptions.iframeEnabled && bidderResponseBody.sync_htmls) {
bidderResponseBody.sync_htmls.forEach(sync => {
syncs.push({
type: 'iframe',
url: sync
});
});
}
return syncs;
},
/**
* Serialize supply chain object
* @param {Object} supplyChain
* @returns {String | undefined}
*/
serializeSupplyChain: function(supplyChain) {
if (!supplyChain || !supplyChain.nodes) return undefined
const { ver, complete, nodes } = supplyChain
return `${ver},${complete}!${spec.serializeSupplyChainNodes(nodes)}`
},
/**
* Serialize each supply chain nodes
* @param {Array} nodes
* @returns {String}
*/
serializeSupplyChainNodes: function(nodes) {
const fields = ['asi', 'sid', 'hp', 'rid', 'name', 'domain']
return nodes.map((n) => {
return fields.map((f) => {
return encodeURIComponent(n[f] || '').replace(/!/g, '%21')
}).join(',')
}).join('!')
}
}
function pickAdFormats(bidRequest) {
let sizes = bidRequest.sizes || []
sizes.push(...(bidRequest.mediaTypes?.banner?.sizes || []))
const adFormatIDs = [];
for (const size of sizes) {
if (size.length !== 2) {
continue
}
const adFormatID = BannerSizeMap[`${size[0]}x${size[1]}`];
if (adFormatID) {
adFormatIDs.push(adFormatID);
}
}
return [...new Set(adFormatIDs)]
}
registerBidder(spec);