How to setup tracking of transactions with multiple currencies?

Jozo KovacUse-cases


Tracking purchases/transactions in multiple currencies can be a challenging task. We have a solution that converts currencies on client using an actual currency conversion table.


Challenge

Let’s have an e-commerce that accepts payments in multiple currencies. Many analytics systems including Exponea allow reporting only in a base currency. So we need to convert currencies before data are sent to analytics.

Analysis
The transaction event is tracked using the following code:

var price = $('#price').text(); // change selector on your website
var currency = $('#currency').text();

exponea.track('transaction', {
  'price': price, 
  'currency': currency
});

Knowing this, we just need to convert the price to the base currency.

Proposed solution

Money.js is a simple JavaScript currency conversion library.

fx.convert(12.99, {from: "GBP", to: "HKD"});

Money.js need a currency conversion table in order to work. Lets use Fixer.io – JSON API for foreign exchange rates.

Putting it all together:

var script = document.createElement('script');
script.src = "//openexchangerates.github.io/money.js/money.min.js";
script.onload = function () {
    $.getJSON("//api.fixer.io/latest", function(data) {
       fx.rates = data.rates;
       fx.base = "EUR";

       var price = $('#price').text(); // change selector on your website
       var currency = $('#currency').text();
       var priceBaseCurrency = fx.convert(price, {from: currency, to: 'EUR'}),

       exponea.track('transaction', {
         'price': price, 
         'currency': currency,
         'base price': priceBaseCurrency, 
         'base currency': 'EUR',
       });
    }); 
};
document.head.appendChild(script);

The code above loads current foreign exchange rates from Fixer, pushes them to Money.JS and coverts the price from any currency to the base currency (EUR). EUR to EUR conversion is also possible.

This script can be used in the Exponea content modifying rules, for an easier tracking of transactions in multiple currencies than ever before.