Try it free

How to implement e-commerce tracking

E-commerce tracking with AFS Analytics

Thanks to the analytics.js library, AFS Analytics, web analytics solution, offers numerous features making the tracking of an E-Commerce website a breeze. This means, the capture of transactions and the generation of analyzes, to monitor the performance and the evolution of the generated revenues. The installation of AFS Analytics can boost the efficiency and return on investment of online shops.

This guide explains how to use the various e-commerce functions offered by the analytics.js library.

e-commerce tracking with web analytics

The benefits of implementing e-commerce tracking

Implementing transaction tracking on your site will give you access to many 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 or department.
  • Conversion rate.
  • Return on investment of marketing campaigns.
  • Cost of acquiring a customer.
  • Number of days leading to a transaction.
  • Profile of buyers.
  • Thanks to all these analyzes, you will see in real time the trends of the day, the duds and the items that sell well.

    What are the steps of a successful transaction?

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

  • 1. The visitor validates the order.
  • 2. Payment processing through your e-commerce platform. This is verification and acceptance of payment.
  • 3. The transaction is accepted; the buyer is redirected to the thank you page of your website. This page can be generated by the e-commerce platform and displayed by your site.
  • Note: The "thank you page" is very important because it is on this page that you must add the AFS Analytics code for sending data. Once the data is received by AFS, it will be processed in real time.

    The javaScript code to add to the Thank Page

    .

    The transmission of transaction data takes place in three steps. You must first verify that the AFS Analytics tracking code is 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
  • 1. Add transaction

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

    addTransaction syntax:
    
    aa('set', 'addTransaction', {
    'Id': '1234567',
    'Affiliation': 'storename',
    'Revenue': '120.90',
    'Shipping': '12 .65 ',
    'Tax': '5.55',
    'Currency': 'EUR'
    });
    

    The addTransaction fields Only the id, affiliation and revenue fields are required. If the tax or shipping fields are not filled in, they will be set with the value 0. currency defaults to the currency in your AFS Analytics account.

    The "id" field

    This is the unique identifier of the transaction. If the value is -1, 0 or auto. AFS Analytics will generate a unique identifier.

    The "affiliation" field

    The name of the store or affiliate where the transaction originated. Example: 'Toys and Co'

    The "revenue" field

    This field specifies 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).

    The "shipping" field

    This field specifies the transport cost. This is a numeric value, the decimal part is separated by a point.

    The "tax" field

    This field specifies the total taxes. In Europe, it is Value Added tax or VAT. This is a numeric value

    The "Currency" field

    This field indicates the currency of the transaction. It's the ISO code 4217, which is a 3-letter code. For example: USD for US Dollar, EUR for EURO and GBP for pound sterling.

    Syntax of the simplified version:

    As always it is possible to use the simplified version:

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

    2. Add a list of items ordered.

    Once you have specified the transaction details, you can specify the items ordered. We use the command set followed by the keyword addItem. The third argument is an object detailing the product specifications.

    Syntax of addItem
    
    aa('set', 'addItem',
    {
    'Id': '0',
    'Name': 'Product1',
    'Sku': '001',
    'Category': 'cat1',
    'Price': '100',
    'Quantity': "1"
    });
    
    The "id" field

    The id field is the identifier of the transaction. The identifier is therefore the same as the one set for addTransaction. If ID has a value of 0 or -1, the identifier will be initialized with the value set previously in addTransaction.

    The "name" field

    This field specifies the name of the item.

    The "sku" field

    This field specifies the reference or code of the item.

    The category field

    This field specifies the category in which the item belongs.

    The "price" field

    This field specifies the price of the item by unit. The decimal part must be separated by a dot.

    The "quantity" field

    This field specifies the quantity purchased. This is an integer value.

    The fields id and name are required. The price and quantity fields are optional but strongly recommended. If they are not filled in, the price will be set to0 and the quantity to 1. The fields SKU and category are recommended. The currency field is optional.

    Syntax of the simplified version:
    
    aa('set', 'addItem', 'id', 'name', 'sku', 'category', 'price', 'quantity');
    aa('set', 'addItem', '1234567', 'Drone Aircraft', 'SKU00358', 'Drone', '256.56', '1');
    

    3. Sending data to AFS Analytics

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

    Syntax:
    
    aa('send', 'ecommerce', [callback], [params];
    </code>
    <p>
    The <mtag>callback</mtag> and <mtag>params</mtag> arguments are optional.
    </p>
    <b>Sending all the data:</b>
    <code<
    aa('send', 'ecommerce');
    
    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>
    

    Case where the thank you page is hosted on the e-commerce platform

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

    If the thank you page is hosted by the payment platform, you must 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.

    1. Getting the visitor's cookie on the page sending on the e-commerce 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');
    
    // get the visitor's cookie on the order form of your site
    // record the cookie in the visitorCookie variable.
    
    Var visitorCookie;
    aa('create', 'XXXXXXXX', 'auto');
    aa('send', 'pageview');
    aa(function (tracker){
    VisitorCookie = tracker.get ('cookie.str');
      // alert (visitorCookie);
    });
    </ script>
    

    2. Sending the cookie to the e-commerce platform:

    When sending the data to the e-commerce 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
    

    3. 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 page 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>
    ...
    ...
    //We add the 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');
    //We send the 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 toy’,'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>
    

    The PHP API

    In some cases, it is not possible to execute "JavaScript" code, for example when transactions are managed by 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.

    By AFS Analytics, Wednesday, May 17, 2017