Ecommerce Tracking in OpenCart 2.3

Around 6 months back, I had published an article on “How to add GA e-commerce tracking in Opencart“. I am really thankful to all  viewers specially those who commented on my article and wrote mail about the code that  encouraged me to write back. These are not the only things, when I was searching about E-commerce Tracking extension for Opencart. I found one free extension where developer have mentioned this website for reference and extension was working fine on Opencart 2.0.

Opencart ecommerce

When I went through all responses, I found out there is some issue in those codes which I had mentioned in my previous article. For Opencart 2.0, it is working fine but problem is arising when we are trying to implement that code on higher version of Opencart. Since Opencart 2.3 is available to use, that’s why I am going to work on latest version of Opencart (v2.3).

In this Blog I am Going to work on 2 things:

  • How to add Google Analytics Code in OpenCart 2.3.
  • How to add Google Analytics Ecommerce Tracking in Opencart 2.3.

How to add Google Analytics Code in OpenCart 2.3:

There is one default Extension added to Opencart 2.3. We have to just install, enable and add the Google Analytics Code.

Steps to add Google Analytics Code in Opencart2.3

  • Click on Extensions menu in Opencart admin panel dashboard.
  • Again Click on Extensions sub-menu.
  • Click on Plus sign(+).
Opencart Ecommerce Google-analytics Extensions Enable
Opencart Ecommerce Google-analytics Extensions Enable
  • Click on Edit button.
Opencart Ecommerce - Google Analytics-Extensions-edit
Opencart Ecommerce – Google analytics extensions Edit
  • Add Google Analytics Tracking Code in Textarea.
  • Click on save button.
Opencart Ecommerce - Google Analytics - Add GA Code
Opencart Ecommerce – Google analytics – add Google Analytics Tracking Code

By following above steps you can easily setup Google Analytics(GA) Tracking Code on your website.

How to add Google Analytics Ecommerce Tracking in Opencart 2.3

In this section you will see how setup Google Analytics Ecommerce Tracking code, But before going further please Make sure you have successfully installed GA Tracking code.

Steps to Add Ecommerce Tracking Code in Opencart:

  • Login to FTP.
  • open order.php( file path \catalog\model\checkout\order.php)
  • Add the below code in order.php.
// NOC ecommerce code start here
  public function getOrderTax($order_id){
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE code = 'tax' AND order_id = '" . (int)$order_id . "' LIMIT 1");
    return $query->row;	
  }
  
  public function getOrderShipping($order_id){
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE code = 'shipping' AND order_id = '" . (int)$order_id . "' LIMIT 1");
    return $query->row;	
  }
  
  public function getOrderProducts($order_id){
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
    if($query->num_rows){
        return $query->rows;
    } else {
        return false;	
    }
  }
  
  //NOC ecommerce code end here

Image: Instruction

Opencart Ecommerce Code instruction- order.php
Opencart Ecommerce Code instruction- order.php

Image: After Code Insertion

Opencart ecommerce code – order.php
Opencart ecommerce code – order.php
  • Save order.php.
  • open success.php (file path  \catalog\controller\checkout\success.php).
  • Add the below code in success.php.
//NOC get Order-id
  $order_id = $this->session->data['order_id'];
  
  //NOC get Order-details
  if(isset($order_id))
  {
    //LOAD MODEL
    $this->load->model('checkout/order');
    
    //GET ORDER DETAILS
    $order_info = $this->model_checkout_order->getOrder($order_id);
    
    //NEW MODEL TO COLLECT TAX
    $get_order_tax = $this->model_checkout_order->getOrderTax($order_id);
    
    if($get_order_tax){
        //ASSIGN TAX TO NEW VARIABLE
        $order_tax = $get_order_tax['value'];
    } else {
        //THERE WAS NO TAX COLLECTED
        $order_tax = 0;
    }
    
    //NEW MODEL TO COLLECT SHIPPING
    $get_order_shipping = $this->model_checkout_order->getOrderShipping($order_id);
    
    if($get_order_shipping){
        //ASSIGN SHIPPING TO NEW VARIABLE
        $order_shipping = $get_order_shipping['value'];
    } else {
        //THERE WAS NO SHIPPING COLLECTED
        $order_shipping = 0;
    }
    
    //NEW MODEL TO COLLECT ALL PRODUCTS ASSOCIATED WITH ORDER
    $get_order_products = $this->model_checkout_order->getOrderProducts($order_id);
    
    //CREATE ARRAY TO HOLD PRODUCTS
    $order_products = array();
    
    foreach($get_order_products as $prod){				
    
        $order_products[] = array(
            'order_id'  => $order_id,
            'model'     => $prod['model'],
            'name'      => $prod['name'],
            'category'  => '',
            'price'     => number_format($prod['price'], 2, '.', ','),
            'quantity'  => $prod['quantity']
        );
    
    }
    
    //NEW ORDER ARRAY
    $order_tracker = array(
        'order_id'    => $order_id,
        'store_name'  => $order_info['store_name'],
        'total'       => $order_info['total'],
        'tax'         => $order_tax,
        'shipping'    => $order_shipping,
        'city'        => $order_info['payment_city'],
        'state'       => $order_info['payment_zone'],
        'country'     => $order_info['payment_country'],
        'currency'    => $order_info['currency_code'],
        'products'    => $order_products
    );   
    $data['order_tracker'] = $order_tracker;
  }

