|
| 1 | +import {registerBidder} from '../src/adapters/bidderFactory'; |
| 2 | +import {config} from '../src/config'; |
| 3 | +import {BANNER, VIDEO} from '../src/mediaTypes'; |
| 4 | +import * as utils from '../src/utils'; |
| 5 | + |
| 6 | +const BIDDER_CODE = 'richaudience'; |
| 7 | + |
| 8 | +export const spec = { |
| 9 | + code: BIDDER_CODE, |
| 10 | + aliases: ['ra'], |
| 11 | + supportedMediaTypes: [BANNER, VIDEO], |
| 12 | + |
| 13 | + /*** |
| 14 | + * Determines whether or not the given bid request is valid |
| 15 | + * |
| 16 | + * @param {bidRequest} bid The bid params to validate. |
| 17 | + * @returns {boolean} True if this is a valid bid, and false otherwise |
| 18 | + */ |
| 19 | + isBidRequestValid: function (bid) { |
| 20 | + return !!(bid.params && bid.params.pid && bid.params.supplyType); |
| 21 | + }, |
| 22 | + /*** |
| 23 | + * Build a server request from the list of valid BidRequests |
| 24 | + * @param {validBidRequests} is an array of the valid bids |
| 25 | + * @param {bidderRequest} bidder request object |
| 26 | + * @returns {ServerRequest} Info describing the request to the server |
| 27 | + */ |
| 28 | + buildRequests: function (validBidRequests, bidderRequest) { |
| 29 | + return validBidRequests.map(bid => { |
| 30 | + var payload = { |
| 31 | + bidfloor: bid.params.bidfloor, |
| 32 | + ifa: bid.params.ifa, |
| 33 | + pid: bid.params.pid, |
| 34 | + supplyType: bid.params.supplyType, |
| 35 | + currencyCode: config.getConfig('currency.adServerCurrency'), |
| 36 | + auctionId: bid.auctionId, |
| 37 | + bidId: bid.bidId, |
| 38 | + BidRequestsCount: bid.bidRequestsCount, |
| 39 | + bidder: bid.bidder, |
| 40 | + bidderRequestId: bid.bidderRequestId, |
| 41 | + tagId: bid.adUnitCode, |
| 42 | + sizes: bid.sizes.map(size => ({ |
| 43 | + w: size[0], |
| 44 | + h: size[1], |
| 45 | + })), |
| 46 | + referer: (typeof bidderRequest.refererInfo.referer != 'undefined' ? encodeURIComponent(bidderRequest.refererInfo.referer) : null), |
| 47 | + numIframes: (typeof bidderRequest.refererInfo.numIframes != 'undefined' ? bidderRequest.refererInfo.numIframes : null), |
| 48 | + transactionId: bid.transactionId, |
| 49 | + timeout: config.getConfig('bidderTimeout'), |
| 50 | + }; |
| 51 | + |
| 52 | + if (bidderRequest && bidderRequest.gdprConsent) { |
| 53 | + payload.gdpr_consent = bidderRequest.gdprConsent.consentString; |
| 54 | + payload.gdpr = bidderRequest.gdprConsent.gdprApplies; // we're handling the undefined case server side |
| 55 | + } else { |
| 56 | + payload.gdpr_consent = ''; |
| 57 | + payload.gdpr = null; |
| 58 | + } |
| 59 | + |
| 60 | + var payloadString = JSON.stringify(payload); |
| 61 | + |
| 62 | + var endpoint = 'https://shb.richaudience.com/hb/'; |
| 63 | + |
| 64 | + return { |
| 65 | + method: 'POST', |
| 66 | + url: endpoint, |
| 67 | + data: payloadString, |
| 68 | + }; |
| 69 | + }); |
| 70 | + }, |
| 71 | + /*** |
| 72 | + * Read the response from the server and build a list of bids |
| 73 | + * @param {serverResponse} Response from the server. |
| 74 | + * @param {bidRequest} Bid request object |
| 75 | + * @returns {bidResponses} Array of bids which were nested inside the server |
| 76 | + */ |
| 77 | + interpretResponse: function (serverResponse, bidRequest) { |
| 78 | + const bidResponses = []; |
| 79 | + |
| 80 | + var response = serverResponse.body; |
| 81 | + |
| 82 | + try { |
| 83 | + if (response) { |
| 84 | + var bidResponse = { |
| 85 | + requestId: JSON.parse(bidRequest.data).bidId, |
| 86 | + cpm: response.cpm, |
| 87 | + width: response.width, |
| 88 | + height: response.height, |
| 89 | + creativeId: response.creative_id, |
| 90 | + mediaType: response.media_type, |
| 91 | + netRevenue: response.netRevenue, |
| 92 | + currency: response.currency, |
| 93 | + ttl: response.ttl, |
| 94 | + }; |
| 95 | + |
| 96 | + if (response.media_type === 'video') { |
| 97 | + bidResponse.vastXml = response.vastXML; |
| 98 | + } else { |
| 99 | + bidResponse.ad = response.adm |
| 100 | + } |
| 101 | + |
| 102 | + bidResponses.push(bidResponse); |
| 103 | + } |
| 104 | + } catch (error) { |
| 105 | + utils.logError('Error while parsing Rich Audience response', error); |
| 106 | + } |
| 107 | + return bidResponses |
| 108 | + }, |
| 109 | + /*** |
| 110 | + * User Syncs |
| 111 | + * |
| 112 | + * @param {syncOptions} Publisher prebid configuration |
| 113 | + * @param {serverResponses} Response from the server |
| 114 | + * @param {gdprConsent} GPDR consent object |
| 115 | + * @returns {Array} |
| 116 | + */ |
| 117 | + getUserSyncs: function (syncOptions, serverResponses, gdprConsent) { |
| 118 | + const syncs = []; |
| 119 | + |
| 120 | + var rand = Math.floor(Math.random() * 9999999999); |
| 121 | + var syncUrl = ''; |
| 122 | + |
| 123 | + if (gdprConsent && typeof gdprConsent.consentString === 'string') { |
| 124 | + syncUrl = 'https://sync.richaudience.com/dcf3528a0b8aa83634892d50e91c306e/?ord=' + rand + '&pubconsent=' + gdprConsent.consentString + '&euconsent=' + gdprConsent.consentString; |
| 125 | + } else { |
| 126 | + syncUrl = 'https://sync.richaudience.com/dcf3528a0b8aa83634892d50e91c306e/?ord=' + rand; |
| 127 | + } |
| 128 | + |
| 129 | + if (syncOptions.iframeEnabled) { |
| 130 | + syncs.push({ |
| 131 | + type: 'iframe', |
| 132 | + url: syncUrl |
| 133 | + }); |
| 134 | + } |
| 135 | + return syncs |
| 136 | + }, |
| 137 | +}; |
| 138 | + |
| 139 | +registerBidder(spec); |
0 commit comments