Skip to content

Commit

Permalink
updates after pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofequativ committed Oct 25, 2024
1 parent 08e3c52 commit 0bc782c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 73 deletions.
30 changes: 30 additions & 0 deletions libraries/equativUtils/equativUtils.js
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 4 additions & 24 deletions modules/equativBidAdapter.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
36 changes: 5 additions & 31 deletions modules/smartadserverBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -256,15 +256,15 @@ 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 {
type = VIDEO;
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({});
Expand Down Expand Up @@ -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 = [];
Expand Down
16 changes: 9 additions & 7 deletions test/spec/modules/equativBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -259,21 +261,21 @@ 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', () => {
const bid = {
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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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);
});
});

Expand Down
23 changes: 12 additions & 11 deletions test/spec/modules/smartadserverBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
});

Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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);
});
});
});
Expand Down

0 comments on commit 0bc782c

Please sign in to comment.