forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.js
371 lines (334 loc) · 14.1 KB
/
currency.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import {deepSetValue, logError, logInfo, logMessage, logWarn} from '../src/utils.js';
import {getGlobal} from '../src/prebidGlobal.js';
import { EVENTS, REJECTION_REASON } from '../src/constants.js';
import {ajax} from '../src/ajax.js';
import {config} from '../src/config.js';
import {getHook} from '../src/hook.js';
import {defer} from '../src/utils/promise.js';
import {registerOrtbProcessor, REQUEST} from '../src/pbjsORTB.js';
import {timedAuctionHook, timedBidResponseHook} from '../src/utils/perfMetrics.js';
import {on as onEvent, off as offEvent} from '../src/events.js';
import { enrichFPD } from '../src/fpd/enrichment.js';
import { timeoutQueue } from '../libraries/timeoutQueue/timeoutQueue.js';
const DEFAULT_CURRENCY_RATE_URL = 'https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=$$TODAY$$';
const CURRENCY_RATE_PRECISION = 4;
const MODULE_NAME = 'currency';
let ratesURL;
let bidResponseQueue = [];
let conversionCache = {};
let currencyRatesLoaded = false;
let needToCallForCurrencyFile = true;
let adServerCurrency = 'USD';
export var currencySupportEnabled = false;
export var currencyRates = {};
let bidderCurrencyDefault = {};
let defaultRates;
export let responseReady = defer();
const delayedAuctions = timeoutQueue();
let auctionDelay = 0;
/**
* Configuration function for currency
* @param {object} config
* @param {string} [config.adServerCurrency = 'USD']
* ISO 4217 3-letter currency code that represents the target currency. (e.g. 'EUR'). If this value is present,
* the currency conversion feature is activated.
* @param {number} [config.granularityMultiplier = 1]
* A decimal value representing how mcuh to scale the price granularity calculations.
* @param {object} config.bidderCurrencyDefault
* An optional argument to specify bid currencies for bid adapters. This option is provided for the transitional phase
* before every bid adapter will specify its own bid currency. If the adapter specifies a bid currency, this value is
* ignored for that bidder.
*
* example:
* {
* rubicon: 'USD'
* }
* @param {string} [config.conversionRateFile = 'URL pointing to conversion file']
* Optional path to a file containing currency conversion data. Prebid.org hosts a file that is used as the default,
* if not specified.
* @param {object} [config.rates]
* This optional argument allows you to specify the rates with a JSON object, subverting the need for a external
* config.conversionRateFile parameter. If this argument is specified, the conversion rate file will not be loaded.
*
* example:
* {
* 'GBP': { 'CNY': 8.8282, 'JPY': 141.7, 'USD': 1.2824 },
* 'USD': { 'CNY': 6.8842, 'GBP': 0.7798, 'JPY': 110.49 }
* }
* @param {object} [config.defaultRates]
* This optional currency rates definition follows the same format as config.rates, however it is only utilized if
* there is an error loading the config.conversionRateFile.
*/
export function setConfig(config) {
ratesURL = DEFAULT_CURRENCY_RATE_URL;
if (config.rates !== null && typeof config.rates === 'object') {
currencyRates.conversions = config.rates;
currencyRatesLoaded = true;
needToCallForCurrencyFile = false; // don't call if rates are already specified
}
if (config.defaultRates !== null && typeof config.defaultRates === 'object') {
defaultRates = config.defaultRates;
// set up the default rates to be used if the rate file doesn't get loaded in time
currencyRates.conversions = defaultRates;
currencyRatesLoaded = true;
}
if (typeof config.adServerCurrency === 'string') {
auctionDelay = config.auctionDelay;
logInfo('enabling currency support', arguments);
adServerCurrency = config.adServerCurrency;
if (config.conversionRateFile) {
logInfo('currency using override conversionRateFile:', config.conversionRateFile);
ratesURL = config.conversionRateFile;
}
// see if the url contains a date macro
// this is a workaround to the fact that jsdelivr doesn't currently support setting a 24-hour HTTP cache header
// So this is an approach to let the browser cache a copy of the file each day
// We should remove the macro once the CDN support a day-level HTTP cache setting
const macroLocation = ratesURL.indexOf('$$TODAY$$');
if (macroLocation !== -1) {
// get the date to resolve the macro
const d = new Date();
let month = `${d.getMonth() + 1}`;
let day = `${d.getDate()}`;
if (month.length < 2) month = `0${month}`;
if (day.length < 2) day = `0${day}`;
const todaysDate = `${d.getFullYear()}${month}${day}`;
// replace $$TODAY$$ with todaysDate
ratesURL = `${ratesURL.substring(0, macroLocation)}${todaysDate}${ratesURL.substring(macroLocation + 9, ratesURL.length)}`;
}
initCurrency();
} else {
// currency support is disabled, setting defaults
auctionDelay = 0;
logInfo('disabling currency support');
resetCurrency();
}
if (typeof config.bidderCurrencyDefault === 'object') {
bidderCurrencyDefault = config.bidderCurrencyDefault;
}
}
config.getConfig('currency', config => setConfig(config.currency));
function errorSettingsRates(msg) {
if (defaultRates) {
logWarn(msg);
logWarn('Currency failed loading rates, falling back to currency.defaultRates');
} else {
logError(msg);
}
}
function loadRates() {
if (needToCallForCurrencyFile) {
needToCallForCurrencyFile = false;
currencyRatesLoaded = false;
ajax(ratesURL,
{
success: function (response) {
try {
currencyRates = JSON.parse(response);
logInfo('currencyRates set to ' + JSON.stringify(currencyRates));
conversionCache = {};
currencyRatesLoaded = true;
processBidResponseQueue();
delayedAuctions.resume();
} catch (e) {
errorSettingsRates('Failed to parse currencyRates response: ' + response);
}
},
error: function (...args) {
errorSettingsRates(...args);
currencyRatesLoaded = true;
processBidResponseQueue();
delayedAuctions.resume();
needToCallForCurrencyFile = true;
}
}
);
} else {
processBidResponseQueue();
}
}
function initCurrency() {
conversionCache = {};
if (!currencySupportEnabled) {
currencySupportEnabled = true;
// Adding conversion function to prebid global for external module and on page use
getGlobal().convertCurrency = (cpm, fromCurrency, toCurrency) => parseFloat(cpm) * getCurrencyConversion(fromCurrency, toCurrency);
getHook('addBidResponse').before(addBidResponseHook, 100);
getHook('responsesReady').before(responsesReadyHook);
enrichFPD.before(enrichFPDHook);
getHook('requestBids').before(requestBidsHook, 50);
onEvent(EVENTS.AUCTION_TIMEOUT, rejectOnAuctionTimeout);
onEvent(EVENTS.AUCTION_INIT, loadRates);
loadRates();
}
}
export function resetCurrency() {
if (currencySupportEnabled) {
getHook('addBidResponse').getHooks({hook: addBidResponseHook}).remove();
getHook('responsesReady').getHooks({hook: responsesReadyHook}).remove();
enrichFPD.getHooks({hook: enrichFPDHook}).remove();
getHook('requestBids').getHooks({hook: requestBidsHook}).remove();
offEvent(EVENTS.AUCTION_TIMEOUT, rejectOnAuctionTimeout);
offEvent(EVENTS.AUCTION_INIT, loadRates);
delete getGlobal().convertCurrency;
adServerCurrency = 'USD';
conversionCache = {};
currencySupportEnabled = false;
currencyRatesLoaded = false;
needToCallForCurrencyFile = true;
currencyRates = {};
bidderCurrencyDefault = {};
responseReady = defer();
}
}
function responsesReadyHook(next, ready) {
next(ready.then(() => responseReady.promise));
}
export const addBidResponseHook = timedBidResponseHook('currency', function addBidResponseHook(fn, adUnitCode, bid, reject) {
if (!bid) {
return fn.call(this, adUnitCode, bid, reject); // if no bid, call original and let it display warnings
}
let bidder = bid.bidderCode || bid.bidder;
if (bidderCurrencyDefault[bidder]) {
let currencyDefault = bidderCurrencyDefault[bidder];
if (bid.currency && currencyDefault !== bid.currency) {
logWarn(`Currency default '${bidder}: ${currencyDefault}' ignored. adapter specified '${bid.currency}'`);
} else {
bid.currency = currencyDefault;
}
}
// default to USD if currency not set
if (!bid.currency) {
logWarn('Currency not specified on bid. Defaulted to "USD"');
bid.currency = 'USD';
}
// used for analytics
bid.getCpmInNewCurrency = function(toCurrency) {
return (parseFloat(this.cpm) * getCurrencyConversion(this.currency, toCurrency)).toFixed(3);
};
// execute immediately if the bid is already in the desired currency
if (bid.currency === adServerCurrency) {
return fn.call(this, adUnitCode, bid, reject);
}
bidResponseQueue.push([fn, this, adUnitCode, bid, reject]);
if (!currencySupportEnabled || currencyRatesLoaded) {
processBidResponseQueue();
}
});
function rejectOnAuctionTimeout({auctionId}) {
bidResponseQueue = bidResponseQueue.filter(([fn, ctx, adUnitCode, bid, reject]) => {
if (bid.auctionId === auctionId) {
reject(REJECTION_REASON.CANNOT_CONVERT_CURRENCY)
} else {
return true;
}
});
}
function processBidResponseQueue() {
while (bidResponseQueue.length > 0) {
const [fn, ctx, adUnitCode, bid, reject] = bidResponseQueue.shift();
if (bid !== undefined && 'currency' in bid && 'cpm' in bid) {
let fromCurrency = bid.currency;
try {
let conversion = getCurrencyConversion(fromCurrency);
if (conversion !== 1) {
bid.cpm = (parseFloat(bid.cpm) * conversion).toFixed(4);
bid.currency = adServerCurrency;
}
} catch (e) {
logWarn('getCurrencyConversion threw error: ', e);
reject(REJECTION_REASON.CANNOT_CONVERT_CURRENCY);
continue;
}
}
fn.call(ctx, adUnitCode, bid, reject);
}
responseReady.resolve();
}
function getCurrencyConversion(fromCurrency, toCurrency = adServerCurrency) {
var conversionRate = null;
var rates;
let cacheKey = `${fromCurrency}->${toCurrency}`;
if (cacheKey in conversionCache) {
conversionRate = conversionCache[cacheKey];
logMessage('Using conversionCache value ' + conversionRate + ' for ' + cacheKey);
} else if (currencySupportEnabled === false) {
if (fromCurrency === 'USD') {
conversionRate = 1;
} else {
throw new Error('Prebid currency support has not been enabled and fromCurrency is not USD');
}
} else if (fromCurrency === toCurrency) {
conversionRate = 1;
} else {
if (fromCurrency in currencyRates.conversions) {
// using direct conversion rate from fromCurrency to toCurrency
rates = currencyRates.conversions[fromCurrency];
if (!(toCurrency in rates)) {
// bid should fail, currency is not supported
throw new Error('Specified adServerCurrency in config \'' + toCurrency + '\' not found in the currency rates file');
}
conversionRate = rates[toCurrency];
logInfo('getCurrencyConversion using direct ' + fromCurrency + ' to ' + toCurrency + ' conversionRate ' + conversionRate);
} else if (toCurrency in currencyRates.conversions) {
// using reciprocal of conversion rate from toCurrency to fromCurrency
rates = currencyRates.conversions[toCurrency];
if (!(fromCurrency in rates)) {
// bid should fail, currency is not supported
throw new Error('Specified fromCurrency \'' + fromCurrency + '\' not found in the currency rates file');
}
conversionRate = roundFloat(1 / rates[fromCurrency], CURRENCY_RATE_PRECISION);
logInfo('getCurrencyConversion using reciprocal ' + fromCurrency + ' to ' + toCurrency + ' conversionRate ' + conversionRate);
} else {
// first defined currency base used as intermediary
var anyBaseCurrency = Object.keys(currencyRates.conversions)[0];
if (!(fromCurrency in currencyRates.conversions[anyBaseCurrency])) {
// bid should fail, currency is not supported
throw new Error('Specified fromCurrency \'' + fromCurrency + '\' not found in the currency rates file');
}
var toIntermediateConversionRate = 1 / currencyRates.conversions[anyBaseCurrency][fromCurrency];
if (!(toCurrency in currencyRates.conversions[anyBaseCurrency])) {
// bid should fail, currency is not supported
throw new Error('Specified adServerCurrency in config \'' + toCurrency + '\' not found in the currency rates file');
}
var fromIntermediateConversionRate = currencyRates.conversions[anyBaseCurrency][toCurrency];
conversionRate = roundFloat(toIntermediateConversionRate * fromIntermediateConversionRate, CURRENCY_RATE_PRECISION);
logInfo('getCurrencyConversion using intermediate ' + fromCurrency + ' thru ' + anyBaseCurrency + ' to ' + toCurrency + ' conversionRate ' + conversionRate);
}
}
if (!(cacheKey in conversionCache)) {
logMessage('Adding conversionCache value ' + conversionRate + ' for ' + cacheKey);
conversionCache[cacheKey] = conversionRate;
}
return conversionRate;
}
function roundFloat(num, dec) {
var d = 1;
for (let i = 0; i < dec; i++) {
d += '0';
}
return Math.round(num * d) / d;
}
export function setOrtbCurrency(ortbRequest, bidderRequest, context) {
if (currencySupportEnabled) {
ortbRequest.cur = ortbRequest.cur || [context.currency || adServerCurrency];
}
}
registerOrtbProcessor({type: REQUEST, name: 'currency', fn: setOrtbCurrency});
function enrichFPDHook(next, fpd) {
return next(fpd.then(ortb2 => {
deepSetValue(ortb2, 'ext.prebid.adServerCurrency', adServerCurrency);
return ortb2;
}))
}
export const requestBidsHook = timedAuctionHook('currency', function requestBidsHook(fn, reqBidsConfigObj) {
const continueAuction = ((that) => () => fn.call(that, reqBidsConfigObj))(this);
if (!currencyRatesLoaded && auctionDelay > 0) {
delayedAuctions.submit(auctionDelay, continueAuction, () => {
logWarn(`${MODULE_NAME}: Fetch attempt did not return in time for auction ${reqBidsConfigObj.auctionId}`)
continueAuction();
});
} else {
continueAuction();
}
});