Skip to content

Commit

Permalink
Long form video price bucket bugfix (#4125)
Browse files Browse the repository at this point in the history
* long form price bucket bugfix

* updated logic to use medium as default granularity

* remove unused import

* use contants

* move functions to auction module
  • Loading branch information
jaiminpanchal27 authored Aug 28, 2019
1 parent 18543d6 commit 761226f
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 34 deletions.
11 changes: 7 additions & 4 deletions modules/adpod.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

import * as utils from '../src/utils';
import { addBidToAuction, doCallbacksIfTimedout, AUCTION_IN_PROGRESS, callPrebidCache } from '../src/auction';
import { addBidToAuction, doCallbacksIfTimedout, AUCTION_IN_PROGRESS, callPrebidCache, getPriceByGranularity, getPriceGranularity } from '../src/auction';
import { checkAdUnitSetup } from '../src/prebid';
import { checkVideoBidSetup } from '../src/video';
import { setupBeforeHookFnOnce, module } from '../src/hook';
Expand All @@ -23,6 +23,7 @@ import { ADPOD } from '../src/mediaTypes';
import Set from 'core-js/library/fn/set';
import find from 'core-js/library/fn/array/find';
import { auctionManager } from '../src/auctionManager';
import CONSTANTS from '../src/constants.json';

const from = require('core-js/library/fn/array/from');

Expand Down Expand Up @@ -119,7 +120,9 @@ function createDispatcher(timeoutDuration) {
function attachPriceIndustryDurationKeyToBid(bid, brandCategoryExclusion) {
let initialCacheKey = bidCacheRegistry.getInitialCacheKey(bid);
let duration = utils.deepAccess(bid, 'video.durationBucket');
let cpmFixed = bid.cpm.toFixed(2);
const granularity = getPriceGranularity(bid.mediaType);
let cpmFixed = getPriceByGranularity(granularity)(bid);

let pcd;

if (brandCategoryExclusion) {
Expand Down Expand Up @@ -424,10 +427,10 @@ export function callPrebidCacheAfterAuction(bids, callback) {
* @param {Object} bid
*/
export function sortByPricePerSecond(a, b) {
if (a.cpm / a.video.durationBucket < b.cpm / b.video.durationBucket) {
if (a.adserverTargeting[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] / a.video.durationBucket < b.adserverTargeting[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] / b.video.durationBucket) {
return 1;
}
if (a.cpm / a.video.durationBucket > b.cpm / b.video.durationBucket) {
if (a.adserverTargeting[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] / a.video.durationBucket > b.adserverTargeting[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] / b.video.durationBucket) {
return -1;
}
return 0;
Expand Down
55 changes: 37 additions & 18 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,41 @@ function setupBidTargeting(bidObject, bidderRequest) {
bidObject.adserverTargeting = Object.assign(bidObject.adserverTargeting || {}, keyValues);
}

/**
* This function returns the price granularity defined. It can be either publisher defined or default value
* @param {string} mediaType
* @returns {string} granularity
*/
export const getPriceGranularity = (mediaType) => {
// Use the config value 'mediaTypeGranularity' if it has been set for mediaType, else use 'priceGranularity'
const mediaTypeGranularity = config.getConfig(`mediaTypePriceGranularity.${mediaType}`);
const granularity = (typeof mediaType === 'string' && mediaTypeGranularity) ? ((typeof mediaTypeGranularity === 'string') ? mediaTypeGranularity : 'custom') : config.getConfig('priceGranularity');
return granularity;
}

/**
* This function returns a function to get bid price by price granularity
* @param {string} granularity
* @returns {function}
*/
export const getPriceByGranularity = (granularity) => {
return (bid) => {
if (granularity === CONSTANTS.GRANULARITY_OPTIONS.AUTO) {
return bid.pbAg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.DENSE) {
return bid.pbDg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.LOW) {
return bid.pbLg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.MEDIUM) {
return bid.pbMg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.HIGH) {
return bid.pbHg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.CUSTOM) {
return bid.pbCg;
}
}
}

/**
* @param {string} mediaType
* @param {string} bidderCode
Expand All @@ -530,9 +565,7 @@ export function getStandardBidderSettings(mediaType, bidderCode) {
};
}
const TARGETING_KEYS = CONSTANTS.TARGETING_KEYS;
// Use the config value 'mediaTypeGranularity' if it has been set for mediaType, else use 'priceGranularity'
const mediaTypeGranularity = config.getConfig(`mediaTypePriceGranularity.${mediaType}`);
const granularity = (typeof mediaType === 'string' && mediaTypeGranularity) ? ((typeof mediaTypeGranularity === 'string') ? mediaTypeGranularity : 'custom') : config.getConfig('priceGranularity');
const granularity = getPriceGranularity(mediaType);

let bidderSettings = $$PREBID_GLOBAL$$.bidderSettings;
if (!bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD]) {
Expand All @@ -542,21 +575,7 @@ export function getStandardBidderSettings(mediaType, bidderCode) {
bidderSettings[CONSTANTS.JSON_MAPPING.BD_SETTING_STANDARD][CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING] = [
createKeyVal(TARGETING_KEYS.BIDDER, 'bidderCode'),
createKeyVal(TARGETING_KEYS.AD_ID, 'adId'),
createKeyVal(TARGETING_KEYS.PRICE_BUCKET, function(bidResponse) {
if (granularity === CONSTANTS.GRANULARITY_OPTIONS.AUTO) {
return bidResponse.pbAg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.DENSE) {
return bidResponse.pbDg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.LOW) {
return bidResponse.pbLg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.MEDIUM) {
return bidResponse.pbMg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.HIGH) {
return bidResponse.pbHg;
} else if (granularity === CONSTANTS.GRANULARITY_OPTIONS.CUSTOM) {
return bidResponse.pbCg;
}
}),
createKeyVal(TARGETING_KEYS.PRICE_BUCKET, getPriceByGranularity(granularity)),
createKeyVal(TARGETING_KEYS.SIZE, 'size'),
createKeyVal(TARGETING_KEYS.DEAL, 'dealId'),
createKeyVal(TARGETING_KEYS.SOURCE, 'source'),
Expand Down
Loading

0 comments on commit 761226f

Please sign in to comment.