forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadqueryBidAdapter.js
268 lines (238 loc) · 7.48 KB
/
adqueryBidAdapter.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
import {buildUrl, logInfo, logMessage, parseSizesInput, triggerPixel} from '../src/utils.js';
/**
* @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
* @typedef {import('../src/adapters/bidderFactory.js').TimedOutBid} TimedOutBid
*/
const ADQUERY_GVLID = 902;
const ADQUERY_BIDDER_CODE = 'adquery';
const ADQUERY_BIDDER_DOMAIN_PROTOCOL = 'https';
const ADQUERY_BIDDER_DOMAIN = 'bidder.adquery.io';
const ADQUERY_STATIC_DOMAIN_PROTOCOL = 'https';
const ADQUERY_STATIC_DOMAIN = 'api.adquery.io';
const ADQUERY_USER_SYNC_DOMAIN = ADQUERY_BIDDER_DOMAIN;
const ADQUERY_DEFAULT_CURRENCY = 'PLN';
const ADQUERY_NET_REVENUE = true;
const ADQUERY_TTL = 360;
/** @type {BidderSpec} */
export const spec = {
code: ADQUERY_BIDDER_CODE,
gvlid: ADQUERY_GVLID,
supportedMediaTypes: [BANNER],
/**
* @param {object} bid
* @return {boolean}
*/
isBidRequestValid: (bid) => {
return !!(bid && bid.params && bid.params.placementId && bid.mediaTypes.banner.sizes)
},
/**
* @param {BidRequest[]} bidRequests
* @param {*} bidderRequest
* @return {ServerRequest}
*/
buildRequests: (bidRequests, bidderRequest) => {
const requests = [];
let adqueryRequestUrl = buildUrl({
protocol: ADQUERY_BIDDER_DOMAIN_PROTOCOL,
hostname: ADQUERY_BIDDER_DOMAIN,
pathname: '/prebid/bid',
// search: params
});
for (let i = 0, len = bidRequests.length; i < len; i++) {
const request = {
method: 'POST',
url: adqueryRequestUrl, // ADQUERY_BIDDER_DOMAIN_PROTOCOL + '://' + ADQUERY_BIDDER_DOMAIN + '/prebid/bid',
data: buildRequest(bidRequests[i], bidderRequest),
options: {
withCredentials: false,
crossOrigin: true
}
};
requests.push(request);
}
return requests;
},
/**
* @param {*} response
* @param {ServerRequest} request
* @return {Bid[]}
*/
interpretResponse: (response, request) => {
logMessage(request);
logMessage(response);
const res = response && response.body && response.body.data;
let bidResponses = [];
if (!res) {
return [];
}
const bidResponse = {
requestId: res.requestId,
cpm: res.cpm,
width: res.mediaType.width,
height: res.mediaType.height,
creativeId: res.creationId,
dealId: res.dealid || '',
currency: res.currency || ADQUERY_DEFAULT_CURRENCY,
netRevenue: ADQUERY_NET_REVENUE,
ttl: ADQUERY_TTL,
referrer: '',
ad: '<script src="' + res.adqLib + '"></script>' + res.tag,
mediaType: res.mediaType.name || 'banner',
meta: {
advertiserDomains: res.adDomains && res.adDomains.length ? res.adDomains : [],
mediaType: res.mediaType.name || 'banner',
}
};
bidResponses.push(bidResponse);
logInfo('bidResponses', bidResponses);
return bidResponses;
},
/**
* @param {TimedOutBid} timeoutData
*/
onTimeout: (timeoutData) => {
if (timeoutData == null) {
return;
}
logInfo('onTimeout ', timeoutData);
let params = {
bidder: timeoutData.bidder,
bId: timeoutData.bidId,
adUnitCode: timeoutData.adUnitCode,
timeout: timeoutData.timeout,
auctionId: timeoutData.auctionId,
};
let adqueryRequestUrl = buildUrl({
protocol: ADQUERY_BIDDER_DOMAIN_PROTOCOL,
hostname: ADQUERY_BIDDER_DOMAIN,
pathname: '/prebid/eventTimeout',
search: params
});
triggerPixel(adqueryRequestUrl);
},
/**
* @param {Bid} bid
*/
onBidWon: (bid) => {
logInfo('onBidWon', bid);
let copyOfBid = { ...bid }
delete copyOfBid.ad
const shortBidString = JSON.stringify(copyOfBid);
const encodedBuf = window.btoa(shortBidString);
let params = {
q: encodedBuf,
};
let adqueryRequestUrl = buildUrl({
protocol: ADQUERY_BIDDER_DOMAIN_PROTOCOL,
hostname: ADQUERY_BIDDER_DOMAIN,
pathname: '/prebid/eventBidWon',
search: params
});
triggerPixel(adqueryRequestUrl);
},
/**
* @param {Bid} bid
*/
onSetTargeting: (bid) => {
logInfo('onSetTargeting', bid);
let params = {
bidder: bid.bidder,
width: bid.width,
height: bid.height,
bid: bid.adId,
mediaType: bid.mediaType,
cpm: bid.cpm,
requestId: bid.requestId,
adUnitCode: bid.adUnitCode
};
let adqueryRequestUrl = buildUrl({
protocol: ADQUERY_BIDDER_DOMAIN_PROTOCOL,
hostname: ADQUERY_BIDDER_DOMAIN,
pathname: '/prebid/eventSetTargeting',
search: params
});
triggerPixel(adqueryRequestUrl);
},
/**
* Retrieves user synchronization URLs based on provided options and consents.
*
* @param {object} syncOptions - Options for synchronization.
* @param {object[]} serverResponses - Array of server responses.
* @param {object} gdprConsent - GDPR consent object.
* @param {object} uspConsent - USP consent object.
* @returns {object[]} - Array of synchronization URLs.
*/
getUserSyncs: (syncOptions, serverResponses, gdprConsent, uspConsent) => {
logMessage('getUserSyncs', syncOptions, serverResponses, gdprConsent, uspConsent);
let syncData = {
'gdpr': gdprConsent && gdprConsent.gdprApplies ? 1 : 0,
'gdpr_consent': gdprConsent && gdprConsent.consentString ? gdprConsent.consentString : '',
'ccpa_consent': uspConsent && uspConsent.uspConsent ? uspConsent.uspConsent : '',
};
if (window.qid) { // only for new users (new qid)
syncData.qid = window.qid;
}
let syncUrlObject = {
protocol: ADQUERY_BIDDER_DOMAIN_PROTOCOL,
hostname: ADQUERY_USER_SYNC_DOMAIN,
pathname: '/prebid/userSync',
search: syncData
};
if (syncOptions.iframeEnabled) {
syncUrlObject.protocol = ADQUERY_STATIC_DOMAIN_PROTOCOL;
syncUrlObject.hostname = ADQUERY_STATIC_DOMAIN;
syncUrlObject.pathname = '/user-sync-iframe.html';
return [{
type: 'iframe',
url: buildUrl(syncUrlObject)
}];
}
return [{
type: 'image',
url: buildUrl(syncUrlObject)
}];
}
};
function buildRequest(validBidRequests, bidderRequest) {
let bid = validBidRequests;
logInfo('buildRequest: ', bid);
let userId = null;
if (window.qid) {
userId = window.qid;
}
if (bid.userId && bid.userId.qid) {
userId = bid.userId.qid
}
if (!userId) {
// onetime User ID
const ramdomValues = Array.from(window.crypto.getRandomValues(new Uint32Array(4)));
userId = ramdomValues.map(val => val.toString(36)).join('').substring(0, 20);
logMessage('generated onetime User ID: ', userId);
window.qid = userId;
}
let pageUrl = '';
if (bidderRequest && bidderRequest.refererInfo) {
pageUrl = bidderRequest.refererInfo.page || '';
}
return {
v: '$prebid.version$',
placementCode: bid.params.placementId,
auctionId: null,
type: bid.params.type,
adUnitCode: bid.adUnitCode,
bidQid: userId,
bidId: bid.bidId,
bidder: bid.bidder,
bidPageUrl: pageUrl,
bidderRequestId: bid.bidderRequestId,
bidRequestsCount: bid.bidRequestsCount,
bidderRequestsCount: bid.bidderRequestsCount,
sizes: parseSizesInput(bid.mediaTypes.banner.sizes).toString(),
};
}
registerBidder(spec);