|
| 1 | +import { registerBidder } from 'src/adapters/bidderFactory'; |
| 2 | +import { BANNER } from 'src/mediaTypes'; |
| 3 | + |
| 4 | +const BIDDER_CODE = 'microad'; |
| 5 | + |
| 6 | +const ENDPOINT_URLS = { |
| 7 | + 'production': '//s-rtb-pb.send.microad.jp/prebid', |
| 8 | + 'test': 'https://rtbtest.send.microad.jp/prebid' |
| 9 | +}; |
| 10 | +export let ENVIRONMENT = 'production'; |
| 11 | + |
| 12 | +/* eslint-disable no-template-curly-in-string */ |
| 13 | +const EXT_URL_STRING = '${COMPASS_EXT_URL}'; |
| 14 | +const EXT_REF_STRING = '${COMPASS_EXT_REF}'; |
| 15 | +const EXT_IFA_STRING = '${COMPASS_EXT_IFA}'; |
| 16 | +const EXT_APPID_STRING = '${COMPASS_EXT_APPID}'; |
| 17 | +const EXT_GEO_STRING = '${COMPASS_EXT_GEO}'; |
| 18 | +/* eslint-enable no-template-curly-in-string */ |
| 19 | + |
| 20 | +const BANNER_CODE = 1; |
| 21 | +const NATIVE_CODE = 2; |
| 22 | +const VIDEO_CODE = 4; |
| 23 | + |
| 24 | +function createCBT() { |
| 25 | + const randomValue = Math.floor(Math.random() * Math.pow(10, 18)).toString(16); |
| 26 | + const date = new Date().getTime().toString(16); |
| 27 | + return randomValue + date; |
| 28 | +} |
| 29 | + |
| 30 | +function createBitSequenceFromMediaType(hi, code) { |
| 31 | + return (hi ? -1 : 0) & code; |
| 32 | +} |
| 33 | + |
| 34 | +function convertMediaTypes(bid) { |
| 35 | + return createBitSequenceFromMediaType(bid.mediaTypes.banner, BANNER_CODE) | |
| 36 | + createBitSequenceFromMediaType(bid.mediaTypes.native, NATIVE_CODE) | |
| 37 | + createBitSequenceFromMediaType(bid.mediaTypes.video, VIDEO_CODE); |
| 38 | +} |
| 39 | + |
| 40 | +export const spec = { |
| 41 | + code: BIDDER_CODE, |
| 42 | + supportedMediaTypes: [BANNER], |
| 43 | + isBidRequestValid: function(bid) { |
| 44 | + return !!(bid && bid.params && bid.params.spot && bid.mediaTypes && (bid.mediaTypes.banner || bid.mediaTypes.native || bid.mediaTypes.video)); |
| 45 | + }, |
| 46 | + buildRequests: function(validBidRequests, bidderRequest) { |
| 47 | + const requests = []; |
| 48 | + |
| 49 | + validBidRequests.forEach(bid => { |
| 50 | + const bidParams = bid.params; |
| 51 | + const params = { |
| 52 | + spot: bidParams.spot, |
| 53 | + url: bidderRequest.refererInfo.canonicalUrl || window.location.href, |
| 54 | + referrer: bidderRequest.refererInfo.referer, |
| 55 | + bid_id: bid.bidId, |
| 56 | + transaction_id: bid.transactionId, |
| 57 | + media_types: convertMediaTypes(bid), |
| 58 | + cbt: createCBT() |
| 59 | + }; |
| 60 | + |
| 61 | + if (bidParams.url) { |
| 62 | + params['url_macro'] = bidParams.url.replace(EXT_URL_STRING, ''); |
| 63 | + } |
| 64 | + |
| 65 | + if (bidParams.referrer) { |
| 66 | + params['referrer_macro'] = bidParams.referrer.replace(EXT_REF_STRING, ''); |
| 67 | + } |
| 68 | + |
| 69 | + if (bidParams.ifa) { |
| 70 | + params['ifa'] = bidParams.ifa.replace(EXT_IFA_STRING, ''); |
| 71 | + } |
| 72 | + |
| 73 | + if (bidParams.appid) { |
| 74 | + params['appid'] = bidParams.appid.replace(EXT_APPID_STRING, ''); |
| 75 | + } |
| 76 | + |
| 77 | + if (bidParams.geo) { |
| 78 | + const geo = bidParams.geo.replace(EXT_GEO_STRING, ''); |
| 79 | + if (/^[0-9.\-]+,[0-9.\-]+$/.test(geo)) { |
| 80 | + params['geo'] = geo; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + requests.push({ |
| 85 | + method: 'GET', |
| 86 | + url: ENDPOINT_URLS[ENVIRONMENT], |
| 87 | + data: params, |
| 88 | + options: { Accept: 'application/json' } |
| 89 | + }); |
| 90 | + }); |
| 91 | + return requests; |
| 92 | + }, |
| 93 | + interpretResponse: function(serverResponse) { |
| 94 | + const body = serverResponse.body; |
| 95 | + const bidResponses = []; |
| 96 | + |
| 97 | + if (body.cpm && body.cpm > 0) { |
| 98 | + const bidResponse = { |
| 99 | + requestId: body.requestId, |
| 100 | + cpm: body.cpm, |
| 101 | + width: body.width, |
| 102 | + height: body.height, |
| 103 | + ad: body.ad, |
| 104 | + ttl: body.ttl, |
| 105 | + creativeId: body.creativeId, |
| 106 | + netRevenue: body.netRevenue, |
| 107 | + currency: body.currency, |
| 108 | + }; |
| 109 | + |
| 110 | + if (body.dealId) { |
| 111 | + bidResponse['dealId'] = body.dealId; |
| 112 | + } |
| 113 | + |
| 114 | + bidResponses.push(bidResponse); |
| 115 | + } |
| 116 | + |
| 117 | + return bidResponses; |
| 118 | + }, |
| 119 | + getUserSyncs: function(syncOptions, serverResponses) { |
| 120 | + const syncs = []; |
| 121 | + |
| 122 | + if (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled) { |
| 123 | + return syncs; |
| 124 | + } |
| 125 | + |
| 126 | + serverResponses.forEach(resp => { |
| 127 | + const syncIframeUrls = resp.body.syncUrls.iframe; |
| 128 | + const syncImageUrls = resp.body.syncUrls.image; |
| 129 | + if (syncOptions.iframeEnabled && syncIframeUrls) { |
| 130 | + syncIframeUrls.forEach(syncIframeUrl => { |
| 131 | + syncs.push({ |
| 132 | + type: 'iframe', |
| 133 | + url: syncIframeUrl |
| 134 | + }); |
| 135 | + }); |
| 136 | + } |
| 137 | + if (syncOptions.pixelEnabled && syncImageUrls) { |
| 138 | + syncImageUrls.forEach(syncImageUrl => { |
| 139 | + syncs.push({ |
| 140 | + type: 'image', |
| 141 | + url: syncImageUrl |
| 142 | + }); |
| 143 | + }); |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + return syncs; |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +registerBidder(spec); |
0 commit comments