|
| 1 | +import { registerBidder } from '../src/adapters/bidderFactory.js'; |
| 2 | +import { deepSetValue, isPlainObject, logWarn } from '../src/utils.js'; |
| 3 | +import { BANNER } from '../src/mediaTypes.js'; |
| 4 | +import { ortbConverter } from '../libraries/ortbConverter/converter.js'; |
| 5 | + |
| 6 | +const BIDDER_CODE = 'blockthrough'; |
| 7 | +const GVLID = 815; |
| 8 | +const ENDPOINT_URL = 'https://pbs.btloader.com/openrtb2/auction'; |
| 9 | +const SYNC_URL = 'https://cdn.btloader.com/user_sync.html'; |
| 10 | + |
| 11 | +const CONVERTER = ortbConverter({ |
| 12 | + context: { |
| 13 | + netRevenue: true, |
| 14 | + ttl: 60, |
| 15 | + }, |
| 16 | + imp, |
| 17 | + request, |
| 18 | + bidResponse, |
| 19 | +}); |
| 20 | + |
| 21 | +/** |
| 22 | + * Builds an impression object for the ORTB 2.5 request. |
| 23 | + * |
| 24 | + * @param {function} buildImp - The function for building an imp object. |
| 25 | + * @param {Object} bidRequest - The bid request object. |
| 26 | + * @param {Object} context - The context object. |
| 27 | + * @returns {Object} The ORTB 2.5 imp object. |
| 28 | + */ |
| 29 | +function imp(buildImp, bidRequest, context) { |
| 30 | + const imp = buildImp(bidRequest, context); |
| 31 | + const { params, ortb2Imp } = bidRequest; |
| 32 | + |
| 33 | + if (params) { |
| 34 | + deepSetValue(imp, 'ext', params); |
| 35 | + } |
| 36 | + if (ortb2Imp?.ext?.gpid) { |
| 37 | + deepSetValue(imp, 'ext.gpid', ortb2Imp.ext.gpid); |
| 38 | + } |
| 39 | + |
| 40 | + return imp; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Builds a request object for the ORTB 2.5 request. |
| 45 | + * |
| 46 | + * @param {function} buildRequest - The function for building a request object. |
| 47 | + * @param {Array} imps - An array of ORTB 2.5 impression objects. |
| 48 | + * @param {Object} bidderRequest - The bidder request object. |
| 49 | + * @param {Object} context - The context object. |
| 50 | + * @returns {Object} The ORTB 2.5 request object. |
| 51 | + */ |
| 52 | +function request(buildRequest, imps, bidderRequest, context) { |
| 53 | + const request = buildRequest(imps, bidderRequest, context); |
| 54 | + deepSetValue(request, 'ext.prebid.channel', { |
| 55 | + name: 'pbjs', |
| 56 | + version: '$prebid.version$', |
| 57 | + }); |
| 58 | + |
| 59 | + if (window.location.href.includes('btServerTest=true')) { |
| 60 | + request.test = 1; |
| 61 | + } |
| 62 | + |
| 63 | + return request; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Processes a bid response using the provided build function, bid, and context. |
| 68 | + * |
| 69 | + * @param {Function} buildBidResponse - The function to build the bid response. |
| 70 | + * @param {Object} bid - The bid object to include in the bid response. |
| 71 | + * @param {Object} context - The context object containing additional information. |
| 72 | + * @returns {Object} - The processed bid response. |
| 73 | + */ |
| 74 | +function bidResponse(buildBidResponse, bid, context) { |
| 75 | + const bidResponse = buildBidResponse(bid, context); |
| 76 | + const { seat } = context.seatbid || {}; |
| 77 | + bidResponse.btBidderCode = seat; |
| 78 | + |
| 79 | + return bidResponse; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Checks if a bid request is valid. |
| 84 | + * |
| 85 | + * @param {Object} bid - The bid request object. |
| 86 | + * @returns {boolean} True if the bid request is valid, false otherwise. |
| 87 | + */ |
| 88 | +function isBidRequestValid(bid) { |
| 89 | + if (!isPlainObject(bid.params) || !Object.keys(bid.params).length) { |
| 90 | + logWarn('BT Bid Adapter: bid params must be provided.'); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + return true; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Builds the bid requests for the BT Service. |
| 99 | + * |
| 100 | + * @param {Array} validBidRequests - An array of valid bid request objects. |
| 101 | + * @param {Object} bidderRequest - The bidder request object. |
| 102 | + * @returns {Array} An array of BT Service bid requests. |
| 103 | + */ |
| 104 | +function buildRequests(validBidRequests, bidderRequest) { |
| 105 | + const data = CONVERTER.toORTB({ |
| 106 | + bidRequests: validBidRequests, |
| 107 | + bidderRequest, |
| 108 | + }); |
| 109 | + |
| 110 | + return [ |
| 111 | + { |
| 112 | + method: 'POST', |
| 113 | + url: ENDPOINT_URL, |
| 114 | + data, |
| 115 | + bids: validBidRequests, |
| 116 | + }, |
| 117 | + ]; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Interprets the server response and maps it to bids. |
| 122 | + * |
| 123 | + * @param {Object} serverResponse - The server response object. |
| 124 | + * @param {Object} request - The request object. |
| 125 | + * @returns {Array} An array of bid objects. |
| 126 | + */ |
| 127 | +function interpretResponse(serverResponse, request) { |
| 128 | + if (!serverResponse || !request) { |
| 129 | + return []; |
| 130 | + } |
| 131 | + |
| 132 | + return CONVERTER.fromORTB({ |
| 133 | + response: serverResponse.body, |
| 134 | + request: request.data, |
| 135 | + }).bids; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Generates user synchronization data based on provided options and consents. |
| 140 | + * |
| 141 | + * @param {Object} syncOptions - Synchronization options. |
| 142 | + * @param {Object[]} serverResponses - An array of server responses. |
| 143 | + * @param {Object} gdprConsent - GDPR consent information. |
| 144 | + * @param {string} uspConsent - US Privacy consent string. |
| 145 | + * @param {Object} gppConsent - Google Publisher Policies (GPP) consent information. |
| 146 | + * @returns {Object[]} An array of user synchronization objects. |
| 147 | + */ |
| 148 | +function getUserSyncs( |
| 149 | + syncOptions, |
| 150 | + serverResponses, |
| 151 | + gdprConsent, |
| 152 | + uspConsent, |
| 153 | + gppConsent |
| 154 | +) { |
| 155 | + if (!syncOptions.iframeEnabled || !serverResponses?.length) { |
| 156 | + return []; |
| 157 | + } |
| 158 | + |
| 159 | + const bidderCodes = new Set(); |
| 160 | + serverResponses.forEach((serverResponse) => { |
| 161 | + if (serverResponse?.body?.ext?.responsetimemillis) { |
| 162 | + Object.keys(serverResponse.body.ext.responsetimemillis).forEach( |
| 163 | + bidderCodes.add, |
| 164 | + bidderCodes |
| 165 | + ); |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + if (!bidderCodes.size) { |
| 170 | + return []; |
| 171 | + } |
| 172 | + |
| 173 | + const syncs = []; |
| 174 | + const syncUrl = new URL(SYNC_URL); |
| 175 | + syncUrl.searchParams.set('bidders', [...bidderCodes].join(',')); |
| 176 | + |
| 177 | + if (gdprConsent) { |
| 178 | + syncUrl.searchParams.set('gdpr', Number(gdprConsent.gdprApplies)); |
| 179 | + syncUrl.searchParams.set('gdpr_consent', gdprConsent.consentString); |
| 180 | + } |
| 181 | + if (gppConsent) { |
| 182 | + syncUrl.searchParams.set('gpp', gppConsent.gppString); |
| 183 | + syncUrl.searchParams.set('gpp_sid', gppConsent.applicableSections); |
| 184 | + } |
| 185 | + if (uspConsent) { |
| 186 | + syncUrl.searchParams.set('us_privacy', uspConsent); |
| 187 | + } |
| 188 | + |
| 189 | + syncs.push({ type: 'iframe', url: syncUrl.href }); |
| 190 | + |
| 191 | + return syncs; |
| 192 | +} |
| 193 | + |
| 194 | +export const spec = { |
| 195 | + code: BIDDER_CODE, |
| 196 | + gvlid: GVLID, |
| 197 | + supportedMediaTypes: [BANNER], |
| 198 | + isBidRequestValid, |
| 199 | + buildRequests, |
| 200 | + interpretResponse, |
| 201 | + getUserSyncs, |
| 202 | +}; |
| 203 | + |
| 204 | +registerBidder(spec); |
0 commit comments