forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwireBidAdapter.js
266 lines (239 loc) · 7.48 KB
/
cwireBidAdapter.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
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getStorageManager} from '../src/storageManager.js';
import {BANNER} from '../src/mediaTypes.js';
import {generateUUID, getParameterByName, isNumber, logError, logInfo} from '../src/utils.js';
import {hasPurpose1Consent} from '../src/utils/gdpr.js';
import { sendBeacon } from '../src/ajax.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
*/
// ------------------------------------
const BIDDER_CODE = 'cwire';
const CWID_KEY = 'cw_cwid';
export const BID_ENDPOINT = 'https://prebid.cwi.re/v1/bid';
export const EVENT_ENDPOINT = 'https://prebid.cwi.re/v1/event';
export const GVL_ID = 1081;
/**
* Allows limiting ad impressions per site render. Unique per prebid instance ID.
*/
export const pageViewId = generateUUID();
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
/**
* Retrieve dimensions and CSS max height/width from a given slot and attach the properties to the bidRequest.
* @param bid
* @returns {*&{cwExt: {dimensions: {width: number, height: number}, style: {maxWidth: number, maxHeight: number}}}}
*/
function slotDimensions(bid) {
let adUnitCode = bid.adUnitCode;
let slotEl = document.getElementById(adUnitCode);
if (slotEl) {
logInfo(`Slot element found: ${adUnitCode}`)
const slotW = slotEl.offsetWidth
const slotH = slotEl.offsetHeight
const cssMaxW = slotEl.style?.maxWidth;
const cssMaxH = slotEl.style?.maxHeight;
logInfo(`Slot dimensions (w/h): ${slotW} / ${slotH}`)
logInfo(`Slot Styles (maxW/maxH): ${cssMaxW} / ${cssMaxH}`)
bid = {
...bid,
cwExt: {
dimensions: {
width: slotW,
height: slotH,
},
style: {
...(cssMaxW) && {
maxWidth: cssMaxW
},
...(cssMaxH) && {
maxHeight: cssMaxH
}
}
}
}
}
return bid
}
/**
* Extracts feature flags from a comma-separated url parameter `cwfeatures`.
*
* @returns *[]
*/
function getFeatureFlags() {
let ffParam = getParameterByName('cwfeatures')
if (ffParam) {
return ffParam.split(',')
}
return []
}
function getRefGroups() {
const groups = getParameterByName('cwgroups')
if (groups) {
return groups.split(',')
}
return []
}
/**
* Reads the CWID from local storage.
*/
function getCwid() {
return storage.localStorageIsEnabled() ? storage.getDataFromLocalStorage(CWID_KEY) : null;
}
function hasCwid() {
return storage.localStorageIsEnabled() && storage.getDataFromLocalStorage(CWID_KEY);
}
/**
* Store the CWID to local storage.
*/
function updateCwid(cwid) {
if (storage.localStorageIsEnabled()) {
storage.setDataInLocalStorage(CWID_KEY, cwid)
} else {
logInfo(`Could not set CWID ${cwid} in localstorage`);
}
}
/**
* Extract and collect cwire specific extensions.
*/
function getCwExtension() {
const cwId = getCwid();
const cwCreative = getParameterByName('cwcreative')
const cwGroups = getRefGroups()
const cwFeatures = getFeatureFlags();
// Enable debug flag by passing ?cwdebug=true as url parameter.
// Note: pbjs_debug=true enables it on prebid level
// More info: https://docs.prebid.org/troubleshooting/troubleshooting-guide.html#turn-on-prebidjs-debug-messages
const debug = getParameterByName('cwdebug');
return {
...(cwId) && {
cwid: cwId
},
...(cwGroups.length > 0) && {
refgroups: cwGroups
},
...(cwFeatures.length > 0) && {
featureFlags: cwFeatures
},
...(cwCreative) && {
cwcreative: cwCreative
},
...(debug) && {
debug: true
}
};
}
export const spec = {
code: BIDDER_CODE,
gvlid: GVL_ID,
supportedMediaTypes: [BANNER],
/**
* Determines whether the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
if (!bid.params?.domainId || !isNumber(bid.params.domainId)) {
logError('domainId not provided or not a number');
if (!bid.params?.placementId || !isNumber(bid.params.placementId)) {
logError('placementId not provided or not a number');
return false;
}
if (!bid.params?.pageId || !isNumber(bid.params.pageId)) {
logError('pageId not provided or not a number');
return false;
}
return true;
}
return true;
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} validBidRequests An array of bids.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
// There are more fields on the refererInfo object
let referrer = bidderRequest?.refererInfo?.page
// process bid requests
let processed = validBidRequests
.map(bid => slotDimensions(bid))
// Flattens the pageId, domainId and placement Id for backwards compatibility.
.map((bid) => ({...bid, pageId: bid.params?.pageId, domainId: bid.params?.domainId, placementId: bid.params?.placementId}));
const extensions = getCwExtension();
const payload = {
slots: processed,
httpRef: referrer,
// TODO: Verify whether the auctionId and the usage of pageViewId make sense.
pageViewId: pageViewId,
sdk: {
version: '$prebid.version$'
},
...extensions
};
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: BID_ENDPOINT,
data: payloadString,
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequest) {
if (!hasCwid()) {
const cwid = serverResponse.body?.cwid
if (cwid) {
updateCwid(cwid);
}
}
// Rename `html` response property to `ad` as used by prebid.
const bids = serverResponse.body?.bids.map(({html, ...rest}) => ({...rest, ad: html}));
return bids || [];
},
onBidWon: function (bid) {
logInfo(`Bid won.`)
const event = {
type: 'BID_WON',
payload: {
bid: bid
}
}
sendBeacon(EVENT_ENDPOINT, JSON.stringify(event))
},
onBidderError: function (error, bidderRequest) {
logInfo(`Bidder error: ${error}`)
const event = {
type: 'BID_ERROR',
payload: {
error: error,
bidderRequest: bidderRequest
}
}
sendBeacon(EVENT_ENDPOINT, JSON.stringify(event))
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {
logInfo('Collecting user-syncs: ', JSON.stringify({syncOptions, gdprConsent, uspConsent, serverResponses}));
const syncs = []
if (hasPurpose1Consent(gdprConsent)) {
logInfo('GDPR purpose 1 consent was given, adding user-syncs')
let type = (syncOptions.pixelEnabled) ? 'image' : null ?? (syncOptions.iframeEnabled) ? 'iframe' : null
if (type) {
syncs.push({
type: type,
url: 'https://ib.adnxs.com/getuid?https://prebid.cwi.re/v1/cookiesync?xandrId=$UID'
})
}
}
logInfo('Collected user-syncs: ', JSON.stringify({syncs}))
return syncs
}
};
registerBidder(spec);