Google Analytics Enhanced Ecommerce Tracking for OpenCart

  1. Home
  2. Docs
  3. Google Analytics Enhanced Ecommerce Tracking for OpenCart
  4. Installation
  5. Default Template

Default Template

The ocmod adds the add to cart tracking script to product page. You will also need to setup manually in case if you want to track the addition of items or removal of items. 

Please follow the below step (only if you are developer)

The activity involves identification of javascript file which is responsible for add to cart / remove cart activity. By default, opencart uses catalog/view/javascript/common.js for these activity. In some templates they might use thier own javascript file. You will need to identify that file and make the following changes.

Open catalog/view/javascript/common.js

Change 1: Add the following code the end of the page

function gaAddtocart(e){$.ajax({url:"index.php?route=extension/module/google_ecommerce/get_product_info&product_id="+e,type:"post",dataType:"json",success:function(e){e.error?console.log(e.error):gtag("event","add_to_cart",{items:[{id:e.id,name:e.product_name,list_name:e.list_name,brand:e.brand,category:e.category,price:e.price}]})}})}function gaRemovefromcart(e){$.ajax({url:"index.php?route=extension/module/google_ecommerce/get_product_info&product_id="+e,type:"post",dataType:"json",success:function(e){e.error?console.log(e.error):gtag("event","remove_from_cart",{items:[{id:e.id,name:e.product_name,list_name:e.list_name,brand:e.brand,category:e.category,price:e.price}]})}})}

Example:

Change 2: Identify the add to cart function and add the following function just after the success command

gaAddtocart(product_id); 

Example:

Change 3: Identify the remove function and add the following function just after the success command

gaRemovefromcart(json.product_id);

Was this article helpful to you? Yes No