Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config api updates #1633

Merged
merged 4 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 3 additions & 27 deletions src/adaptermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { mapSizes } from './sizeMapping';
import { processNativeAdUnitParams, nativeAdapters } from './native';
import { StorageManager, pbjsSyncsKey } from './storagemanager';
import { ajaxBuilder } from 'src/ajax';
import { config, RANDOM } from 'src/config';

var utils = require('./utils.js');
var CONSTANTS = require('./constants.json');
Expand All @@ -13,22 +14,9 @@ var events = require('./events');
var _bidderRegistry = {};
exports.bidderRegistry = _bidderRegistry;

// create s2s settings objectType_function
let _s2sConfig = {
endpoint: CONSTANTS.S2S.DEFAULT_ENDPOINT,
adapter: CONSTANTS.S2S.ADAPTER,
syncEndpoint: CONSTANTS.S2S.SYNC_ENDPOINT
};

const RANDOM = 'random';
const FIXED = 'fixed';

const VALID_ORDERS = {};
VALID_ORDERS[RANDOM] = true;
VALID_ORDERS[FIXED] = true;
let _s2sConfig = config.getConfig('s2sConfig');

var _analyticsRegistry = {};
let _bidderSequence = RANDOM;

function getBids({bidderCode, auctionId, bidderRequestId, adUnits}) {
return adUnits.map(adUnit => {
Expand Down Expand Up @@ -107,7 +95,7 @@ exports.makeBidRequests = function(adUnits, auctionStart, auctionId, cbTimeout)
let bidRequests = [];
let bidderCodes = getBidderCodes(adUnits);
const syncedBidders = StorageManager.get(pbjsSyncsKey);
if (_bidderSequence === RANDOM) {
if (config.getConfig('bidderSequence') === RANDOM) {
bidderCodes = shuffle(bidderCodes);
}

Expand Down Expand Up @@ -284,15 +272,3 @@ exports.enableAnalytics = function (config) {
}
});
};

exports.setBidderSequence = function (order) {
if (VALID_ORDERS[order]) {
_bidderSequence = order;
} else {
utils.logWarn(`Invalid order: ${order}. Bidder Sequence was not set.`);
}
};

exports.setS2SConfig = function (config) {
_s2sConfig = config;
};
2 changes: 1 addition & 1 deletion src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function newAuction({adUnits, adUnitCodes}) {
}

function doCallbacksIfNeeded() {
if (bid.timeToRespond > $$PREBID_GLOBAL$$.cbTimeout + $$PREBID_GLOBAL$$.timeoutBuffer) {
if (bid.timeToRespond > $$PREBID_GLOBAL$$.cbTimeout + config.getConfig('timeoutBuffer')) {
executeCallback(true);
}
}
Expand Down
64 changes: 55 additions & 9 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ const DEFAULT_USERSYNC = {
syncsPerBidder: 5,
syncDelay: 3000
};
const DEFAULT_TIMEOUTBUFFER = 200;
const DEFAULT_S2SCONFIG = {
enabled: false,
endpoint: 'https://prebid.adnxs.com/pbs/v1/auction',
timeout: 1000,
maxBids: 1,
adapter: 'prebidServer',
syncEndpoint: 'https://prebid.adnxs.com/pbs/v1/cookie_sync',
cookieSet: true,
bidders: []
};

export const RANDOM = 'random';
const FIXED = 'fixed';

const VALID_ORDERS = {};
VALID_ORDERS[RANDOM] = true;
VALID_ORDERS[FIXED] = true;

const DEFAULT_BIDDER_SEQUENCE = RANDOM;

const GRANULARITY_OPTIONS = {
'LOW': 'low',
Expand Down Expand Up @@ -48,9 +68,6 @@ export function newConfig() {
// `debug` is equivalent to legacy `pbjs.logging` property
_debug: DEFAULT_DEBUG,
get debug() {
if ($$PREBID_GLOBAL$$.logging || $$PREBID_GLOBAL$$.logging === false) {
return $$PREBID_GLOBAL$$.logging;
}
return this._debug;
},
set debug(val) {
Expand All @@ -60,7 +77,7 @@ export function newConfig() {
// default timeout for all bids
_bidderTimeout: DEFAULT_BIDDER_TIMEOUT,
get bidderTimeout() {
return $$PREBID_GLOBAL$$.bidderTimeout || this._bidderTimeout;
return this._bidderTimeout;
},
set bidderTimeout(val) {
this._bidderTimeout = val;
Expand All @@ -69,7 +86,7 @@ export function newConfig() {
// domain where prebid is running for cross domain iframe communication
_publisherDomain: DEFAULT_PUBLISHER_DOMAIN,
get publisherDomain() {
return $$PREBID_GLOBAL$$.publisherDomain || this._publisherDomain;
return this._publisherDomain;
},
set publisherDomain(val) {
this._publisherDomain = val;
Expand Down Expand Up @@ -114,14 +131,43 @@ export function newConfig() {
this._sendAllBids = val;
},

// calls existing function which may be moved after deprecation
_bidderSequence: DEFAULT_BIDDER_SEQUENCE,
get bidderSequence() {
return this._bidderSequence;
},
set bidderSequence(val) {
$$PREBID_GLOBAL$$.setBidderSequence(val);
if (VALID_ORDERS[val]) {
this._bidderSequence = val;
} else {
utils.logWarn(`Invalid order: ${val}. Bidder Sequence was not set.`);
}
},

// calls existing function which may be moved after deprecation
// timeout buffer to adjust for bidder CDN latency
_timoutBuffer: DEFAULT_TIMEOUTBUFFER,
get timeoutBuffer() {
return this._timoutBuffer;
},
set timeoutBuffer(val) {
this._timoutBuffer = val;
},

_s2sConfig: DEFAULT_S2SCONFIG,
get s2sConfig() {
return this._s2sConfig;
},
set s2sConfig(val) {
$$PREBID_GLOBAL$$.setS2SConfig(val);
if (!utils.contains(Object.keys(val), 'accountId')) {
utils.logError('accountId missing in Server to Server config');
return;
}

if (!utils.contains(Object.keys(val), 'bidders')) {
utils.logError('bidders missing in Server to Server config');
return;
}

this._s2sConfig = Object.assign({}, DEFAULT_S2SCONFIG, val);
},

// userSync defaults
Expand Down
3 changes: 0 additions & 3 deletions src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@
"hb_deal"
],
"S2S" : {
"DEFAULT_ENDPOINT" : "https://prebid.adnxs.com/pbs/v1/auction",
"SRC" : "s2s",
"ADAPTER" : "prebidServer",
"SYNC_ENDPOINT" : "https://prebid.adnxs.com/pbs/v1/cookie_sync",
"SYNCED_BIDDERS_KEY": "pbjsSyncs"
}
}
98 changes: 0 additions & 98 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { flatten, uniques, isGptPubadsDefined, adUnitsFilter } from './utils';
import { videoAdUnit, hasNonVideoBidder } from './video';
import { nativeAdUnit, nativeBidder, hasNonNativeBidder } from './native';
import './polyfill';
import { parse as parseURL, format as formatURL } from './url';
import { listenMessagesFromCreative } from './secureCreatives';
import { userSync } from 'src/userSync.js';
import { loadScript } from './adloader';
Expand All @@ -20,7 +19,6 @@ var utils = require('./utils.js');
var adaptermanager = require('./adaptermanager');
var bidfactory = require('./bidfactory');
var events = require('./events');
var adserver = require('./adserver.js');
const { triggerUserSyncs } = userSync;

/* private variables */
Expand All @@ -35,25 +33,12 @@ var eventValidators = {

/* Public vars */
$$PREBID_GLOBAL$$._winningBids = [];
$$PREBID_GLOBAL$$._adsReceived = [];

$$PREBID_GLOBAL$$.bidderSettings = $$PREBID_GLOBAL$$.bidderSettings || {};

/** @deprecated - use pbjs.setConfig({ bidderTimeout: <timeout> }) */
$$PREBID_GLOBAL$$.bidderTimeout = $$PREBID_GLOBAL$$.bidderTimeout;

// current timeout set in `requestBids` or to default `bidderTimeout`
$$PREBID_GLOBAL$$.cbTimeout = $$PREBID_GLOBAL$$.cbTimeout || 200;

// timeout buffer to adjust for bidder CDN latency
$$PREBID_GLOBAL$$.timeoutBuffer = 200;

/** @deprecated - use pbjs.setConfig({ debug: <true|false> }) */
$$PREBID_GLOBAL$$.logging = $$PREBID_GLOBAL$$.logging;

/** @deprecated - use pbjs.setConfig({ publisherDomain: <domain> ) */
$$PREBID_GLOBAL$$.publisherDomain = $$PREBID_GLOBAL$$.publisherDomain;

// let the world know we are loaded
$$PREBID_GLOBAL$$.libLoaded = true;

Expand Down Expand Up @@ -498,52 +483,6 @@ $$PREBID_GLOBAL$$.getAllWinningBids = function () {
return $$PREBID_GLOBAL$$._winningBids;
};

/**
* Build master video tag from publishers adserver tag
* @param {string} adserverTag default url
* @param {Object} options options for video tag
*
* @deprecated Include the dfpVideoSupport module in your build, and use the $$PREBID_GLOBAL$$.adservers.dfp.buildVideoAdUrl function instead.
* This function will be removed in Prebid 1.0.
*/
$$PREBID_GLOBAL$$.buildMasterVideoTagFromAdserverTag = function (adserverTag, options) {
utils.logWarn('$$PREBID_GLOBAL$$.buildMasterVideoTagFromAdserverTag will be removed in Prebid 1.0. Include the dfpVideoSupport module in your build, and use the $$PREBID_GLOBAL$$.adservers.dfp.buildVideoAdUrl function instead');
utils.logInfo('Invoking $$PREBID_GLOBAL$$.buildMasterVideoTagFromAdserverTag', arguments);
var urlComponents = parseURL(adserverTag);

// return original adserverTag if no bids received
if ($$PREBID_GLOBAL$$._bidsReceived.length === 0) {
return adserverTag;
}

var masterTag = '';
if (options.adserver.toLowerCase() === 'dfp') {
var dfpAdserverObj = adserver.dfpAdserver(options, urlComponents);
if (!dfpAdserverObj.verifyAdserverTag()) {
utils.logError('Invalid adserverTag, required google params are missing in query string');
}
dfpAdserverObj.appendQueryParams();
masterTag = formatURL(dfpAdserverObj.urlComponents);
} else {
utils.logError('Only DFP adserver is supported');
return;
}
return masterTag;
};

/**
* Set the order bidders are called in. Valid values are:
*
* "fixed": Bidders will be called in the order in which they were defined within the adUnit.bids array.
* "random": Bidders will be called in random order.
*
* If never called, Prebid will use "random" as the default.
*
* @param {string} order One of the valid orders, described above.
* @deprecated - use pbjs.setConfig({ bidderSequence: <order> })
*/
$$PREBID_GLOBAL$$.setBidderSequence = adaptermanager.setBidderSequence;

/**
* Get array of highest cpm bids for all adUnits, or highest cpm bid
* object for the given adUnit
Expand All @@ -554,43 +493,6 @@ $$PREBID_GLOBAL$$.getHighestCpmBids = function (adUnitCode) {
return targeting.getWinningBids(adUnitCode);
};

/**
* Set config for server to server header bidding
* @deprecated - use pbjs.setConfig({ s2sConfig: <options> })
* @typedef {Object} options - required
* @property {boolean} enabled enables S2S bidding
* @property {string[]} bidders bidders to request S2S
* === optional params below ===
* @property {string} [endpoint] endpoint to contact
* @property {number} [timeout] timeout for S2S bidders - should be lower than `pbjs.requestBids({timeout})`
* @property {string} [adapter] adapter code to use for S2S
* @property {string} [syncEndpoint] endpoint URL for syncing cookies
* @property {boolean} [cookieSet] enables cookieSet functionality
*/
$$PREBID_GLOBAL$$.setS2SConfig = function(options) {
if (!utils.contains(Object.keys(options), 'accountId')) {
utils.logError('accountId missing in Server to Server config');
return;
}

if (!utils.contains(Object.keys(options), 'bidders')) {
utils.logError('bidders missing in Server to Server config');
return;
}

const config = Object.assign({
enabled: false,
endpoint: CONSTANTS.S2S.DEFAULT_ENDPOINT,
timeout: 1000,
maxBids: 1,
adapter: CONSTANTS.S2S.ADAPTER,
syncEndpoint: CONSTANTS.S2S.SYNC_ENDPOINT,
cookieSet: true,
bidders: []
}, options);
adaptermanager.setS2SConfig(config);
};

/**
* Get Prebid config options
* @param {Object} options
Expand Down
32 changes: 14 additions & 18 deletions test/spec/config_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ let setConfig;

describe('config API', () => {
let logErrorSpy;
let logWarnSpy;
beforeEach(() => {
const config = newConfig();
getConfig = config.getConfig;
setConfig = config.setConfig;
logErrorSpy = sinon.spy(utils, 'logError');
logWarnSpy = sinon.spy(utils, 'logWarn');
});

afterEach(() => {
utils.logError.restore();
utils.logWarn.restore();
});

it('setConfig is a function', () => {
Expand Down Expand Up @@ -52,34 +55,16 @@ describe('config API', () => {
expect(getConfig('debug')).to.be.true;
});

// remove test when @deprecated $$PREBID_GLOBAL$$.logging removed
it('gets legacy logging in deprecation window', () => {
$$PREBID_GLOBAL$$.logging = false;
expect(getConfig('debug')).to.equal(false);
});

it('sets bidderTimeout', () => {
setConfig({ bidderTimeout: 1000 });
expect(getConfig('bidderTimeout')).to.be.equal(1000);
});

// remove test when @deprecated $$PREBID_GLOBAL$$.bidderTimeout removed
it('gets legacy bidderTimeout in deprecation window', () => {
$$PREBID_GLOBAL$$.bidderTimeout = 5000;
expect(getConfig('bidderTimeout')).to.equal(5000);
});

it('gets user-defined publisherDomain', () => {
setConfig({ publisherDomain: 'fc.kahuna' });
expect(getConfig('publisherDomain')).to.equal('fc.kahuna');
});

// remove test when @deprecated $$PREBID_GLOBAL$$.publisherDomain removed
it('gets legacy publisherDomain in deprecation window', () => {
$$PREBID_GLOBAL$$.publisherDomain = 'ad.example.com';
expect(getConfig('publisherDomain')).to.equal('ad.example.com');
});

it('gets default userSync config', () => {
expect(getConfig('userSync')).to.eql({
syncEnabled: true,
Expand Down Expand Up @@ -160,4 +145,15 @@ describe('config API', () => {
const error = 'Prebid Error: no value passed to `setPriceGranularity()`';
assert.ok(logErrorSpy.calledWith(error), 'expected error was logged');
});

it('should log a warning on invalid values', () => {
setConfig({ bidderSequence: 'unrecognized sequence' });
expect(logWarnSpy.calledOnce).to.equal(true);
});

it('should not log warnings when given recognized values', () => {
setConfig({ bidderSequence: 'fixed' });
setConfig({ bidderSequence: 'random' });
expect(logWarnSpy.called).to.equal(false);
});
});
Loading