Categories: Google Analytics and Ecommerce Tracking

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.

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
  • Click on Edit button.
Opencart Ecommerce – Google analytics extensions Edit
  • Add Google Analytics Tracking Code in Textarea.
  • Click on save button.
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

Image: After Code Insertion

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

Image: After Code Insertion

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

Image : After Code Insertion

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- Verification

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

Dinesh

Share
Published by
Dinesh
Tags: Ecommerce trackinggoogle analyticsopencart

Recent Posts

  • Adword Conversion Tracking
  • Google Adwords Remarketing

Conversion and Remaketing code in BigCommerce

In this blog, you will see how to add Adwords Conversion Tracking and Remarkeing Code in BigCommerce.BigCommerce is one of…

8 years ago
  • Google Analytics and Ecommerce Tracking

Google Analytics Definition and Common Terms

 If you have gone through our previous blog about Google Analytics, you might have sound understanding about Google Analytics and how it…

8 years ago
  • Google Analytics and Ecommerce Tracking

What is Google Analytics and it’s Features?

Do you want to know more about your visitors and how your content is performing?​ Whether you run a website…

8 years ago
  • Google Analytics and Ecommerce Tracking

How to Add Google Analytics in BigCommerce

In this blog, you will see how to add Google Analytics and E-commerce Tracking Code  in BigCommerce. BigCommerce is one of the…

8 years ago
  • Google Analytics and Ecommerce Tracking

Google Analytics and E-commerce Tracking in Miva Merchant

If you have gone through some of previous blog, you might have seen how to install google analytics, e-commerce tracking…

8 years ago
  • Google Analytics Event Tracking
  • Google Tag Manager (GTM)

Contact Form 7 Event Tracking Via GTM

In the last post, you had seen How to add event tracking in gravity form via Google Tag Manager. Now in this…

8 years ago