|
1 | 1 | import { registerBidder } from '../src/adapters/bidderFactory.js';
|
2 | 2 | import { config } from '../src/config.js';
|
| 3 | +import * as utils from '../src/utils.js'; |
| 4 | +import * as url from '../src/url.js'; |
3 | 5 |
|
4 | 6 | const BIDDER_CODE = 'sublime';
|
| 7 | +const BIDDER_GVLID = 114; |
5 | 8 | const DEFAULT_BID_HOST = 'pbjs.sskzlabs.com';
|
6 | 9 | const DEFAULT_CURRENCY = 'EUR';
|
7 | 10 | const DEFAULT_PROTOCOL = 'https';
|
8 | 11 | const DEFAULT_TTL = 600;
|
9 |
| -const SUBLIME_VERSION = '0.4.0'; |
| 12 | +const SUBLIME_ANTENNA = 'antenna.ayads.co'; |
| 13 | +const SUBLIME_VERSION = '0.5.2'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Debug log message |
| 17 | + * @param {String} msg |
| 18 | + * @param {Object=} obj |
| 19 | + */ |
| 20 | +export function log(msg, obj) { |
| 21 | + utils.logInfo('SublimeBidAdapter - ' + msg, obj); |
| 22 | +} |
| 23 | + |
| 24 | +// Default state |
| 25 | +export const state = { |
| 26 | + zoneId: '', |
| 27 | + transactionId: '' |
| 28 | +}; |
10 | 29 |
|
11 |
| -export const spec = { |
12 |
| - code: BIDDER_CODE, |
13 |
| - gvlid: 114, |
14 |
| - aliases: [], |
| 30 | +/** |
| 31 | + * Set a new state |
| 32 | + * @param {Object} value |
| 33 | + */ |
| 34 | +export function setState(value) { |
| 35 | + Object.assign(state, value); |
| 36 | + log('State has been updated :', state); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Send pixel to our debug endpoint |
| 41 | + * @param {string} eventName - Event name that will be send in the e= query string |
| 42 | + */ |
| 43 | +export function sendEvent(eventName) { |
| 44 | + const ts = Date.now(); |
| 45 | + const eventObject = { |
| 46 | + t: ts, |
| 47 | + tse: ts, |
| 48 | + z: state.zoneId, |
| 49 | + e: eventName, |
| 50 | + src: 'pa', |
| 51 | + puid: state.transactionId, |
| 52 | + trId: state.transactionId, |
| 53 | + ver: SUBLIME_VERSION, |
| 54 | + }; |
| 55 | + |
| 56 | + log('Sending pixel for event: ' + eventName, eventObject); |
| 57 | + |
| 58 | + const queryString = url.formatQS(eventObject); |
| 59 | + utils.triggerPixel('https://' + SUBLIME_ANTENNA + '/?' + queryString); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Determines whether or not the given bid request is valid. |
| 64 | + * |
| 65 | + * @param {BidRequest} bid The bid params to validate. |
| 66 | + * @return {Boolean} True if this is a valid bid, and false otherwise. |
| 67 | + */ |
| 68 | +function isBidRequestValid(bid) { |
| 69 | + return !!Number(bid.params.zoneId); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Make a server request from the list of BidRequests. |
| 74 | + * |
| 75 | + * @param {BidRequest[]} validBidRequests - An array of bids |
| 76 | + * @param {Object} bidderRequest - Info describing the request to the server. |
| 77 | + * @return {ServerRequest|ServerRequest[]} - Info describing the request to the server. |
| 78 | + */ |
| 79 | +function buildRequests(validBidRequests, bidderRequest) { |
| 80 | + const commonPayload = { |
| 81 | + pbav: SUBLIME_VERSION, |
| 82 | + // Current Prebid params |
| 83 | + prebidVersion: '$prebid.version$', |
| 84 | + currencyCode: config.getConfig('currency.adServerCurrency') || DEFAULT_CURRENCY, |
| 85 | + timeout: (typeof bidderRequest === 'object' && !!bidderRequest) ? bidderRequest.timeout : config.getConfig('bidderTimeout'), |
| 86 | + }; |
| 87 | + |
| 88 | + // RefererInfo |
| 89 | + if (bidderRequest && bidderRequest.refererInfo) { |
| 90 | + commonPayload.referer = bidderRequest.refererInfo.referer; |
| 91 | + commonPayload.numIframes = bidderRequest.refererInfo.numIframes; |
| 92 | + } |
| 93 | + // GDPR handling |
| 94 | + if (bidderRequest && bidderRequest.gdprConsent) { |
| 95 | + commonPayload.gdprConsent = bidderRequest.gdprConsent.consentString; |
| 96 | + commonPayload.gdpr = bidderRequest.gdprConsent.gdprApplies; // we're handling the undefined case server side |
| 97 | + } |
| 98 | + |
| 99 | + return validBidRequests.map(bid => { |
| 100 | + const bidHost = bid.params.bidHost || DEFAULT_BID_HOST; |
| 101 | + const protocol = bid.params.protocol || DEFAULT_PROTOCOL; |
| 102 | + |
| 103 | + setState({ |
| 104 | + transactionId: bid.transactionId, |
| 105 | + zoneId: bid.params.zoneId, |
| 106 | + debug: bid.params.debug || false, |
| 107 | + }); |
15 | 108 |
|
16 |
| - /** |
17 |
| - * Determines whether or not the given bid request is valid. |
18 |
| - * |
19 |
| - * @param {BidRequest} bid The bid params to validate. |
20 |
| - * @return boolean True if this is a valid bid, and false otherwise. |
21 |
| - */ |
22 |
| - isBidRequestValid: (bid) => { |
23 |
| - return !!bid.params.zoneId; |
24 |
| - }, |
25 |
| - |
26 |
| - /** |
27 |
| - * Make a server request from the list of BidRequests. |
28 |
| - * |
29 |
| - * @param {BidRequest[]} validBidRequests An array of bids |
30 |
| - * @param {Object} bidderRequest - Info describing the request to the server. |
31 |
| - * @return ServerRequest Info describing the request to the server. |
32 |
| - */ |
33 |
| - buildRequests: (validBidRequests, bidderRequest) => { |
34 |
| - let commonPayload = { |
35 |
| - sublimeVersion: SUBLIME_VERSION, |
36 |
| - // Current Prebid params |
37 |
| - prebidVersion: '$prebid.version$', |
38 |
| - currencyCode: config.getConfig('currency.adServerCurrency') || DEFAULT_CURRENCY, |
39 |
| - timeout: config.getConfig('bidderTimeout'), |
| 109 | + const bidPayload = { |
| 110 | + adUnitCode: bid.adUnitCode, |
| 111 | + auctionId: bid.auctionId, |
| 112 | + bidder: bid.bidder, |
| 113 | + bidderRequestId: bid.bidderRequestId, |
| 114 | + bidRequestsCount: bid.bidRequestsCount, |
| 115 | + requestId: bid.bidId, |
| 116 | + sizes: bid.sizes.map(size => ({ |
| 117 | + w: size[0], |
| 118 | + h: size[1], |
| 119 | + })), |
| 120 | + transactionId: bid.transactionId, |
| 121 | + zoneId: bid.params.zoneId, |
40 | 122 | };
|
41 | 123 |
|
42 |
| - // RefererInfo |
43 |
| - if (bidderRequest && bidderRequest.refererInfo) { |
44 |
| - commonPayload.referer = bidderRequest.refererInfo.referer; |
45 |
| - commonPayload.numIframes = bidderRequest.refererInfo.numIframes; |
| 124 | + const payload = Object.assign({}, commonPayload, bidPayload); |
| 125 | + |
| 126 | + return { |
| 127 | + method: 'POST', |
| 128 | + url: protocol + '://' + bidHost + '/bid', |
| 129 | + data: payload, |
| 130 | + options: { |
| 131 | + contentType: 'application/json', |
| 132 | + withCredentials: true |
| 133 | + }, |
46 | 134 | }
|
47 |
| - // GDPR handling |
48 |
| - if (bidderRequest && bidderRequest.gdprConsent) { |
49 |
| - commonPayload.gdprConsent = bidderRequest.gdprConsent.consentString; |
50 |
| - commonPayload.gdpr = bidderRequest.gdprConsent.gdprApplies; // we're handling the undefined case server side |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Unpack the response from the server into a list of bids. |
| 140 | + * |
| 141 | + * @param {*} serverResponse A successful response from the server. |
| 142 | + * @param {*} bidRequest An object with bid request informations |
| 143 | + * @return {Bid[]} An array of bids which were nested inside the server. |
| 144 | + */ |
| 145 | +function interpretResponse(serverResponse, bidRequest) { |
| 146 | + const bidResponses = []; |
| 147 | + const response = serverResponse.body; |
| 148 | + |
| 149 | + if (response) { |
| 150 | + if (response.timeout || !response.ad || /<!--\s+No\s+ad\s+-->/gmi.test(response.ad)) { |
| 151 | + return bidResponses; |
51 | 152 | }
|
52 | 153 |
|
53 |
| - return validBidRequests.map(bid => { |
54 |
| - let bidPayload = { |
55 |
| - adUnitCode: bid.adUnitCode, |
56 |
| - auctionId: bid.auctionId, |
57 |
| - bidder: bid.bidder, |
58 |
| - bidderRequestId: bid.bidderRequestId, |
59 |
| - bidRequestsCount: bid.bidRequestsCount, |
60 |
| - requestId: bid.bidId, |
61 |
| - sizes: bid.sizes.map(size => ({ |
62 |
| - w: size[0], |
63 |
| - h: size[1], |
64 |
| - })), |
65 |
| - transactionId: bid.transactionId, |
66 |
| - zoneId: bid.params.zoneId, |
67 |
| - }; |
| 154 | + // Setting our returned sizes object to default values |
| 155 | + let returnedSizes = { |
| 156 | + width: 1800, |
| 157 | + height: 1000 |
| 158 | + }; |
68 | 159 |
|
69 |
| - let protocol = bid.params.protocol || DEFAULT_PROTOCOL; |
70 |
| - let bidHost = bid.params.bidHost || DEFAULT_BID_HOST; |
71 |
| - let payload = Object.assign({}, commonPayload, bidPayload); |
72 |
| - |
73 |
| - return { |
74 |
| - method: 'POST', |
75 |
| - url: protocol + '://' + bidHost + '/bid', |
76 |
| - data: payload, |
77 |
| - options: { |
78 |
| - contentType: 'application/json', |
79 |
| - withCredentials: true |
80 |
| - }, |
81 |
| - }; |
82 |
| - }); |
83 |
| - }, |
84 |
| - |
85 |
| - /** |
86 |
| - * Unpack the response from the server into a list of bids. |
87 |
| - * |
88 |
| - * @param {*} serverResponse A successful response from the server. |
89 |
| - * @param {*} bidRequest An object with bid request informations |
90 |
| - * @return {Bid[]} An array of bids which were nested inside the server. |
91 |
| - */ |
92 |
| - interpretResponse: (serverResponse, bidRequest) => { |
93 |
| - const bidResponses = []; |
94 |
| - const response = serverResponse.body; |
95 |
| - |
96 |
| - if (response) { |
97 |
| - if (response.timeout || !response.ad || response.ad.match(/<!-- No ad -->/gmi)) { |
98 |
| - return bidResponses; |
99 |
| - } |
100 |
| - |
101 |
| - // Setting our returned sizes object to default values |
102 |
| - let returnedSizes = { |
103 |
| - width: 1800, |
104 |
| - height: 1000 |
| 160 | + // Verifying Banner sizes |
| 161 | + if (bidRequest && bidRequest.data && bidRequest.data.w === 1 && bidRequest.data.h === 1) { |
| 162 | + // If banner sizes are 1x1 we set our default size object to 1x1 |
| 163 | + returnedSizes = { |
| 164 | + width: 1, |
| 165 | + height: 1 |
105 | 166 | };
|
| 167 | + } |
106 | 168 |
|
107 |
| - // Verifying Banner sizes |
108 |
| - if (bidRequest && bidRequest.data && bidRequest.data.w === 1 && bidRequest.data.h === 1) { |
109 |
| - // If banner sizes are 1x1 we set our default size object to 1x1 |
110 |
| - returnedSizes = { |
111 |
| - width: 1, |
112 |
| - height: 1 |
113 |
| - }; |
114 |
| - } |
115 |
| - |
116 |
| - const bidResponse = { |
117 |
| - requestId: response.requestId || '', |
118 |
| - cpm: response.cpm || 0, |
119 |
| - width: response.width || returnedSizes.width, |
120 |
| - height: response.height || returnedSizes.height, |
121 |
| - creativeId: response.creativeId || 1, |
122 |
| - dealId: response.dealId || 1, |
123 |
| - currency: response.currency || DEFAULT_CURRENCY, |
124 |
| - netRevenue: response.netRevenue || true, |
125 |
| - ttl: response.ttl || DEFAULT_TTL, |
126 |
| - ad: response.ad, |
127 |
| - }; |
| 169 | + const bidResponse = { |
| 170 | + requestId: response.requestId || '', |
| 171 | + cpm: response.cpm || 0, |
| 172 | + width: response.width || returnedSizes.width, |
| 173 | + height: response.height || returnedSizes.height, |
| 174 | + creativeId: response.creativeId || 1, |
| 175 | + dealId: response.dealId || 1, |
| 176 | + currency: response.currency || DEFAULT_CURRENCY, |
| 177 | + netRevenue: response.netRevenue || true, |
| 178 | + ttl: response.ttl || DEFAULT_TTL, |
| 179 | + ad: response.ad, |
| 180 | + pbav: SUBLIME_VERSION |
| 181 | + }; |
128 | 182 |
|
129 |
| - bidResponses.push(bidResponse); |
130 |
| - } |
| 183 | + bidResponses.push(bidResponse); |
| 184 | + } |
| 185 | + |
| 186 | + return bidResponses; |
| 187 | +} |
| 188 | + |
| 189 | +/** |
| 190 | + * Send pixel when bidWon event is triggered |
| 191 | + * @param {Object} timeoutData |
| 192 | + */ |
| 193 | +function onBidWon(bid) { |
| 194 | + log('Bid won', bid); |
| 195 | + sendEvent('bidwon'); |
| 196 | +} |
| 197 | + |
| 198 | +/** |
| 199 | + * Send debug when we timeout |
| 200 | + * @param {Object} timeoutData |
| 201 | + */ |
| 202 | +function onTimeout(timeoutData) { |
| 203 | + log('Timeout from adapter', timeoutData); |
| 204 | + sendEvent('bidtimeout'); |
| 205 | +} |
131 | 206 |
|
132 |
| - return bidResponses; |
133 |
| - }, |
| 207 | +export const spec = { |
| 208 | + code: BIDDER_CODE, |
| 209 | + gvlid: BIDDER_GVLID, |
| 210 | + aliases: [], |
| 211 | + isBidRequestValid: isBidRequestValid, |
| 212 | + buildRequests: buildRequests, |
| 213 | + interpretResponse: interpretResponse, |
| 214 | + onBidWon: onBidWon, |
| 215 | + onTimeout: onTimeout, |
134 | 216 | };
|
135 | 217 |
|
136 | 218 | registerBidder(spec);
|
0 commit comments