forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnigelBidAdapter.js
256 lines (228 loc) · 8.16 KB
/
snigelBidAdapter.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
import {config} from '../src/config.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
import {deepAccess, isArray, isFn, isPlainObject, inIframe, getDNT, generateUUID} from '../src/utils.js';
import {hasPurpose1Consent} from '../src/utils/gdpr.js';
import {getStorageManager} from '../src/storageManager.js';
const BIDDER_CODE = 'snigel';
const GVLID = 1076;
const DEFAULT_URL = 'https://adserv.snigelweb.com/bp/v1/prebid';
const DEFAULT_TTL = 60;
const DEFAULT_CURRENCIES = ['USD'];
const FLOOR_MATCH_ALL_SIZES = '*';
const SESSION_ID_KEY = '_sn_session_pba';
const getConfig = config.getConfig;
const storageManager = getStorageManager({bidderCode: BIDDER_CODE});
const refreshes = {};
const placementCounters = {};
const pageViewId = generateUUID();
const pageViewStart = new Date().getTime();
let auctionCounter = 0;
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER],
isBidRequestValid: function (bidRequest) {
return !!bidRequest.params.placement;
},
buildRequests: function (bidRequests, bidderRequest) {
const gdprApplies = deepAccess(bidderRequest, 'gdprConsent.gdprApplies');
return {
method: 'POST',
url: getEndpoint(),
data: JSON.stringify({
id: bidderRequest.auctionId,
accountId: deepAccess(bidRequests, '0.params.accountId'),
site: deepAccess(bidRequests, '0.params.site'),
sessionId: getSessionId(),
counter: auctionCounter++,
pageViewId: pageViewId,
pageViewStart: pageViewStart,
gdprConsent: gdprApplies === true ? hasFullGdprConsent(deepAccess(bidderRequest, 'gdprConsent')) : false,
cur: getCurrencies(),
test: getTestFlag(),
version: 'v' + '$prebid.version$',
adapterVersion: '2.0',
gpp: deepAccess(bidderRequest, 'gppConsent.gppString') || deepAccess(bidderRequest, 'ortb2.regs.gpp'),
gpp_sid:
deepAccess(bidderRequest, 'gppConsent.applicableSections') || deepAccess(bidderRequest, 'ortb2.regs.gpp_sid'),
gdprApplies: gdprApplies,
gdprConsentString: gdprApplies === true ? deepAccess(bidderRequest, 'gdprConsent.consentString') : undefined,
gdprConsentProv: gdprApplies === true ? deepAccess(bidderRequest, 'gdprConsent.addtlConsent') : undefined,
uspConsent: deepAccess(bidderRequest, 'uspConsent'),
coppa: getConfig('coppa'),
eids: deepAccess(bidRequests, '0.userIdAsEids'),
schain: deepAccess(bidRequests, '0.schain'),
page: getPage(bidderRequest),
topframe: inIframe() === true ? 0 : 1,
device: {
w: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
h: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
dnt: getDNT() ? 1 : 0,
language: getLanguage(),
},
placements: bidRequests.map((r) => {
return {
id: r.adUnitCode,
tid: r.transactionId,
gpid: deepAccess(r, 'ortb2Imp.ext.gpid'),
pbadslot: deepAccess(r, 'ortb2Imp.ext.data.pbadslot') || deepAccess(r, 'ortb2Imp.ext.gpid'),
name: r.params.placement,
counter: getPlacementCounter(r.params.placement),
sizes: r.sizes,
floor: getPriceFloor(r, BANNER, FLOOR_MATCH_ALL_SIZES),
refresh: getRefreshInformation(r.adUnitCode),
params: r.params.additionalParams,
};
}),
}),
bidderRequest,
};
},
interpretResponse: function (serverResponse, bidRequest) {
if (!serverResponse.body || !serverResponse.body.bids) {
return [];
}
return serverResponse.body.bids.map((bid) => {
return {
requestId: mapIdToRequestId(bid.id, bidRequest),
cpm: bid.price,
creativeId: bid.crid,
currency: serverResponse.body.cur,
width: bid.width,
height: bid.height,
ad: bid.ad,
netRevenue: true,
ttl: bid.ttl || DEFAULT_TTL,
meta: bid.meta,
};
});
},
getUserSyncs: function (syncOptions, responses, gdprConsent, uspConsent, gppConsent) {
const syncUrl = getSyncUrl(responses || []);
if (syncUrl && syncOptions.iframeEnabled && hasSyncConsent(gdprConsent, uspConsent, gppConsent)) {
return [{type: 'iframe', url: getSyncEndpoint(syncUrl, gdprConsent)}];
}
},
};
registerBidder(spec);
function getPage(bidderRequest) {
return getConfig(`${BIDDER_CODE}.page`) || deepAccess(bidderRequest, 'refererInfo.page') || window.location.href;
}
function getEndpoint() {
return getConfig(`${BIDDER_CODE}.url`) || DEFAULT_URL;
}
function getTestFlag() {
return getConfig(`${BIDDER_CODE}.test`) === true;
}
function getLanguage() {
return navigator && navigator.language
? navigator.language.indexOf('-') != -1
? navigator.language.split('-')[0]
: navigator.language
: undefined;
}
function getCurrencies() {
const currencyOverrides = getConfig(`${BIDDER_CODE}.cur`);
if (currencyOverrides !== undefined && (!isArray(currencyOverrides) || currencyOverrides.length === 0)) {
throw Error('Currency override must be an array with at least one currency');
}
return currencyOverrides || DEFAULT_CURRENCIES;
}
function getFloorCurrency() {
return getConfig(`${BIDDER_CODE}.floorCur`) || getCurrencies()[0];
}
function getPriceFloor(bidRequest, mediaType, size) {
if (isFn(bidRequest.getFloor)) {
const cur = getFloorCurrency();
const floorInfo = bidRequest.getFloor({
currency: cur,
mediaType: mediaType,
size: size,
});
if (isPlainObject(floorInfo) && !isNaN(floorInfo.floor)) {
return {
cur: floorInfo.currency || cur,
value: floorInfo.floor,
};
}
}
}
function getRefreshInformation(adUnitCode) {
const refresh = refreshes[adUnitCode];
if (!refresh) {
refreshes[adUnitCode] = {
count: 0,
previousTime: new Date(),
};
return undefined;
}
const currentTime = new Date();
const timeDifferenceSeconds = Math.floor((currentTime - refresh.previousTime) / 1000);
refresh.count += 1;
refresh.previousTime = currentTime;
return {
count: refresh.count,
time: timeDifferenceSeconds,
};
}
function getPlacementCounter(placement) {
const counter = placementCounters[placement];
if (counter === undefined) {
placementCounters[placement] = 0;
return 0;
}
placementCounters[placement]++;
return placementCounters[placement];
}
function mapIdToRequestId(id, bidRequest) {
return bidRequest.bidderRequest.bids.filter((bid) => bid.adUnitCode === id)[0].bidId;
}
function hasUspConsent(uspConsent) {
return typeof uspConsent !== 'string' || !(uspConsent[0] === '1' && uspConsent[2] === 'Y');
}
function hasGppConsent(gppConsent) {
return (
!(gppConsent && Array.isArray(gppConsent.applicableSections)) ||
gppConsent.applicableSections.every((section) => typeof section === 'number' && section <= 5)
);
}
function hasSyncConsent(gdprConsent, uspConsent, gppConsent) {
return hasPurpose1Consent(gdprConsent) && hasUspConsent(uspConsent) && hasGppConsent(gppConsent);
}
function hasFullGdprConsent(gdprConsent) {
try {
const purposeConsents = Object.values(gdprConsent.vendorData.purpose.consents);
return (
purposeConsents.length > 0 &&
purposeConsents.every((value) => value === true) &&
gdprConsent.vendorData.vendor.consents[GVLID] === true
);
} catch (e) {
return false;
}
}
function getSyncUrl(responses) {
return getConfig(`${BIDDER_CODE}.syncUrl`) || deepAccess(responses[0], 'body.syncUrl');
}
function getSyncEndpoint(url, gdprConsent) {
return `${url}?gdpr=${gdprConsent?.gdprApplies ? 1 : 0}&gdpr_consent=${encodeURIComponent(
gdprConsent?.consentString || ''
)}`;
}
function getSessionId() {
try {
if (storageManager.localStorageIsEnabled()) {
let sessionId = storageManager.getDataFromLocalStorage(SESSION_ID_KEY);
if (sessionId == null) {
sessionId = generateUUID();
storageManager.setDataInLocalStorage(SESSION_ID_KEY, sessionId);
}
return sessionId;
} else {
return undefined;
}
} catch (e) {
return undefined;
}
}