Skip to content

Commit

Permalink
add a check against the size config when setting targeting (prebid#3183)
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich authored and Pedro López Jiménez committed Mar 18, 2019
1 parent f58c248 commit b23aebc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
15 changes: 1 addition & 14 deletions src/adaptermanager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @module adaptermanger */

import { flatten, getBidderCodes, getDefinedParams, shuffle, timestamp } from './utils';
import { resolveStatus } from './sizeMapping';
import { getLabels, resolveStatus } from './sizeMapping';
import { processNativeAdUnitParams, nativeAdapters } from './native';
import { newBidder } from './adapters/bidderFactory';
import { ajaxBuilder } from 'src/ajax';
Expand Down Expand Up @@ -34,19 +34,6 @@ var _analyticsRegistry = {};
* @property {Array<string>} activeLabels the labels specified as being active by requestBids
*/

/**
* Returns object describing the status of labels on the adUnit or bidder along with labels passed into requestBids
* @param bidOrAdUnit the bidder or adUnit to get label info on
* @param activeLabels the labels passed to requestBids
* @returns {LabelDescriptor}
*/
function getLabels(bidOrAdUnit, activeLabels) {
if (bidOrAdUnit.labelAll) {
return {labelAll: true, labels: bidOrAdUnit.labelAll, activeLabels};
}
return {labelAll: false, labels: bidOrAdUnit.labelAny, activeLabels};
}

function getBids({bidderCode, auctionId, bidderRequestId, adUnits, labels, src}) {
return adUnits.reduce((result, adUnit) => {
let {
Expand Down
27 changes: 27 additions & 0 deletions src/sizeMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ export function setSizeConfig(config) {
}
config.getConfig('sizeConfig', config => setSizeConfig(config.sizeConfig));

/**
* Returns object describing the status of labels on the adUnit or bidder along with labels passed into requestBids
* @param bidOrAdUnit the bidder or adUnit to get label info on
* @param activeLabels the labels passed to requestBids
* @returns {LabelDescriptor}
*/
export function getLabels(bidOrAdUnit, activeLabels) {
if (bidOrAdUnit.labelAll) {
return {labelAll: true, labels: bidOrAdUnit.labelAll, activeLabels};
}
return {labelAll: false, labels: bidOrAdUnit.labelAny, activeLabels};
}

/**
* Determines whether a single size is valid given configured sizes
* @param {Array} size [width, height]
* @param {Array<SizeConfig>} configs
* @returns {boolean}
*/
export function sizeSupported(size, configs = sizeConfig) {
let maps = evaluateSizeConfig(configs);
if (!maps.shouldFilter) {
return true;
}
return !!maps.sizesSupported[size];
}

/**
* Resolves the unique set of the union of all sizes and labels that are active from a SizeConfig.mediaQuery match
* @param {Array<string>} labels Labels specified on adUnit or bidder
Expand Down
2 changes: 2 additions & 0 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { uniques, isGptPubadsDefined, getHighestCpm, getOldestHighestCpmBid, gro
import { config } from './config';
import { NATIVE_TARGETING_KEYS } from './native';
import { auctionManager } from './auctionManager';
import { sizeSupported } from './sizeMapping';
import includes from 'core-js/library/fn/array/includes';

const utils = require('./utils.js');
Expand Down Expand Up @@ -198,6 +199,7 @@ export function newTargeting(auctionManager) {

function getBidsReceived() {
const bidsReceived = auctionManager.getBidsReceived()
.filter(bid => bid.mediaType !== 'banner' || sizeSupported([bid.width, bid.height]))
.filter(isUnusedBid)
.filter(exports.isBidNotExpired)
;
Expand Down
9 changes: 8 additions & 1 deletion test/spec/sizeMapping_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { resolveStatus, setSizeConfig } from 'src/sizeMapping';
import { resolveStatus, setSizeConfig, sizeSupported } from 'src/sizeMapping';
import includes from 'core-js/library/fn/array/includes';

let utils = require('src/utils');
Expand Down Expand Up @@ -75,6 +75,13 @@ describe('sizeMapping', function () {
});

describe('when handling sizes', function () {
it('should allow us to validate a single size', function() {
matchMediaOverride = (str) => str === '(min-width: 1200px)' ? {matches: true} : {matches: false};

expect(sizeSupported([300, 250])).to.equal(true);
expect(sizeSupported([80, 80])).to.equal(false);
});

it('should log a warning when mediaQuery property missing from sizeConfig', function () {
let errorConfig = deepClone(sizeConfig);

Expand Down

0 comments on commit b23aebc

Please sign in to comment.