Convert currency with a custom event

Tealeaf provides an $S function to get a currency conversion rate between a base (baseCurrency) and target currency (targetCurrency).

As an international company, you get payments from customers all over the world. However, you have to be able to see what those payments mean to your own currency. You can create this custom event to take any source currency and convert it to the target currency that you want to see it in for reporting purposes.

Note: This custom event assumes that you are already capturing the target currency in your code. For example, you live in China and your site is already capturing all payments made in yuan. And now you want to convert all the payments from the United States dollar.

$S.getCurrencyRate(String baseCurrency, String targetCurrency)

  • param baseCurrency - the base currency code, for example USD.
  • param targetCurrency - the target currency code, for example CNY.
  • return: the function returns double value as the exchange rate of 1 baseCurrency to N targetCurrency, for example 1 USD = 7 CNY.
  • In this example, getCurrencyRate("USD","CNY")=7. If there is any invalid code, it will return 0, because there is an error. Otherwise, all other conversions will be greater than zero.

To create a custom event to convert currency:

  1. Use the event that already captures the order value in Yen. We'll call this order_value with the event name E_ORDER_VALUE_1595573059448.
  2. Define a hit attribute event to capture the source currency in which the payment is made. For this example, we'll call this source_currency with the name P_SOURCE_CURRENCY_1590662966584.
  3. Define a third event that has order_value and source_currency as triggers that is called order_value_converted, with the name E_ORDER_VALUE_CONVERTED_1595570208827.
  4. In the advanced mode of the order_value_converted event, write the code to convert the source currency to the target currency "USD", as in the example below.

In the code example:

  • target_currency defined USD as the target currency to convert to;
  • source_currency is the original currency code captured in the user session;
  • rate is the conversion rate between source currency and target currency;
{
if ($F.getLastHitNumber("E_ORDER_VALUE_1595573059448") == $H.HitNumber && $P["P_SOURCE_CURRENCY_1590662966584"].patternFound()){
  var target_currency="USD";
  var source_currency=$P["P_SOURCE_CURRENCY_1590662966584"].firstValue();
  var rate=$S.getCurrencyRate(source_currency, target_currency);
  var order_value_converted=$F.getFirstEventFirstNumericValue("E_ORDER_VALUE_1595573059448") * rate;   
  $F.setFacts("E_ORDER_VALUE_CONVERTED_1595570208827",order_value_converted); }
}

