|
| 1 | +import {isEmpty, parseUrl} from '../src/utils.js'; |
| 2 | +import { registerBidder } from '../src/adapters/bidderFactory.js'; |
| 3 | +import { BANNER } from '../src/mediaTypes.js'; |
| 4 | +import {getStorageManager} from '../src/storageManager.js'; |
| 5 | + |
| 6 | +const NETWORK_ID = 11090; |
| 7 | +const AD_TYPES = [4309, 641]; |
| 8 | +const DTX_TYPES = [5061]; |
| 9 | +const TARGET_NAME = 'inline'; |
| 10 | +const BIDDER_CODE = 'flipp'; |
| 11 | +const ENDPOINT = 'https://gateflipp.flippback.com/flyer-locator-service/client_bidding'; |
| 12 | +const DEFAULT_TTL = 30; |
| 13 | +const DEFAULT_CURRENCY = 'USD'; |
| 14 | +const DEFAULT_CREATIVE_TYPE = 'NativeX'; |
| 15 | +const VALID_CREATIVE_TYPES = ['DTX', 'NativeX']; |
| 16 | +const FLIPP_USER_KEY = 'flipp-uid'; |
| 17 | +const COMPACT_DEFAULT_HEIGHT = 600; |
| 18 | + |
| 19 | +let userKey = null; |
| 20 | +export const storage = getStorageManager({bidderCode: BIDDER_CODE}); |
| 21 | + |
| 22 | +export function getUserKey(options = {}) { |
| 23 | + if (userKey) { |
| 24 | + return userKey; |
| 25 | + } |
| 26 | + |
| 27 | + // If the partner provides the user key use it, otherwise fallback to cookies |
| 28 | + if (options.userKey && isValidUserKey(options.userKey)) { |
| 29 | + userKey = options.userKey; |
| 30 | + return options.userKey; |
| 31 | + } |
| 32 | + // Grab from Cookie |
| 33 | + const foundUserKey = storage.cookiesAreEnabled() && storage.getCookie(FLIPP_USER_KEY); |
| 34 | + if (foundUserKey) { |
| 35 | + return foundUserKey; |
| 36 | + } |
| 37 | + |
| 38 | + // Generate if none found |
| 39 | + userKey = generateUUID(); |
| 40 | + |
| 41 | + // Set cookie |
| 42 | + if (storage.cookiesAreEnabled()) { |
| 43 | + storage.setCookie(FLIPP_USER_KEY, userKey); |
| 44 | + } |
| 45 | + |
| 46 | + return userKey; |
| 47 | +} |
| 48 | + |
| 49 | +function isValidUserKey(userKey) { |
| 50 | + return !userKey.startsWith('#'); |
| 51 | +} |
| 52 | + |
| 53 | +const generateUUID = () => { |
| 54 | + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { |
| 55 | + const r = (Math.random() * 16) | 0; |
| 56 | + const v = c === 'x' ? r : (r & 0x3) | 0x8; |
| 57 | + return v.toString(16); |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * Determines if a creativeType is valid |
| 63 | + * |
| 64 | + * @param {string} creativeType The Creative Type to validate. |
| 65 | + * @return string creativeType if this is a valid Creative Type, and 'NativeX' otherwise. |
| 66 | + */ |
| 67 | +const validateCreativeType = (creativeType) => { |
| 68 | + if (creativeType && VALID_CREATIVE_TYPES.includes(creativeType)) { |
| 69 | + return creativeType; |
| 70 | + } else { |
| 71 | + return DEFAULT_CREATIVE_TYPE; |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +const getAdTypes = (creativeType) => { |
| 76 | + if (creativeType === 'DTX') { |
| 77 | + return DTX_TYPES; |
| 78 | + } |
| 79 | + return AD_TYPES; |
| 80 | +} |
| 81 | + |
| 82 | +export const spec = { |
| 83 | + code: BIDDER_CODE, |
| 84 | + supportedMediaTypes: [BANNER], |
| 85 | + /** |
| 86 | + * Determines whether or not the given bid request is valid. |
| 87 | + * |
| 88 | + * @param {BidRequest} bid The bid params to validate. |
| 89 | + * @return boolean True if this is a valid bid, and false otherwise. |
| 90 | + */ |
| 91 | + isBidRequestValid: function(bid) { |
| 92 | + return !!(bid.params.siteId) && !!(bid.params.publisherNameIdentifier); |
| 93 | + }, |
| 94 | + /** |
| 95 | + * Make a server request from the list of BidRequests. |
| 96 | + * |
| 97 | + * @param {BidRequest[]} validBidRequests[] an array of bids |
| 98 | + * @param {BidderRequest} bidderRequest master bidRequest object |
| 99 | + * @return ServerRequest Info describing the request to the server. |
| 100 | + */ |
| 101 | + buildRequests: function(validBidRequests, bidderRequest) { |
| 102 | + const urlParams = parseUrl(bidderRequest.refererInfo.page).search; |
| 103 | + const contentCode = urlParams['flipp-content-code']; |
| 104 | + const userKey = getUserKey(validBidRequests[0]?.params); |
| 105 | + const placements = validBidRequests.map((bid, index) => { |
| 106 | + const options = bid.params.options || {}; |
| 107 | + if (!options.hasOwnProperty('startCompact')) { |
| 108 | + options.startCompact = true; |
| 109 | + } |
| 110 | + return { |
| 111 | + divName: TARGET_NAME, |
| 112 | + networkId: NETWORK_ID, |
| 113 | + siteId: bid.params.siteId, |
| 114 | + adTypes: getAdTypes(bid.params.creativeType), |
| 115 | + count: 1, |
| 116 | + ...(!isEmpty(bid.params.zoneIds) && {zoneIds: bid.params.zoneIds}), |
| 117 | + properties: { |
| 118 | + ...(!isEmpty(contentCode) && {contentCode: contentCode.slice(0, 32)}), |
| 119 | + }, |
| 120 | + options, |
| 121 | + prebid: { |
| 122 | + requestId: bid.bidId, |
| 123 | + publisherNameIdentifier: bid.params.publisherNameIdentifier, |
| 124 | + height: bid.mediaTypes.banner.sizes[index][0], |
| 125 | + width: bid.mediaTypes.banner.sizes[index][1], |
| 126 | + creativeType: validateCreativeType(bid.params.creativeType), |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + return { |
| 131 | + method: 'POST', |
| 132 | + url: ENDPOINT, |
| 133 | + data: { |
| 134 | + placements, |
| 135 | + url: bidderRequest.refererInfo.page, |
| 136 | + user: { |
| 137 | + key: userKey, |
| 138 | + }, |
| 139 | + }, |
| 140 | + } |
| 141 | + }, |
| 142 | + /** |
| 143 | + * Unpack the response from the server into a list of bids. |
| 144 | + * |
| 145 | + * @param {ServerResponse} serverResponse A successful response from the server. |
| 146 | + * @param {BidRequest} bidRequest A bid request object |
| 147 | + * @return {Bid[]} An array of bids which were nested inside the server. |
| 148 | + */ |
| 149 | + interpretResponse: function(serverResponse, bidRequest) { |
| 150 | + if (!serverResponse?.body) return []; |
| 151 | + const placements = bidRequest.data.placements; |
| 152 | + const res = serverResponse.body; |
| 153 | + if (!isEmpty(res) && !isEmpty(res.decisions) && !isEmpty(res.decisions.inline)) { |
| 154 | + return res.decisions.inline.map(decision => { |
| 155 | + const placement = placements.find(p => p.prebid.requestId === decision.prebid?.requestId); |
| 156 | + const height = placement.options?.startCompact ? COMPACT_DEFAULT_HEIGHT : decision.height; |
| 157 | + return { |
| 158 | + bidderCode: BIDDER_CODE, |
| 159 | + requestId: decision.prebid?.requestId, |
| 160 | + cpm: decision.prebid?.cpm, |
| 161 | + width: decision.width, |
| 162 | + height, |
| 163 | + creativeId: decision.adId, |
| 164 | + currency: DEFAULT_CURRENCY, |
| 165 | + netRevenue: true, |
| 166 | + ttl: DEFAULT_TTL, |
| 167 | + ad: decision.prebid?.creative, |
| 168 | + } |
| 169 | + }); |
| 170 | + } |
| 171 | + return []; |
| 172 | + }, |
| 173 | + |
| 174 | + /** |
| 175 | + * Register the user sync pixels which should be dropped after the auction. |
| 176 | + * |
| 177 | + * @param {SyncOptions} syncOptions Which user syncs are allowed? |
| 178 | + * @param {ServerResponse[]} serverResponses List of server's responses. |
| 179 | + * @return {UserSync[]} The user syncs which should be dropped. |
| 180 | + */ |
| 181 | + getUserSyncs: (syncOptions, serverResponses) => [], |
| 182 | +} |
| 183 | +registerBidder(spec); |
0 commit comments