Skip to main content
All CollectionsSales TrackingLink Tracking
Multiple Currency Support for Link Tracking Offers
Multiple Currency Support for Link Tracking Offers

How to convert non-USD sale amounts to USD using an exchange rate API and server postback tracking

Heather Clark avatar
Written by Heather Clark
Updated over 3 months ago

Overview

Aspire currently tracks all sale amounts in USD and does not automatically convert non-USD amounts to USD currency. This article is intended to serve as an example of how a brand could use a custom solution to convert order amounts to USD before sending the value to Aspire to record a conversion.

Note: This is required for any stores that accept multiple currencies.

Requirements

  1. Technical ability to write custom Javascript before the conversion is recorded.

  2. A Link Tracking Offer with the Conversion Tracking method set to either Server Postback with Transaction ID, Server Postback with Partner ID, or Javascript Postback.

  3. An exchange rate API service.

    1. The example script in this article uses Exchange Rates API. Please note that you must upgrade to a paid plan to use Exchange Rates API for this purpose. Their Basic plan (currently $10/month) provides up to 10,000 API requests per month and supports all base currencies.

Directions

Before calling the postback URL or Javascript SDK to indicate a conversion, you must use the Exchange Rate API (or a similar service) to convert the order amount to USD. After converting, you will pass the new USD order amount to the Aspire Postback URL or Javascript SDK.

The example script below is intended for Shopify brands using Javascript SDK and Exchange Rates API. This script would be added as a Custom Pixel under Customer Events in Shopify. More information on using Javascript SDK for Shopify can be found here.

⚠️ Important: The following script is not one-size-fits-all and is merely given as an example to help your technical team implement a solution that meets your needs. This script is specific to Shopify brands using Exchange Rates API.

Example Script for Shopify Brand using Exchange Rates API

Before using this script, you must enter the API key you received from Exchange Rates API in the placeholder value YOUR API KEY HERE within the code snippet where it says:
const EXCHANGE_RATES_API_KEY = 'YOUR API KEY HERE';

Note: If you have multiple Shopify stores that support non-USD store currencies, you must add this script to each Shopify store.

!function(){
var o = window.tdl = window.tdl || [];
if (o.invoked) window.console && console.error && console.error("Tune snippet has been included more than once.");
else {
o.invoked = !0;
o.methods = ["init", "identify", "convert"];
o.factory = function(n){
return function(){
var e = Array.prototype.slice.call(arguments);
e.unshift(n), o.push(e), o
}
};
for(var e = 0; e < o.methods.length; e++){
var n = o.methods[e];
o[n] = o.factory(n)
}
o.init = function(e){
var n = document.createElement("script");
n.type = "text/javascript", n.async = !0;
n.src = "https://js.go2sdk.com/v2/tune.js";
var t = document.getElementsByTagName("script")[0];
t.parentNode.insertBefore(n, t), o.domain = e
}
}
}();


tdl.init("https://aspireiq.go2cloud.org")
analytics.subscribe('checkout_completed', (event) => {
async function convertToUSD(currencyCodeFrom, amount) {
// Documentation for this API: https://exchangeratesapi.io/documentation/
const EXCHANGE_RATES_API_KEY = 'YOUR API KEY HERE';
const response = await fetch(
`https://api.exchangeratesapi.io/v1/convert?access_key=${EXCHANGE_RATES_API_KEY}&from=${currencyCodeFrom}&to=USD&amount=${amount}`,
{ method: "GET" }
);
const result = await response.json();
return result.result;
}

async function recordConversion(){
const cartSubtotalCurrencyCode = event.data.checkout.subtotalPrice.currencyCode;

let cartSubtotal = event.data.checkout.subtotalPrice.currencyCode;


let cartSubtotalUSD = cartSubtotal;

if (cartSubtotalCurrencyCode !== "USD") {
try {
cartSubtotalUSD = await convertToUSD(
cartSubtotalCurrencyCode,
cartSubtotal
);
} catch (error) {
cartSubtotalUSD = 0;
console.error("Error when converting cart subtotal to USD", error);
}
}
console.log({cartSubtotalUSD})
tdl.convert({
'amount': cartSubtotalUSD,
'adv_unique1': event.data.checkout.order.id,
});
}
recordConversion()
});
Did this answer your question?