Image: Instruction

Opencart Ecommerce Code instruction – success.php
Opencart Ecommerce Code instruction – success.php

Image: After Code Insertion

Opencart Ecommerce Code – success.php
Opencart Ecommerce Code – success.php
  • Save order.php.
  • Open success.tpl (File Path \catalog\view\theme\template name\template\common\success.tpl).
  • Add the below code in success.tpl.
<?php 
 /* NOC Ecommerce Tracking Code in success.tpl file */
 if(isset($order_tracker)){ 

        $tracking_info = '<script type="text/javascript">'.PHP_EOL;
        $tracking_info .= "ga('require', 'ecommerce', 'ecommerce.js');".PHP_EOL;

//ADD TOP LEVEL TRACKING INFO
        $tracking_info .= "ga('ecommerce:addTransaction', {
        id: '" . $order_tracker['order_id'] . "', 
        affiliation: '" . $order_tracker['store_name'] . "',
        revenue: " . $order_tracker['total'] . ", 
        shipping: " . $order_tracker['shipping'] . " , 
        tax: " . $order_tracker['tax'] . " 
        }); ".PHP_EOL;


//ADD INFO FOR EACH PRODUCT
        foreach($order_tracker['products'] as $product){
            $tracking_info .= "ga('ecommerce:addItem', {
            id: '" . $order_tracker['order_id'] . "',
            sku: '" . $product['model'] . "',
            name: '" . $product['name'] . "', 
            category: '', 
            price: " . $product['price'] . ", 
            quantity: " . $product['quantity'] ."
            });".PHP_EOL;
        }

    $tracking_info .= "ga('ecommerce:send');".PHP_EOL;


        $tracking_info .= '</script>'.PHP_EOL;

        echo $tracking_info;

    } 
?>

Image: Instruction

Opencart Ecommerce Code instruction – success.tpl

Image : After Code Insertion

Opencart Ecommerce Code – success.tpl
Opencart Ecommerce Code – success.tpl
  • Save success.tpl.

To confirm that Ecommerce Tracking is working or not, Kindly do a test purchase on chrome browser and verify it in source page as well as Tag Assistant plug-in (By Google).

Image: Final Result

Opencart Ecommerce Final page source code
Opencart Ecommerce Final Page Source Code- Verification

If you have any issues, feel free to leave comment or contact me .

18 thoughts on “Ecommerce Tracking in OpenCart 2.3

  1. Must Works.

    But in my case, I must update also file: /system/storage/modification/catalog/model/checkout/order.php
    cuz I had Proxy error.

  2. Hello

    Thanks for the code.After making the changes i coudnt see the changes in the AG admin.The page value still shows as 0.

    And Tag Assistant plug-in (By Google) paid extension?

  3. Hi Dinesh,

    I am your regularly blog reader.

    I have implemented ecommerce tracking code with the above given instructions to my site and i have done test purchase on chrome browser with the Tag Assistant plug-in (By Google)…unfortunately code is not working. My current opencart site is running on Version 2.1.0.1

    If you could help me out on this it would be great for me.

    Thanks in advance

    Regards
    Harendra

    1. Hi Harendra,
      As I have mention in blog title this code will work on 2.3 and I have tested it in my site, Still no need to worry about that, I think you should take help form http://www.notesonclick.com/blog/add-google-analytics-e-commerce-tracking-in-opencart/ . this might help you . Because whatever codes I have published in my site , have been tested on multiple case but sometime we had to customise(modify) our code because of modification of basic opencart cms.

  4. Hi Dinesh,
    Thank you for this tutorial.

    Сould you help me in my case?

    How to change the code to get the following result on success page (product id into single string):

    sku: [‘1234’, ‘4642’, ‘5677’, ….]

    Many thanks!

Leave a Reply to David Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.