Skip to main content

JavaScript v4 Tracking

Client-side tracking reports orders straight from the shopper's browser, using JavaScript on your "Thank You" or order confirmation page. It is the simplest way to report orders; if you'd rather report from your backend, use server-side tracking instead.

Step 1 — Track visits

To use v4 JavaScript tracking, first enable it on your account. Go to Account > Settings > Tracking and switch on the Enable V4 Tracking toggle.

The Enable V4 Tracking toggle on the Refersion Tracking settings page, switched on.
v4 tracking exposes your Public key

v4 tracking allows public tracking of conversions, which means your Public API key is visible in the browser. Be aware that this carries a risk of fraudulent orders.

Next, copy the following code into the <head> section of every page on your website where you expect affiliates to drive traffic — including your order confirmation page. Replace YOUR-PUBLIC-KEY with the Public API key from your Refersion account.

<!-- REFERSION TRACKING: BEGIN -->
<script>
! function(e, n, t, i, o, c, s, a) {
e.TrackingSystemObject = "r", (s = n.createElement(t)).async = 1, s.src = "https://cdn.refersion.com/refersion.js", s.onload = function() {

// Replace with your Refersion Public API Key
r.pubKey = "YOUR-PUBLIC-KEY";

// Uncomment next line if you need to debug during testing
// r.settings.dbg_mode = true;

r.initializeXDLS().then(() => {
r.launchDefault().then(() => {

// Send a custom event that can be listened to later
const rfsnTrackingEvent = new Event("refersion-loaded");
document.dispatchEvent(rfsnTrackingEvent);

})
})
}, (a = n.getElementsByTagName(t)[0]).parentNode.insertBefore(s, a)
}(window, document, "script");
</script>
<!-- REFERSION TRACKING: END -->

Step 2 — Send orders from your "Thank You" page

Add the code below to your "Thank You" or order confirmation page — the page every customer reaches after completing an order. Use it as a starting template.

Within this code you must insert the customer's transaction information dynamically from the order. The sample values below are placeholders. Not all fields are required, but all are recommended; each one is described in Variable descriptions.

For best performance, place this code just before the </body> tag in your HTML.

Same domain and security level

For tracking to function, the click-tracking code from Step 1 and the code below must run on the same domain and security level (http/https).

<!-- REFERSION TRACKING: BEGIN -->
<script>
document.addEventListener("refersion-loaded", function() {

r.addTrans({
'order_id': '3123123',
'shipping': '12.90',
'tax': '8.23',
'discount': '4.32',
'discount_code': 'TESTCOUPON',
'currency_code': 'USD',
'is_subscription': true,
'subscription_id': '12345'
});

r.addCustomer({
'first_name': 'Nancy',
'last_name': 'Parker',
'email': '[email protected]',
'ip_address': '808.53.77.22'
});

r.addItems({
'sku': 'DD23444',
'quantity': '2',
'price': '100'
});

r.addItems({
'sku': 'DD25444',
'quantity': '6',
'price': '13'
});

r.addItems({
'sku': 'MP39592',
'quantity': '2',
'price': '1000'
});

r.sendConversion();

})
</script>
<!-- REFERSION TRACKING: END -->

Variable descriptions

Transaction data

A transaction represents the entire order that occurred, and contains the following values:

ValueTypeRequiredDescription
order_idStringYesUnique shopping-cart order ID or transaction number used to reference the purchase you're reporting.
shippingNumberNoTotal shipping and handling the customer was charged for the order.
taxNumberNoTotal tax the customer was charged for the order.
discountNumberNoTotal in discounts that were applied to the order.
discount_codeStringNoThe discount or coupon code that was used on the order. A code name is required if you send discount.
currency_codeStringYesThe three-letter currency code of the order totals you are reporting. Example: USD, CAD, GBP.
is_subscriptionBooleanNoInclude this flag if the order is a subscription and has a subscription ID.
subscription_idStringNoSubscription ID to use for the order when the is_subscription flag is on.

Customer data

A customer represents the individual who purchased, and contains the following values:

ValueTypeRequiredDescription
first_nameStringNoCustomer's first name.
last_nameStringNoCustomer's last name. Required if you send first_name.
emailStringNoCustomer's email address.
ip_addressStringNoThe IP address of the customer.

Item data

An item represents an individual product the customer ordered, and contains the following values:

ValueTypeRequiredDescription
skuStringYesA unique product SKU or identifier. Can be blank, but we highly recommend populating it.
quantityNumberYesTotal quantity ordered of the product.
priceNumberYesPrice of each item. For example, if the customer ordered 10 items at $5 each, report 5, not 50. Do not include currency symbols.