From 0bc782c77da2019ebd8ed21c260e6192223f2136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Sok=C3=B3=C5=82?= Date: Fri, 25 Oct 2024 09:19:40 +0200 Subject: [PATCH] updates after pr review --- libraries/equativUtils/equativUtils.js | 30 ++++++++++++++++ modules/equativBidAdapter.js | 28 +++------------ modules/smartadserverBidAdapter.js | 36 +++---------------- test/spec/modules/equativBidAdapter_spec.js | 16 +++++---- .../modules/smartadserverBidAdapter_spec.js | 23 ++++++------ 5 files changed, 60 insertions(+), 73 deletions(-) create mode 100644 libraries/equativUtils/equativUtils.js diff --git a/libraries/equativUtils/equativUtils.js b/libraries/equativUtils/equativUtils.js new file mode 100644 index 00000000000..bdcbdad2f33 --- /dev/null +++ b/libraries/equativUtils/equativUtils.js @@ -0,0 +1,30 @@ +import { VIDEO } from '../../src/mediaTypes.js'; +import { deepAccess, isFn } from '../../src/utils.js'; + +const DEFAULT_FLOOR = 0.0; + +/** + * Get floors from Prebid Price Floors module + * + * @param {object} bid Bid request object + * @param {string} currency Ad server currency + * @param {string} mediaType Bid media type + * @return {number} Floor price + */ +export function getBidFloor (bid, currency, mediaType) { + const floors = []; + + if (isFn(bid.getFloor)) { + (deepAccess(bid, `mediaTypes.${mediaType}.${mediaType === VIDEO ? 'playerSize' : 'sizes'}`) || []).forEach(size => { + const floor = bid.getFloor({ + currency: currency || 'USD', + mediaType, + size + }).floor; + + floors.push(!isNaN(floor) ? floor : DEFAULT_FLOOR); + }); + } + + return floors.length ? Math.min(...floors) : DEFAULT_FLOOR; +} diff --git a/modules/equativBidAdapter.js b/modules/equativBidAdapter.js index c5c96039f67..2c071fe832a 100644 --- a/modules/equativBidAdapter.js +++ b/modules/equativBidAdapter.js @@ -1,7 +1,8 @@ import { BANNER } from '../src/mediaTypes.js'; +import { getBidFloor } from '../libraries/equativUtils/equativUtils.js' import { ortbConverter } from '../libraries/ortbConverter/converter.js'; import { registerBidder } from '../src/adapters/bidderFactory.js'; -import { deepAccess, deepSetValue, isFn, mergeDeep } from '../src/utils.js'; +import { deepAccess, deepSetValue, mergeDeep } from '../src/utils.js'; /** * @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid @@ -26,27 +27,6 @@ export const spec = { }; }, - /** - * @param bidRequest - * @returns {number} - */ - getMinFloor: (bidRequest) => { - const floors = []; - - if (isFn(bidRequest.getFloor)) { - (deepAccess(bidRequest, 'mediaTypes.banner.sizes') || []).forEach(size => { - const floor = bidRequest.getFloor({ size }).floor; - if (!isNaN(floor)) { - floors.push(floor); - } else { - floors.push(0.0); - } - }); - } - - return floors.length ? Math.min(...floors) : 0.0; - }, - /** * @param serverResponse * @param bidRequest @@ -105,8 +85,8 @@ export const converter = ortbConverter({ delete imp.dt; - imp.bidfloor = imp.bidfloor || spec.getMinFloor(bidRequest); - imp.secure = Number(window.location.protocol === 'https:'); + imp.bidfloor = imp.bidfloor || getBidFloor(bidRequest); + imp.secure = 1; imp.tagid = bidRequest.adUnitCode; if (siteId || pageId || formatId) { diff --git a/modules/smartadserverBidAdapter.js b/modules/smartadserverBidAdapter.js index 53b13ecf944..e0ddf0a66e6 100644 --- a/modules/smartadserverBidAdapter.js +++ b/modules/smartadserverBidAdapter.js @@ -4,23 +4,23 @@ import { isArray, isArrayOfNums, isEmpty, - isFn, isInteger, logError } from '../src/utils.js'; import { BANNER, VIDEO } from '../src/mediaTypes.js'; import { config } from '../src/config.js'; +import { getBidFloor } from '../libraries/equativUtils/equativUtils.js' import { registerBidder } from '../src/adapters/bidderFactory.js'; /** * @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest * @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid * @typedef {import('../src/adapters/bidderFactory.js').ServerRequest} ServerRequest + * @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync */ const BIDDER_CODE = 'smartadserver'; const GVL_ID = 45; -const DEFAULT_FLOOR = 0.0; export const spec = { code: BIDDER_CODE, @@ -256,7 +256,7 @@ export const spec = { if (isSupportedVideoContext) { let videoPayload = deepClone(payload); spec.fillPayloadForVideoBidRequest(videoPayload, videoMediaType, bid.params.video); - videoPayload.bidfloor = bid.params.bidfloor || spec.getBidFloor(bid, adServerCurrency, VIDEO); + videoPayload.bidfloor = bid.params.bidfloor || getBidFloor(bid, adServerCurrency, VIDEO); bidRequests.push(spec.createServerRequest(videoPayload, bid.params.domain)); } } else { @@ -264,7 +264,7 @@ export const spec = { spec.fillPayloadForVideoBidRequest(payload, videoMediaType, bid.params.video); } - payload.bidfloor = bid.params.bidfloor || spec.getBidFloor(bid, adServerCurrency, type); + payload.bidfloor = bid.params.bidfloor || getBidFloor(bid, adServerCurrency, type); bidRequests.push(spec.createServerRequest(payload, bid.params.domain)); } else { bidRequests.push({}); @@ -323,38 +323,12 @@ export const spec = { return bidResponses; }, - /** - * Get floors from Prebid Price Floors module - * - * @param {object} bid Bid request object - * @param {string} currency Ad server currency - * @param {string} mediaType Bid media type - * @return {number} Floor price - */ - getBidFloor: (bid, currency, mediaType) => { - const floors = []; - - if (isFn(bid.getFloor)) { - (deepAccess(bid, `mediaTypes.${mediaType}.${mediaType === BANNER ? 'sizes' : 'playerSize'}`) || []).forEach(size => { - const floor = bid.getFloor({ - currency: currency || 'USD', - mediaType, - size - }).floor; - - floors.push(!isNaN(floor) ? floor : DEFAULT_FLOOR); - }); - } - - return floors.length ? Math.min(...floors) : DEFAULT_FLOOR; - }, - /** * User syncs. * * @param {*} syncOptions Publisher prebid configuration. * @param {*} serverResponses A successful response from the server. - * @return {syncs[]} An array of syncs that should be executed. + * @return {UserSync[]} An array of syncs that should be executed. */ getUserSyncs: function (syncOptions, serverResponses) { const syncs = []; diff --git a/test/spec/modules/equativBidAdapter_spec.js b/test/spec/modules/equativBidAdapter_spec.js index c2465176ac1..b3009e38710 100644 --- a/test/spec/modules/equativBidAdapter_spec.js +++ b/test/spec/modules/equativBidAdapter_spec.js @@ -1,3 +1,5 @@ +import { BANNER } from 'src/mediaTypes.js'; +import { getBidFloor } from 'libraries/equativUtils/equativUtils.js' import { spec, converter } from 'modules/equativBidAdapter.js'; describe('Equativ bid adapter tests', () => { @@ -238,7 +240,7 @@ describe('Equativ bid adapter tests', () => { DEFAULT_BID_REQUESTS, DEFAULT_BIDDER_REQUEST ); - expect(request.data.imp[0]).to.have.property('secure').that.within(0, 1); + expect(request.data.imp[0]).to.have.property('secure').that.eq(1); }); it('should have tagid', () => { @@ -259,13 +261,13 @@ describe('Equativ bid adapter tests', () => { }); }); - describe('getMinFloor', () => { + describe('getBidFloor', () => { it('should return floor of 0.0 if floor module not available', () => { const bid = { ...DEFAULT_BID_REQUESTS[0], getFloor: false, }; - expect(spec.getMinFloor(bid)).to.deep.eq(0.0); + expect(getBidFloor(bid)).to.deep.eq(0.0); }); it('should return floor of 0.0 if mediaTypes not defined', () => { @@ -273,7 +275,7 @@ describe('Equativ bid adapter tests', () => { getFloor: () => ({}) }; expect(bid.mediaTypes).to.be.undefined; - expect(spec.getMinFloor(bid)).to.deep.eq(0.0); + expect(getBidFloor(bid)).to.deep.eq(0.0); }); it('should return proper min floor', () => { @@ -289,7 +291,7 @@ describe('Equativ bid adapter tests', () => { } } }; - expect(spec.getMinFloor(bid)).to.deep.eq(1.13); + expect(getBidFloor(bid, 'USD', BANNER)).to.deep.eq(1.13); }); it('should return global media type floor if no rule for size', () => { @@ -305,7 +307,7 @@ describe('Equativ bid adapter tests', () => { } } }; - expect(spec.getMinFloor(bid)).to.deep.eq(0.34); + expect(getBidFloor(bid, 'USD', BANNER)).to.deep.eq(0.34); }); it('should return floor of 0 if no rule for size', () => { @@ -321,7 +323,7 @@ describe('Equativ bid adapter tests', () => { } } }; - expect(spec.getMinFloor(bid)).to.deep.eq(0.0); + expect(getBidFloor(bid, 'USD', BANNER)).to.deep.eq(0.0); }); }); diff --git a/test/spec/modules/smartadserverBidAdapter_spec.js b/test/spec/modules/smartadserverBidAdapter_spec.js index 3ee00a99bd9..0935b6c8404 100644 --- a/test/spec/modules/smartadserverBidAdapter_spec.js +++ b/test/spec/modules/smartadserverBidAdapter_spec.js @@ -2,6 +2,7 @@ import { expect } from 'chai'; import { BANNER, VIDEO } from 'src/mediaTypes.js'; import { config } from 'src/config.js'; import { deepClone } from 'src/utils.js'; +import { getBidFloor } from 'libraries/equativUtils/equativUtils.js' import { spec } from 'modules/smartadserverBidAdapter.js'; // Default params with optional ones @@ -1314,7 +1315,7 @@ describe('Smart bid adapter tests', function () { it('should return default floor when module not activated', function() { const bidRequest = JSON.parse((spec.buildRequests(DEFAULT_PARAMS_WO_OPTIONAL))[0].data); - const floor = spec.getBidFloor(bidRequest, 'EUR'); + const floor = getBidFloor(bidRequest, 'EUR'); expect(floor).to.deep.equal(0); }); @@ -1324,14 +1325,14 @@ describe('Smart bid adapter tests', function () { return { floor: 'one' }; }; - const floor = spec.getBidFloor(bidRequest, 'EUR'); + const floor = getBidFloor(bidRequest, 'EUR'); expect(floor).to.deep.equal(0.0); }); it('should return default floor when currency unknown', function() { const bidRequest = JSON.parse((spec.buildRequests(DEFAULT_PARAMS_WO_OPTIONAL))[0].data); - const floor = spec.getBidFloor(bidRequest, null); + const floor = getBidFloor(bidRequest, null); expect(floor).to.deep.equal(0); }); @@ -1456,25 +1457,25 @@ describe('Smart bid adapter tests', function () { }); it('should return lowest floor from specified ones', () => { - expect(spec.getBidFloor(bid, 'USD', BANNER)).to.deep.eq(1.2); + expect(getBidFloor(bid, 'USD', BANNER)).to.deep.eq(1.2); }); it('should return default floor for media type whatever size', () => { bid.mediaTypes.banner.sizes.push([300, 400]); - expect(spec.getBidFloor(bid, 'USD', BANNER)).to.deep.eq(1.0); + expect(getBidFloor(bid, 'USD', BANNER)).to.deep.eq(1.0); }); it('should return default floor', () => { - expect(spec.getBidFloor(bid, 'USD', VIDEO)).to.deep.eq(0); + expect(getBidFloor(bid, 'USD', VIDEO)).to.deep.eq(0); }); it('should return floor when currency not passed', () => { - expect(spec.getBidFloor(bid, undefined, BANNER)).to.deep.eq(1.2); + expect(getBidFloor(bid, undefined, BANNER)).to.deep.eq(1.2); }); it('should return DEFAULT_FLOOR in case of not a number value from floor module', () => { bid.mediaTypes.banner.sizes.push([30, 60]); - expect(spec.getBidFloor(bid, 'USD', BANNER)).to.deep.eq(0); + expect(getBidFloor(bid, 'USD', BANNER)).to.deep.eq(0); }); it('should return proper video floor', () => { @@ -1485,7 +1486,7 @@ describe('Smart bid adapter tests', function () { ] } }; - expect(spec.getBidFloor(bid, 'USD', VIDEO)).to.deep.eq(2.3); + expect(getBidFloor(bid, 'USD', VIDEO)).to.deep.eq(2.3); }); it('should return default video floor', () => { @@ -1496,11 +1497,11 @@ describe('Smart bid adapter tests', function () { ] } }; - expect(spec.getBidFloor(bid, 'USD', VIDEO)).to.deep.eq(2.1); + expect(getBidFloor(bid, 'USD', VIDEO)).to.deep.eq(2.1); }); it('should return DEFAULT_FLOOR for not supported media type', () => { - expect(spec.getBidFloor(bid, 'USD', 'test')).to.deep.eq(0); + expect(getBidFloor(bid, 'USD', 'test')).to.deep.eq(0); }); }); });