Skip to content
New issue

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

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

Already on GitHub? # to your account

bidderSettings: Allow Currency Code control through adjustCurrency #12645

Closed
wants to merge 14 commits into from
12 changes: 12 additions & 0 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ const queuedCalls = [];

const pbjsInstance = getGlobal();

/**
* Get Prebid config options
* @param {Object} options
* @alias module:pbjs.getConfig
*/
pbjsInstance.getConfig = config.getAnyConfig;

/**
* Clear global state for tests
*/
Expand Down Expand Up @@ -912,6 +919,11 @@ export function adjustBids(bid) {

if (bidPriceAdjusted >= 0) {
bid.cpm = bidPriceAdjusted;
// Get the currency adjust setting for this bidder
const shouldAdjustCurrency = bidderSettings.getCurrencyAdjust(bid.bidderCode);
if (shouldAdjustCurrency === true) {
bid.currency = config.getConfig('currency')?.adServerCurrency;
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/bidderSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ export class ScopedSettings {
this.defaultScope = defaultScope;
}

// To validate currency settings
validateCurrencyAdjust(value) {
return value === undefined || typeof value === 'boolean';
}

// Currency Adjust Method
getCurrencyAdjust(bidderCode) {
// For bidder specific
let currencyAdjust = this.get(bidderCode, JSON_MAPPING.CURRENCY_ADJUST);
// For standard
if (typeof currencyAdjust === 'undefined') {
currencyAdjust = this.get(this.defaultScope, JSON_MAPPING.CURRENCY_ADJUST);
}
return currencyAdjust;
}

/**
* Get setting value at `path` under the given scope, falling back to the default scope if needed.
* If `scope` is `null`, get the setting's default value.
Expand Down
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const JSON_MAPPING = {
BD_ID: 'paramsd',
BD_PL_ID: 'placementId',
ADSERVER_TARGETING: 'adserverTargeting',
BD_SETTING_STANDARD: 'standard'
BD_SETTING_STANDARD: 'standard',
CURRENCY_ADJUST: 'adjustCurrency'
};

export const DEBUG_MODE = 'pbjs_debug';
Expand Down
92 changes: 92 additions & 0 deletions test/spec/unit/core/bidderSettings_currency_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// test/spec/bidderSettings_currency_spec.js
import { JSON_MAPPING } from '../../../../src/constants.js';
import { expect } from 'chai/index.js';
import {bidderSettings} from '../../../../src/bidderSettings.js';
import {config} from '../../../../src/config.js';
import { getGlobal } from '../../../../src/prebidGlobal';
import { adjustBids } from '../../../../src/auction.js';

describe('Bidder Settings Currency Adjustment', function() {
let bid;
let sandbox;

beforeEach(function() {
bid = {
bidderCode: 'appnexus',
cpm: 1.5,
currency: 'USD'
};

// Reset pbjs global object
window.pbjs = getGlobal();
window.pbjs.bidderSettings = {};

// Set default currency config
config.setConfig({
currency: {
adServerCurrency: 'EUR'
}
});
});

afterEach(function() {
config.resetConfig();
});

describe('currency adjustment setting', function() {
it('should respect bidder-specific currency adjustment setting', function() {
// Setup
window.pbjs.bidderSettings = {
appnexus: {
[JSON_MAPPING.CURRENCY_ADJUST]: true
}
};

// Execute
adjustBids(bid);

// Assert
expect(bid.currency).to.equal('EUR');
});

it('should use standard setting when bidder-specific not defined', function() {
// Setup
window.pbjs.bidderSettings = {
standard: {
[JSON_MAPPING.CURRENCY_ADJUST]: true
}
};

// Execute
adjustBids(bid);

// Assert
expect(bid.currency).to.equal('EUR');
});

it('should not adjust currency when setting is false', function() {
// Setup
window.pbjs.bidderSettings = {
appnexus: {
[JSON_MAPPING.CURRENCY_ADJUST]: false
}
};

// Execute
adjustBids(bid);

// Assert
expect(bid.currency).to.equal('USD');
});

it('should not adjust currency when setting is undefined', function() {
// Setup - no settings defined

// Execute
adjustBids(bid);

// Assert
expect(bid.currency).to.equal('USD');
});
});
});
Loading