Skip to content
Implementing ECommerce tracking

ECommerce tracking

With analytics.js, AFS Analytics is offering advanced features making the tracking of an ECommerce website a breeze. By capturing transactions and generating analyzes it helps monitor the performance of an ecommerce website as the evolution of generated revenues. This is how AFS Analytics can boost online shops efficiency and return on investment.

This guide explains how to use the various ECommerce functions offered by analytics.js.

Benefits of implementing ECommerce tracking

Implementing transaction tracking on your site will give you access to an impressive list of reports:

  • Total revenue generated
  • Revenue generated for each product
  • List and quantity of products sold
  • Total products sold
  • Number of transactions
  • Details of transactions by store and department
  • Conversion rate
  • Marketing campaigns return on investment
  • Customer acquisition cost
  • Number of days leading to a transaction
  • Buyers profile

All these reports will help you monitor in real time your eCommerce site performances, observe trends over customisable periodd of time, detect the products that sell well, and those wich subperform.

Steps of a successful transaction

Let's review the steps involved in sending a transaction to AFS Analytics.

  1. The visitor validates the order.
  2. Payment processing through your ECommerce platform. This thr payment verification and acceptance step.
  3. The transaction is accepted; the buyer is redirected to your website thank you page. This page can be generated by an eCommerce platform and displayed by your site.

Note

The thank you page is the page where AFS Analytics code has to be added in order to send completed order data. Once the data is received by AFS, it will be processed in real time.

Adding the javascript code to the Thank Page

The transmission of transaction data takes place in three steps and requires the AFS Analytics tracking code to be present on the page.

The different steps of sending a transaction:

  1. Add the transaction.
  2. Add the items.
  3. Send the data to AFS Analytics

Adding transaction

The first step is to specify the transaction data. We use the command set followed by the addTransaction keyword. The third argument is an object describing the transaction.

Syntax:

aa("set", "addTransaction", {
"Id": "1234567",
"Affiliation": "storename",
"Revenue": "120.90",
"Shipping": "12 .65 ",
"Tax": "5.55",
"Currency": "EUR"
});
Field Description
id* This is the unique identifier of the transaction. If the value is -1, 0 or auto AFS Analytics will generate a unique identifier.
affiliation* The name of the store or affiliate where the transaction originated. Example: "Toys and Co"
revenue* The total revenue of the transaction excluding tax and excluding shipping costs. AFS calculates the total of the transaction by adding revenue + shipping + tax. This is a numeric value, the decimal part is separated by a period (for example: 118.95).
shipping The transport cost as a numeric value, the decimal part being separated by a point.
tax Total taxes as a numeric value. In Europe, this is the Value Added tax or VAT.
currency This field indicates the currency of the transaction ( format: ISO code 4217 ). For example: USD for US Dollar, EUR for EURO and GBP for pound sterling.

Note

Only the id, affiliation and revenue fields are required. If the tax or shipping fields are not filled in, they will be set to 0. When not specified, currency is set by default to the currency defined in your AFS Analytics account settings.

Simplified version syntax:

aa("set", "addTransaction", id, affiliation, revenue, shipping, tax, currency);
aa("set", 
    "addTransaction", 
    "1234567", 
    "storename", 
    "120.90", 
    "12.65", 
    "5.55", 
    "eur"
);

Adding ordered items

Once the transaction details specified, you can list the ordered items by using the set command followed by the addItem keyword. The third argument being an object detailing the product specifications.

Syntax:

aa("set", "addItem",
{
"Id": "0",
"Name": "Product1",
"Sku": "001",
"Category": "cat1",
"Price": "100",
"Quantity": "1"
});
Field Description
id* Transaction identifier. This is the same identifier as the one set with addTransaction. If ID has a value of 0 or -1, the identifier will be automatically set.
name* Specifies the name of the item.
sku Reference or code for this product.
category category to which the item belongs.
price unit price of the product ( decimal part being separated by a dot).
quantity the purchased quantity. This is an integer value.

Note

Only id and name fields are required. price and quantity are optional but strongly recommended. When bot specified, the price will be set to 0 and the quantity to 1. sku and category fields are recommended. The currency field is optional.

