From 3c797b120c8e0fe2b851381300ac5c4b1f92c6e2 Mon Sep 17 00:00:00 2001 From: Fatih Kaya Date: Sat, 5 Nov 2022 12:01:03 +0300 Subject: [PATCH] Admatic adapter: host param control && Add new Bidder --- modules/admaticBidAdapter.js | 80 ++++++++++++++++++--- modules/admaticBidAdapter.md | 4 +- src/adloader.js | 1 + test/spec/modules/admaticBidAdapter_spec.js | 6 +- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/modules/admaticBidAdapter.js b/modules/admaticBidAdapter.js index 039f27e5de0..cf54557c6c8 100644 --- a/modules/admaticBidAdapter.js +++ b/modules/admaticBidAdapter.js @@ -1,13 +1,14 @@ import { getValue, logError, deepAccess, getBidIdParameter, isArray } from '../src/utils.js'; import { loadExternalScript } from '../src/adloader.js'; -import {registerBidder} from '../src/adapters/bidderFactory.js'; - -const ENDPOINT_URL = 'https://layer.serve.admatic.com.tr/pb'; +import { registerBidder } from '../src/adapters/bidderFactory.js'; +import { BANNER, VIDEO } from '../src/mediaTypes.js'; const SYNC_URL = 'https://cdn.serve.admatic.com.tr/showad/sync.js'; -const BIDDER_CODE = 'admatic'; export const spec = { code: 'admatic', + aliases: [ + {code: 'pixad'} + ], supportedMediaTypes: ['video', 'banner'], /** * Determines whether or not the given bid request is valid. @@ -19,11 +20,12 @@ export const spec = { let isValid = false; if (typeof bid.params !== 'undefined') { let isValidNetworkId = _validateId(getValue(bid.params, 'networkId')); - isValid = isValidNetworkId;// && isValidTypeId; + let isValidHost = _validateString(getValue(bid.params, 'host')); + isValid = isValidNetworkId && isValidHost; } if (!isValid) { - logError('AdMatic networkId parameters are required. Bid aborted.'); + logError(`${bid.bidder} networkId and host parameters are required. Bid aborted.`); } return isValid; }, @@ -35,11 +37,13 @@ export const spec = { */ buildRequests: function(validBidRequests, bidderRequest) { const bids = validBidRequests.map(buildRequestObject); + const bidderName = validBidRequests[0].bidder; const networkId = getValue(validBidRequests[0].params, 'networkId'); + const host = getValue(validBidRequests[0].params, 'host'); const currency = getValue(validBidRequests[0].params, 'currency') || 'TRY'; setTimeout(() => { - loadExternalScript(SYNC_URL, BIDDER_CODE); + loadExternalScript(SYNC_URL, bidderName); }, bidderRequest.timeout); const payload = { @@ -58,14 +62,14 @@ export const spec = { imp: bids, ext: { 'cur': currency, - 'type': 'admatic' + 'bidder': bidderName } }; const payloadString = JSON.stringify(payload); return { method: 'POST', - url: ENDPOINT_URL, + url: `https://${host}/pb?bidder=${bidderName}`, data: payloadString, options: { contentType: 'application/json' @@ -96,10 +100,11 @@ export const spec = { advertiserDomains: bid && bid.adomain ? bid.adomain : [] }, ttl: 360, - bidder: 'admatic', + bidder: JSON.parse(request.data).ext.bidder, timeToRespond: 1, requestTimestamp: 1 }; + bidResponses.push(resbid); }); }; @@ -107,11 +112,60 @@ export const spec = { } }; +registerBidder(spec); + +function enrichSlotWithFloors(slot, bidRequest) { + try { + const slotFloors = {}; + + if (bidRequest.getFloor) { + if (bidRequest.mediaTypes?.banner) { + slotFloors.banner = {}; + const bannerSizes = parseSizes(deepAccess(bidRequest, 'mediaTypes.banner.sizes')) + bannerSizes.forEach(bannerSize => slotFloors.banner[parseSize(bannerSize).toString()] = bidRequest.getFloor({ size: bannerSize, mediaType: BANNER })); + } + + if (bidRequest.mediaTypes?.video) { + slotFloors.video = {}; + const videoSizes = parseSizes(deepAccess(bidRequest, 'mediaTypes.video.playerSize')) + videoSizes.forEach(videoSize => slotFloors.video[parseSize(videoSize).toString()] = bidRequest.getFloor({ size: videoSize, mediaType: VIDEO })); + } + + if (Object.keys(slotFloors).length > 0) { + if (!slot) { + slot = {} + } + Object.assign(slot, { + floors: slotFloors + }); + } + } + } catch (e) { + logError('Could not parse floors from Prebid: ' + e); + } +} + +function parseSizes(sizes, parser = s => s) { + if (sizes == undefined) { + return []; + } + if (Array.isArray(sizes[0])) { // is there several sizes ? (ie. [[728,90],[200,300]]) + return sizes.map(size => parser(size)); + } + return [parser(sizes)]; // or a single one ? (ie. [728,90]) +} + +function parseSize(size) { + return size[0] + 'x' + size[1]; +} + function buildRequestObject(bid) { const reqObj = {}; reqObj.size = getSizes(bid); reqObj.id = getBidIdParameter('bidId', bid); - reqObj.floor = getValue(bid.params, 'floor') || 0.01; + + enrichSlotWithFloors(reqObj, bid); + return reqObj; } @@ -144,4 +198,8 @@ function _validateId(id) { return (parseInt(id) > 0); } +function _validateString(str) { + return (typeof str == 'string'); +} + registerBidder(spec); diff --git a/modules/admaticBidAdapter.md b/modules/admaticBidAdapter.md index de84998d0af..2bf9afb3cdc 100644 --- a/modules/admaticBidAdapter.md +++ b/modules/admaticBidAdapter.md @@ -19,7 +19,7 @@ Use `admatic` as bidder. bidder: 'admatic', params: { networkId: 12345, - floor: 0.5 + host: 'layer.serve.admatic.com.tr' } }] },{ @@ -29,7 +29,7 @@ Use `admatic` as bidder. bidder: 'admatic', params: { networkId: 12345, - floor: 0.5 + host: 'layer.serve.admatic.com.tr' } }] }]; diff --git a/src/adloader.js b/src/adloader.js index add5863ddf2..167ffb75476 100644 --- a/src/adloader.js +++ b/src/adloader.js @@ -7,6 +7,7 @@ const _approvedLoadExternalJSList = [ 'debugging', 'adloox', 'admatic', + 'pixad', 'criteo', 'outstream', 'adagio', diff --git a/test/spec/modules/admaticBidAdapter_spec.js b/test/spec/modules/admaticBidAdapter_spec.js index c7d391cfaca..9cbee2fb06b 100644 --- a/test/spec/modules/admaticBidAdapter_spec.js +++ b/test/spec/modules/admaticBidAdapter_spec.js @@ -18,7 +18,8 @@ describe('admaticBidAdapter', () => { let bid = { 'bidder': 'admatic', 'params': { - 'networkId': 10433394 + 'networkId': 10433394, + 'host': 'layer.serve.admatic.com.tr' }, 'adUnitCode': 'adunit-code', 'sizes': [[300, 250], [300, 600]], @@ -37,7 +38,8 @@ describe('admaticBidAdapter', () => { delete bid.params; bid.params = { - 'networkId': 0 + 'networkId': 0, + 'host': 'layer.serve.admatic.com.tr' }; expect(spec.isBidRequestValid(bid)).to.equal(false);