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(+).
- Click on Edit button.
- Add Google Analytics Tracking Code in Textarea.
- Click on save button.
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
Image: After Code Insertion
- 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
Image: After Code Insertion
- 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
- 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
If you have any issues, feel free to leave comment or contact me .
Thanks so much for this. Works like a charm!
This is really nice, codes work for you. Hope it would help others too.
Thanks, but not work for me, in Final Result i dont have register in tag assistance our in source code. Can you help please? I do everything you say in tutorial.
Hi Marco,
Sorry for late reply, I would love to help you.
Thank you for this tutorial. It works for opencart 2.1 as well .
Must Works.
But in my case, I must update also file: /system/storage/modification/catalog/model/checkout/order.php
cuz I had Proxy error.
It worked very well… Thanks.
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?
No Tag Assistant (By Google) is completely free. and it is really good. coming with some more feature.
Hi
Sorry I installed the extension.I had checked my console also.It works like a charm.
Hi Sujana,
I am really glad to see that, our code helped you to get charming solution. for more help you can get in touch with me
Thanks for this, perfect implement and no issue at all
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
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.
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!
ECOMMERCE TRACKING IN OPENCART 3.0.2
any post avaialble
thanks
I will work and post a blog asap.
Brother, thanks so muchhhhhhhhhhhhhhhh!!! Big hug…