From 8f9e6c60dcfb43d42c6c8e80599456e8c4699fa8 Mon Sep 17 00:00:00 2001 From: Peter Damoc Date: Tue, 26 Sep 2023 13:16:56 +0300 Subject: [PATCH] Missena: floor implementation --- modules/missenaBidAdapter.js | 120 ++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 15 deletions(-) diff --git a/modules/missenaBidAdapter.js b/modules/missenaBidAdapter.js index 33fa6857e85..7d497dd7f51 100644 --- a/modules/missenaBidAdapter.js +++ b/modules/missenaBidAdapter.js @@ -1,5 +1,11 @@ -import { buildUrl, formatQS, logInfo, triggerPixel } from '../src/utils.js'; -import { BANNER } from '../src/mediaTypes.js'; +import { + isFn, + deepAccess, + formatQS, + logInfo, + parseSizesInput, +} from '../src/utils.js'; +import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js'; import { registerBidder } from '../src/adapters/bidderFactory.js'; const BIDDER_CODE = 'missena'; @@ -7,6 +13,70 @@ const ENDPOINT_URL = 'https://bid.missena.io/'; const EVENTS_DOMAIN = 'events.missena.io'; const EVENTS_DOMAIN_DEV = 'events.staging.missena.xyz'; +/* Get mediatype from bidRequest */ +function getMediatype(bidRequest) { + if (deepAccess(bidRequest, 'mediaTypes.banner')) { + return BANNER; + } + if (deepAccess(bidRequest, 'mediaTypes.video')) { + return VIDEO; + } + if (deepAccess(bidRequest, 'mediaTypes.native')) { + return NATIVE; + } +} + +function getSize(sizesArray) { + const firstSize = sizesArray[0]; + if (typeof firstSize !== 'string') return {}; + + const [widthStr, heightStr] = firstSize.toUpperCase().split('X'); + return { + width: parseInt(widthStr, 10) || undefined, + height: parseInt(heightStr, 10) || undefined, + }; +} + +/* Get Floor price information */ +function getFloor(bidRequest, size, mediaType) { + if (!isFn(bidRequest.getFloor)) { + return deepAccess(bidRequest, 'params.bidfloor', 0); + } + + const bidFloors = bidRequest.getFloor({ + currency: 'USD', + mediaType, + size: [size.width, size.height], + }); + + if (!isNaN(bidFloors.floor)) { + return bidFloors; + } +} + +function getSizeArray(bid) { + let inputSize = deepAccess(bid, 'mediaTypes.banner.sizes') || bid.sizes || []; + + if (Array.isArray(bid.params?.size)) { + inputSize = !Array.isArray(bid.params.size[0]) + ? [bid.params.size] + : bid.params.size; + } + + return parseSizesInput(inputSize); +} + +function notify(bid, event) { + const hostname = bid.params[0].baseUrl ? EVENTS_DOMAIN_DEV : EVENTS_DOMAIN; + const headers = { + type: 'application/json', + }; + const blob = new Blob([JSON.stringify(event)], headers); + navigator.sendBeacon( + `https://${hostname}/v1/events?t=${bid.params[0].apiKey}`, + blob, + ); +} export const spec = { aliases: ['msna'], code: BIDDER_CODE, @@ -61,6 +131,13 @@ export const spec = { payload.is_internal = bidRequest.params.isInternal; } payload.userEids = bidRequest.userIdAsEids || []; + + let mediatype = getMediatype(bidRequest); + let sizesArray = getSizeArray(bidRequest); + let size = getSize(sizesArray); + + payload.prebidFloor = getFloor(bidRequest, size, mediatype); + return { method: 'POST', url: baseUrl + '?' + formatQS({ t: bidRequest.params.apiKey }), @@ -89,7 +166,7 @@ export const spec = { syncOptions, serverResponses, gdprConsent, - uspConsent + uspConsent, ) { if (!syncOptions.iframeEnabled) { return []; @@ -113,24 +190,37 @@ export const spec = { * Register bidder specific code, which will execute if bidder timed out after an auction * @param {data} Containing timeout specific data */ - onTimeout: function onTimeout(timeoutData) { - logInfo('Missena - Timeout from adapter', timeoutData); + onTimeout: (data) => { + data.forEach((bid) => { + notify(bid, { + name: 'timeout', + parameters: { + bidder: BIDDER_CODE, + placement: bid.params[0].placement, + t: bid.params[0].apiKey, + }, + }); + }); + logInfo('Missena - Timeout from adapter', data); }, /** * Register bidder specific code, which@ will execute if a bid from this bidder won the auction * @param {Bid} The bid that won the auction */ - onBidWon: function (bid) { - const hostname = bid.params[0].baseUrl ? EVENTS_DOMAIN_DEV : EVENTS_DOMAIN; - triggerPixel( - buildUrl({ - protocol: 'https', - hostname, - pathname: '/v1/bidsuccess', - search: { t: bid.params[0].apiKey, provider: bid.meta?.networkName, cpm: bid.cpm, currency: bid.currency }, - }) - ); + onBidWon: (bid) => { + notify(bid, { + name: 'bidsuccess', + provider: bid.meta?.networkName, + parameters: { + t: bid.params[0].apiKey, + placement: bid.params[0].placement, + commission: { + value: bid.cpm, + currency: bid.currency, + }, + }, + }); logInfo('Missena - Bid won', bid); }, };