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.
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.
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',
'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:
| Value | Type | Required | Description |
|---|---|---|---|
order_id | String | Yes | Unique shopping-cart order ID or transaction number used to reference the purchase you're reporting. |
shipping | Number | No | Total shipping and handling the customer was charged for the order. |
tax | Number | No | Total tax the customer was charged for the order. |
discount | Number | No | Total in discounts that were applied to the order. |
discount_code | String | No | The discount or coupon code that was used on the order. A code name is required if you send discount. |
currency_code | String | Yes | The three-letter currency code of the order totals you are reporting. Example: USD, CAD, GBP. |
is_subscription | Boolean | No | Include this flag if the order is a subscription and has a subscription ID. |
subscription_id | String | No | Subscription 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:
| Value | Type | Required | Description |
|---|---|---|---|
first_name | String | No | Customer's first name. |
last_name | String | No | Customer's last name. Required if you send first_name. |
email | String | No | Customer's email address. |
ip_address | String | No | The IP address of the customer. |
Item data
An item represents an individual product the customer ordered, and contains the following values:
| Value | Type | Required | Description |
|---|---|---|---|
sku | String | Yes | A unique product SKU or identifier. Can be blank, but we highly recommend populating it. |
quantity | Number | Yes | Total quantity ordered of the product. |
price | Number | Yes | Price of each item. For example, if the customer ordered 10 items at $5 each, report 5, not 50. Do not include currency symbols. |
Related
- Order Tracking overview — how visit tracking and order reporting fit together.
- Server-side tracking — the alternative to client-side JavaScript.
- API Reference — how to authenticate, and every endpoint the Refersion API exposes.