Simplified version syntax:

aa("set", "addItem", id, name, sku, category, price, quantity);
aa("set", "addItem", 
    "1234567", 
    "Drone Aircraft", 
    "SKU00358", 
    "Drone", 
    "256.56", 
    "1"
);

Sending data to AFS Analytics

Once the whole data is specified, it's time to send it. This is the last step and sending is done using the send command followed by the ecommerce keyword.

Syntax:

aa("send", "ecommerce", [callback], [params];

Note

callback and params arguments are optional.

Sending all the data:

/* Example of complete code on a "thank page" */

<script type="text/javascript">
(function(i,s,o,g,r,a,m){i["AfsAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//code.afsanalytics.com/js/analytics.js","aa");
aa("create", "XXXXXXXX", "auto");
aa("send", "pageview","titleindex","Thank You");
aa("set","addTransaction","1234567","Toys Shop","417.55","15.50","86.61");
aa("set","addItem",{"id":"1234567","name": "Magic doll","sku": "00252","category": "girl toys","price": "125.56","quantity":"1"});
aa("set","addItem",{"id":"1234567","name": "bear plush","sku": "00582","category": "Plush","price": "35.99","quantity":"1"});
aa("set","addItem","1234567","Drone","00358","Drones","256","1");
aa("send","ecommerce");
</script>

Note

The cookie placed by AFS Analytics on your visitors computer is only accessible from your own domain name.

If the thank you page is hosted by the payment platform, you will have to add the AFS Analytics code on this page and specify the visitor's cookie. Generally speaking, when the page is generated by the payment platform, you can add code and retrieve variables previously passed to the payment platform.

  • Getting the visitor's cookie on the page sending on the ECommerce platform:
<script type = "text / javascript">
(Function {i, r, a, m) {i ["AfsAnalyticsObject"] = r;
(I), i [r] .l = 1 * new Date (), a = s.createElement (o),
M = s.getElementsByTagName (o) [0]; a.async = 1; a.src = g; m.parentNode.insertBefore (a, m)
}) (Window, document, "script", "// code.afsanalytics.com/js/analytics.js","aa");

/* gets the visitor's cookie on your site order form
   and records it in the visitorCookie variable. */

var visitorCookie;
aa("create", "XXXXXXXX", "auto");
aa("send", "pageview");
aa(function (tracker){
VisitorCookie = tracker.get ("cookie.str");

});
</ script>
  • Sending the cookie to the ECommerce platform:

When sending the data to the ECommerce platform, you add a variable containing the cookie. Consult the guides of your payment platform to learn how personal variables are transmitted.

https://www.maplateformedepaiement.com/?.... &mavariable=visitorCookie
  • Recovery and transmission of the visitorCookie variable

Recovery and transmission of the visitorCookie variable to AFS Analytics on the thank you page hosted by the payment platform.

/* code on the "thank page" hosted by the payment platform. */
<script>
/* The way to retrieve the variable depends on the ecommerce or payment platform */
var visitorCookie=GetVariable("mavariable");
</script>


/* AFS Analytics code  */

<script type="text/javascript">
(function(i,s,o,g,r,a,m){i["AfsAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//code.afsanalytics.com/js/analytics.js","aa");
aa("create", "XXXXXXXX", visitorCookie); //Tip: we send the cookie to AFS Analytics.
aa("send", "pageview","titleindex","Thank page");

/* Sending transaction data */

aa("set","addTransaction","1234567","Toys Shop","417.55","15.50","86.61");
aa("set","addItem",{"id":"1234567","name": "Magic doll","sku": "00252","category": "girl toys","price": "125.56","quantity":"1"});
aa("set","addItem",{"id":"1234567","name": "bear plush","sku": "00582","category": "Plush","price": "35.99","quantity":"1"});
aa("set","addItem","1234567","Drone","00358","Drones","256","1");
aa("send","ecommerce");
</script>

API PHP

In some cases it may not be possible to execute JavaScript code, like for example when transactions are managed via a webhook. There is an API in development to transmit the information to AFS Analytics via PHP, do not hesitate to contact the support to get it.

Last updated on April 8, 2020 17:30:43