forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadplusBidAdapter.js
204 lines (184 loc) · 5.08 KB
/
adplusBidAdapter.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
import { registerBidder } from '../src/adapters/bidderFactory.js';
import * as utils from '../src/utils.js';
import { BANNER } from '../src/mediaTypes.js';
import { getStorageManager } from '../src/storageManager.js';
// #region Constants
export const BIDDER_CODE = 'adplus';
export const ADPLUS_ENDPOINT = 'https://ssp.ad-plus.com.tr/server/headerBidding';
export const DGID_CODE = 'adplus_dg_id';
export const SESSION_CODE = 'adplus_s_id';
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
const COOKIE_EXP = 1000 * 60 * 60 * 24; // 1 day
// #endregion
// #region Helpers
export function isValidUuid (uuid) {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
uuid
);
}
function getSessionId() {
let sid = storage.cookiesAreEnabled() && storage.getCookie(SESSION_CODE);
if (
!sid || !isValidUuid(sid)
) {
sid = utils.generateUUID();
setSessionId(sid);
}
return sid;
}
function setSessionId(sid) {
if (storage.cookiesAreEnabled()) {
const expires = new Date(Date.now() + COOKIE_EXP).toISOString();
storage.setCookie(SESSION_CODE, sid, expires);
}
}
// #endregion
// #region Bid request validation
function isBidRequestValid(bid) {
if (!bid) {
utils.logError(BIDDER_CODE, 'bid, can not be empty', bid);
return false;
}
if (!bid.params) {
utils.logError(BIDDER_CODE, 'bid.params is required.');
return false;
}
if (!bid.params.adUnitId || typeof bid.params.adUnitId !== 'string') {
utils.logError(
BIDDER_CODE,
'bid.params.adUnitId is missing or has wrong type.'
);
return false;
}
if (!bid.params.inventoryId || typeof bid.params.inventoryId !== 'string') {
utils.logError(
BIDDER_CODE,
'bid.params.inventoryId is missing or has wrong type.'
);
return false;
}
if (
!bid.mediaTypes ||
!bid.mediaTypes[BANNER] ||
!utils.isArray(bid.mediaTypes[BANNER].sizes) ||
bid.mediaTypes[BANNER].sizes.length <= 0 ||
!utils.isArrayOfNums(bid.mediaTypes[BANNER].sizes[0])
) {
utils.logError(BIDDER_CODE, 'Wrong or missing size parameters.');
return false;
}
return true;
}
// #endregion
// #region Building the bid requests
/**
*
* @param {object} bid
* @returns
*/
function createBidRequest(bid) {
// Developer Params
const {
inventoryId,
adUnitId,
extraData,
yearOfBirth,
gender,
categories,
latitude,
longitude,
sdkVersion,
} = bid.params;
return {
method: 'GET',
url: ADPLUS_ENDPOINT,
data: utils.cleanObj({
bidId: bid.bidId,
inventoryId,
adUnitId,
adUnitWidth: bid.mediaTypes[BANNER].sizes[0][0],
adUnitHeight: bid.mediaTypes[BANNER].sizes[0][1],
extraData,
yearOfBirth,
gender,
categories,
latitude,
longitude,
sdkVersion: sdkVersion || '1',
session: getSessionId(),
interstitial: 0,
token: typeof window.top === 'object' && window.top[DGID_CODE] ? window.top[DGID_CODE] : undefined,
secure: window.location.protocol === 'https:' ? 1 : 0,
screenWidth: screen.width,
screenHeight: screen.height,
language: window.navigator.language || 'en-US',
// TODO: these should probably look at refererInfo
pageUrl: window.location.href,
domain: window.location.hostname,
referrer: window.location.referrer,
}),
};
}
function buildRequests(validBidRequests, bidderRequest) {
return validBidRequests.map((req) => createBidRequest(req));
}
// #endregion
// #region Interpreting Responses
/**
*
* @param {HeaderBiddingResponse} responseData
* @param { object } bidParams
* @returns
*/
function createAdResponse(responseData, bidParams) {
return {
requestId: responseData.requestID,
cpm: responseData.cpm,
currency: responseData.currency,
width: responseData.width,
height: responseData.height,
creativeId: responseData.creativeID,
dealId: responseData.dealID,
netRevenue: responseData.netRevenue,
ttl: responseData.ttl,
ad: responseData.ad,
mediaType: responseData.mediaType,
meta: {
advertiserDomains: responseData.advertiserDomains,
primaryCatId: utils.isArray(responseData.categoryIDs) && responseData.categoryIDs.length > 0
? responseData.categoryIDs[0] : undefined,
secondaryCatIds: responseData.categoryIDs,
},
};
}
function interpretResponse(response, request) {
// In case of empty response
if (
response.body == null ||
!utils.isArray(response.body) ||
response.body.length === 0
) {
return [];
}
const bids = response.body.map((bid) => createAdResponse(bid));
return bids;
}
// #endregion
// #region Bidder
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
isBidRequestValid,
buildRequests,
interpretResponse,
onTimeout(timeoutData) {
utils.logError('Adplus adapter timed out for the auction.', timeoutData);
},
onBidWon(bid) {
utils.logInfo(
`Adplus adapter won the auction. Bid id: ${bid.bidId}, Ad Unit Id: ${bid.adUnitId}, Inventory Id: ${bid.inventoryId}`
);
},
};
registerBidder(spec);
// #endregion