|
1 |
| -import { getWindowLocation, deepAccess } from '../src/utils.js'; |
2 |
| -import { registerBidder } from '../src/adapters/bidderFactory.js'; |
3 |
| -import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js'; |
4 |
| -import { convertOrtbRequestToProprietaryNative } from '../src/native.js'; |
| 1 | +import {registerBidder} from '../src/adapters/bidderFactory.js'; |
| 2 | +import {BANNER, VIDEO} from '../src/mediaTypes.js'; |
| 3 | +import {ortbConverter} from '../libraries/ortbConverter/converter.js'; |
| 4 | +import {deepAccess, mergeDeep, convertTypes} from '../src/utils.js'; |
5 | 5 |
|
6 | 6 | const BIDDER_CODE = 'trafficgate';
|
7 |
| -const URL = 'https://[HOST].bc-plugin.com/?c=o&m=multi' |
| 7 | +const URL = 'https://[HOST].bc-plugin.com/prebidjs' |
8 | 8 |
|
9 | 9 | export const spec = {
|
10 | 10 | code: BIDDER_CODE,
|
11 |
| - supportedMediaTypes: [BANNER, VIDEO, NATIVE], |
| 11 | + supportedMediaTypes: [BANNER, VIDEO], |
| 12 | + isBidRequestValid, |
| 13 | + buildRequests, |
| 14 | + interpretResponse, |
| 15 | + transformBidParams, |
| 16 | + isBannerBid |
| 17 | +}; |
12 | 18 |
|
13 |
| - isBidRequestValid: function (bid) { |
14 |
| - return !!(bid.bidId && bid.params && parseInt(bid.params.placementId) && bid.params.host) |
15 |
| - }, |
16 |
| - /** |
17 |
| - * Make a server request from the list of BidRequests. |
18 |
| - * |
19 |
| - * @param {BidRequest[]} validBidRequests A non-empty list of valid bid requests that should be sent to the Server. |
20 |
| - * @return ServerRequest Info describing the request to the server. |
21 |
| - */ |
22 |
| - buildRequests: function (validBidRequests) { |
23 |
| - // convert Native ORTB definition to old-style prebid native definition |
24 |
| - validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests); |
25 |
| - if (validBidRequests && validBidRequests.length === 0) return []; |
26 |
| - |
27 |
| - const location = getWindowLocation() |
28 |
| - const placements = [] |
29 |
| - |
30 |
| - validBidRequests.forEach((bidReq) => { |
31 |
| - placements.push({ |
32 |
| - placementId: bidReq.params.placementId, |
33 |
| - bidId: bidReq.bidId, |
34 |
| - traffic: getMediatype(bidReq) |
35 |
| - }) |
36 |
| - }) |
| 19 | +registerBidder(spec) |
37 | 20 |
|
38 |
| - return { |
39 |
| - method: 'POST', |
40 |
| - url: URL.replace('[HOST]', validBidRequests[0].params.host), |
41 |
| - data: { |
42 |
| - language: (navigator && navigator.language) ? navigator.language : '', |
43 |
| - secure: +(location.protocol === 'https:'), |
44 |
| - host: location.hostname, |
45 |
| - page: location.pathname, |
46 |
| - placements: placements |
| 21 | +const converter = ortbConverter({ |
| 22 | + context: { |
| 23 | + netRevenue: true, |
| 24 | + ttl: 300 |
| 25 | + }, |
| 26 | + imp(buildImp, bidRequest, context) { |
| 27 | + const imp = buildImp(bidRequest, context); |
| 28 | + mergeDeep(imp, { |
| 29 | + ext: { |
| 30 | + bidder: { |
| 31 | + placementId: bidRequest.params.placementId, |
| 32 | + host: bidRequest.params.host |
| 33 | + } |
47 | 34 | }
|
| 35 | + }); |
| 36 | + if (bidRequest.params.customFloor && !imp.bidfloor) { |
| 37 | + imp.bidfloor = bidRequest.params.customFloor; |
48 | 38 | }
|
| 39 | + return imp; |
49 | 40 | },
|
50 |
| - |
51 |
| - interpretResponse: function (opts) { |
52 |
| - const body = opts.body |
53 |
| - const response = [] |
54 |
| - |
55 |
| - for (let i = 0; i < body.length; i++) { |
56 |
| - const item = body[i] |
57 |
| - if (isBidResponseValid(item)) { |
58 |
| - response.push(item) |
| 41 | + request(buildRequest, imps, bidderRequest, context) { |
| 42 | + const req = buildRequest(imps, bidderRequest, context); |
| 43 | + mergeDeep(req, { |
| 44 | + at: 1, |
| 45 | + }) |
| 46 | + const bid = context.bidRequests[0]; |
| 47 | + if (bid.params.test) { |
| 48 | + req.test = 1 |
| 49 | + } |
| 50 | + return req; |
| 51 | + }, |
| 52 | + bidResponse(buildBidResponse, bid, context) { |
| 53 | + const bidResponse = buildBidResponse(bid, context); |
| 54 | + if (bid.ext) { |
| 55 | + bidResponse.meta.networkId = bid.ext.networkId; |
| 56 | + bidResponse.meta.advertiserDomains = bid.ext.advertiserDomains; |
| 57 | + } |
| 58 | + return bidResponse; |
| 59 | + }, |
| 60 | + response(buildResponse, bidResponses, ortbResponse, context) { |
| 61 | + const response = buildResponse(bidResponses, ortbResponse, context); |
| 62 | + return response.bids |
| 63 | + }, |
| 64 | + overrides: { |
| 65 | + imp: { |
| 66 | + bidfloor(setBidFloor, imp, bidRequest, context) { |
| 67 | + const floor = {}; |
| 68 | + setBidFloor(floor, bidRequest, {...context, currency: 'USD'}); |
| 69 | + if (floor.bidfloorcur === 'USD') { |
| 70 | + Object.assign(imp, floor); |
| 71 | + } |
| 72 | + }, |
| 73 | + video(orig, imp, bidRequest, context) { |
| 74 | + if (FEATURES.VIDEO) { |
| 75 | + let videoParams = bidRequest.mediaTypes[VIDEO]; |
| 76 | + if (videoParams) { |
| 77 | + videoParams = Object.assign({}, videoParams, bidRequest.params.video); |
| 78 | + bidRequest = {...bidRequest, mediaTypes: {[VIDEO]: videoParams}} |
| 79 | + } |
| 80 | + orig(imp, bidRequest, context); |
| 81 | + if (imp.video && videoParams?.context === 'outstream') { |
| 82 | + imp.video.placement = imp.video.placement || 4; |
| 83 | + } |
| 84 | + } |
59 | 85 | }
|
60 | 86 | }
|
61 |
| - |
62 |
| - return response |
63 | 87 | }
|
64 |
| -} |
| 88 | +}); |
65 | 89 |
|
66 |
| -registerBidder(spec) |
| 90 | +function transformBidParams(params, isOpenRtb) { |
| 91 | + return convertTypes({ |
| 92 | + 'customFloor': 'number', |
| 93 | + 'placementId': 'number', |
| 94 | + 'host': 'string' |
| 95 | + }, params); |
| 96 | +} |
67 | 97 |
|
68 |
| -function isBidResponseValid (bid) { |
69 |
| - if (!bid.requestId || !bid.cpm || !bid.creativeId || |
70 |
| - !bid.ttl || !bid.currency) { |
| 98 | +function isBidRequestValid(bidRequest) { |
| 99 | + const isValid = bidRequest.params.placementId && bidRequest.params.host; |
| 100 | + if (!isValid) { |
71 | 101 | return false
|
72 | 102 | }
|
73 |
| - switch (bid['mediaType']) { |
74 |
| - case BANNER: |
75 |
| - return Boolean(bid.width && bid.height && bid.ad) |
76 |
| - case VIDEO: |
77 |
| - return Boolean(bid.vastUrl) |
78 |
| - case NATIVE: |
79 |
| - return Boolean(bid.title && bid.image && bid.impressionTrackers) |
80 |
| - default: |
81 |
| - return false |
| 103 | + if (isBannerBid(bidRequest)) { |
| 104 | + return deepAccess(bidRequest, 'mediaTypes.banner.sizes.length') > 0; |
82 | 105 | }
|
| 106 | + return true |
83 | 107 | }
|
84 | 108 |
|
85 |
| -function getMediatype(bidRequest) { |
86 |
| - if (deepAccess(bidRequest, 'mediaTypes.banner')) { |
87 |
| - return BANNER; |
88 |
| - } |
89 |
| - if (deepAccess(bidRequest, 'mediaTypes.video')) { |
90 |
| - return VIDEO; |
| 109 | +function buildRequests(bids, bidderRequest) { |
| 110 | + let videoBids = bids.filter(bid => isVideoBid(bid)); |
| 111 | + let bannerBids = bids.filter(bid => isBannerBid(bid)); |
| 112 | + let requests = bannerBids.length ? [createRequest(bannerBids, bidderRequest, BANNER)] : []; |
| 113 | + videoBids.forEach(bid => { |
| 114 | + requests.push(createRequest([bid], bidderRequest, VIDEO)); |
| 115 | + }); |
| 116 | + return requests; |
| 117 | +} |
| 118 | + |
| 119 | +function createRequest(bidRequests, bidderRequest, mediaType) { |
| 120 | + return { |
| 121 | + method: 'POST', |
| 122 | + url: URL.replace('[HOST]', bidRequests[0].params.host), |
| 123 | + data: converter.toORTB({bidRequests, bidderRequest, context: {mediaType}}) |
91 | 124 | }
|
92 |
| - if (deepAccess(bidRequest, 'mediaTypes.native')) { |
93 |
| - return NATIVE; |
| 125 | +} |
| 126 | + |
| 127 | +function isVideoBid(bid) { |
| 128 | + return !!deepAccess(bid, 'mediaTypes.video'); |
| 129 | +} |
| 130 | + |
| 131 | +function isBannerBid(bid) { |
| 132 | + return !!deepAccess(bid, 'mediaTypes.banner'); |
| 133 | +} |
| 134 | + |
| 135 | +function interpretResponse(resp, req) { |
| 136 | + if (!resp.body) { |
| 137 | + resp.body = {nbr: 0}; |
94 | 138 | }
|
| 139 | + return converter.fromORTB({request: req.data, response: resp.body}); |
| 140 | +} |
| 141 | + |
| 142 | +export const spec2 = { |
| 143 | + code: BIDDER_CODE, |
| 144 | + supportedMediaTypes: [BANNER, VIDEO], |
| 145 | + |
| 146 | + isBidRequestValid: function (bid) { |
| 147 | + return !!(bid.bidId && bid.params && parseInt(bid.params.placementId) && bid.params.host) |
| 148 | + }, |
95 | 149 | }
|
0 commit comments