The following currency codes are supported:

  • Arab Emirates dirham: AED
  • Afghan afghani: AFN
  • Albania lek: ALL
  • Armenian dram: AMD
  • Netherlands Antillean guilder: AOA
  • Argentine pesos: ARS
  • Australian dollar: AUD
  • Aruban florin: AWG
  • Azerbaijani manat: AZN
  • Bosnia-Herzegovina convertible mark: BAM
  • Bajan dollar: BBD
  • Bangladeshi taka: BDT
  • Bulgarian Iev: BGN
  • Bahraini dinar: BHD
  • Burundian franc: BIF
  • Bermudan dollar: BMD
  • Brunei dollar: BND
  • Bolivian boliviano: BOB
  • Brazilian real: BRL
  • Bahamian dollar: BSD
  • Bitcoin: BTC
  • Bhutan: BTN
  • Botswanan pula: BWP
  • Belarusian ruble: BYN
  • Belarusian ruble: BYR
  • Belize dollar: BZD
  • Canadian dollar: CAD
  • Congolese franc: CDF
  • Swiss franc: CHF
  • Chilean unit of account: CLF
  • Chilean peso: CLP
  • Chinese yuan: CNY
  • Colombian peso: COP
  • Costa Rican colón: CRC
  • Cuban peso: CUC
  • Cuban peso: CUP
  • Cape Verdean escudo: CVE
  • Czech koruna: CZK
  • Djiboutian franc: DJF
  • Danish krone: DKK
  • Dominican peso: DOP
  • Algerian dinar: DZD
  • Egyptian pound: EGP
  • Eritrea nafka: ERN
  • Ethiopian birr: ETB
  • Euro: EUR
  • Fijian dollar: FJD
  • Falkland Islands pound: FKP
  • Great British pound: GBP
  • Georgian lari: GEL
  • Guernsey pound: GGP
  • Ghanaian cedi: GHS
  • Gibraltar pound: GIP
  • Gambian dalasi: GMD
  • Guinean franc: GNF
  • Guatemalan quetzal: GTQ
  • Guyanese dollar: GYD
  • Hong Kong dollar: HKD
  • Honduran lempira: HNL
  • Croatian kuna: HRK
  • Haitian gourde: HTG
  • Hungarian forint: HUF
  • Indonesian rupiah: IDR
  • Israeli new shekel: ILS
  • Isle of Man pound: IMP
  • Indian rupee: INR
  • Iraqi dinar: IQD
  • Iranian rial: IRR
  • Icelandic króna: ISK
  • Jersey pound: JEP
  • Jamaican dollar: JMD
  • Jordanian dinar: JOD
  • Japanese yen: JPY
  • Kenyan shilling: KES
  • Kyrgystani som: KGS
  • Cambodian riel: KHR
  • Comorian franc: KMF
  • North Korean won: KPW
  • Korean Republic won: KRW
  • Kuwaiti dinar: KWD
  • Cayman Islands dollar: KYD
  • Kazakhstani tenge: KZT
  • Laotian kip: LAK
  • Lebanese pound: LBP
  • Sri Lankan rupee: LKR
  • Liberian dollar: LRD
  • Lesotho loti: LSL
  • Lithuanian litas: LTL
  • Latvian lats: LVL
  • Libyan dinar: LYD
  • Moroccan dirham: MAD
  • Moldovan leu: MDL
  • Malagasy ariary: MGA
  • Macedonian denar: MKD
  • Myanmar kyat: MMK
  • Mongolian tögrög: MNT
  • Macanese pataca: MOP
  • Mauritanian ouguiya: MRO
  • Mauritian rupee: MUR
  • Maldivian rufiyaa: MVR
  • Malawian kwacha: MWK
  • Mexican peso: MXN
  • Malaysian ringgit: MYR
  • Mozambique metical: MZN
  • Namibian dollar: NAD
  • Nigerian naira: NGN
  • Nicaraguan córdoba: NIO
  • Norwegian krone: NOK
  • Nepalese rupee: NPR
  • New Zealand dollar: NZD
  • Omani rial: OMR
  • Panamanian balboa: PAB
  • Peruvian sol: PEN
  • Papua New Guinean kina: PGK
  • Philippine peso: PHP
  • Pakistani rupee: PKR
  • Poland złoty: PLN
  • Paraguayan guarani: PYG
  • Qatari rial: QAR
  • Romanian leu: RON
  • Serbian dinar: RSD
  • Russian ruble: RUB
  • Rwandan franc: RWF
  • Saudi riyal: SAR
  • Solomon Islands dollar: SBD
  • Seychellois rupee: SCR
  • Sudanese pound: SDG
  • Swedish krona: SEK
  • Singapore dollar: SGD
  • Saint Helenian pound: SHP
  • Sierra Leonean leone: SLL
  • Somali shilling: SOS
  • Surinamese dollar: SRD
  • São Tomé and Príncipe dobra: STD
  • Salvadoran Colón: SVC
  • Syrian pound: SYP
  • Swazi lilangeni: SZL
  • Thai baht: THB
  • Tajikistani somoni: TJS
  • Turkmenistan manat: TMT
  • Tunisian dinar: TND
  • Tongan pa'anga: TOP
  • Turkish lira: TRY
  • Trinidad & Tobago dollar: TTD
  • New Taiwan dollar: TWD
  • Tanzanian shilling: TZS
  • Ukrainian hryvnia: UAH
  • Ugandan shilling: UGX
  • United States dollar: USD
  • Uruguayan peso: UYU
  • Uzbekistani som: UZS
  • Venezuelan bolivar: VEF
  • Vietnamese dong: VND
  • Vanuatu vatu: VUV
  • Samoan tala: WST
  • Central African CFA franc: XAF
  • Silver ounce rate: XAG
  • Gold: XAU
  • East Caribbean dollar: XCD
  • Special drawing rights: XDR
  • West African CFA franc: XOF
  • Central Pacific franc: XPF
  • Yemeni rial: YER
  • South African rand: ZAR
  • Zambian kwacha: ZMK
  • Zambian kwacha: ZMW
  • Zimbabwean dollar: ZWL