Google Ecommerce Tracking in Opencart

Google Ecommerce Tracking in Opencart

This will be a quick tutorial on adding Google Analytics Ecommerce tracking to opencart shops

1. Add the following to a new template file in /public_html/catalog/view/theme/YOUR TEMPLATE FILES/template/common/gasuccess.tpl

<?php echo $header; ?>
<?php echo $column_left; ?>
<?php echo $column_right; ?>

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
'<?php echo $order_id; ?>',           // order ID - required
'Shop Name',  // affiliation or store name
'<?php echo $order_total; ?>',          // total - required
'0',           // tax
'0',              // shipping
'<?php echo $order_city; ?>',       // city
'<?php echo $order_county; ?>',     // state or province
'UK'             // country
]);

// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each

<?php

foreach ($products as $product) {

echo '_gaq.push(['_addItem',';
echo ''' .  $order_id . '',';
echo ''' .  $product['model'] . '',';
echo ''' .  $product['name'] . '',';
/* Removed options as caused error  */
/*    foreach ($product['option'] as $option) {
echo ''' .  $option['name'] . '',';
}
*/
echo ''Null Product Variations',';
echo ''' .  $product['price'] . '',';
echo ''' .  $product['quantity'] . ''';

echo ']);';

}

?>

_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

2. Add the following to /public_html/catalog/controller/checkout/success.php, just below:

<?php
class ControllerCheckoutSuccess extends Controller {
 public function index() {

$this->data['order_id'] = $this->session->data['order_id'];

 /*

 Extract order details and assign to a variable(s) for Google Ecommerce tracking

 */

 $this->load->model('account/order');
 $order_info = $this->model_account_order->getOrder($orderRedirect);

 if ($order_info) {

 // Data required for _addTrans

 $this->data['order_id'] = $orderRedirect;
 $this->data['store_name'] ='SHOP Name';
 $this->data['order_total'] = $order_info['total'];
 $this->data['order_tax'] = '0';
 $this->data['order_shipping'] = '0';
 $this->data['order_city'] = $order_info['payment_city'];
 $this->data['order_county'] = $order_info['payment_zone'];
 $this->data['order_country'] = 'UK';

 // Data required for _addItem

 $this->data['products'] = array();

 $products = $this->model_account_order->getOrderProducts($orderRedirect);

 foreach ($products as $product) {
 $options = $this->model_account_order->getOrderOptions($orderRedirect, $product['order_product_id']);

 $option_data = array();

 foreach ($options as $option) {
 $option_data[] = array(
 'name'  => $option['name'],
 'value' => $option['value'],
 );
 }

 /*

 The below should sit in gasuccess.tpl line 34 for the google e-commerce tracking code
 however it gives the following PHP warning, so it is easier to omit if there are no product options
 <b>Warning</b>: Invalid argument supplied for foreach() in <b>/home/shop/catalog/view/theme/lms/template/common/gasuccess.tpl</b> on line <b>35</b>

 <b>Notice</b>: Undefined index:  name in <b>/home/shop/catalog/controller/checkout/success.php</b> on line <b>65</b>
 <b>Notice</b>: Undefined index:  name in <b>/home/shop/catalog/controller/checkout/success.php</b> on line <b>65</b>
 <b>Notice</b>: Undefined index:  name in <b>/home/shop/catalog/controller/checkout/success.php</b> on line <b>65</b>

 foreach ($product['option'] as $option) {
 echo ''' .  $option['name'] . '',';
 }

 */

 $this->data['products'][] = array(
 'name'     => $product['name'],
 'model'    => $product['model'],
 // 'option'   => $option_data['name'], (see above comment)
 'quantity' => $product['quantity'],
 'price'    => $product['price'],
 );
 }

 }

 /*

 End of Google Ecommerce tracking

 */

Oliver Partridge

Building websites since 2000, I have a wealth of experience in eMarketing, SEO, e-mail marketing and social media marketing. Google+ and Twitter

Leave a Reply


  • HTML5 + CSS3 + Javascript FrameworksHTML5 + CSS3 + Javascript Frameworks

    HTML5 + CSS3 + Javascript Frameworks

    Website Design & Development

    This isn’t meant to be an exhaustive list of modern frameworks for rapid prototyping and building of websites and web apps, but here goes… Related

  • Xubuntu Install FontsXubuntu Install Fonts

    Xubuntu Install Fonts

    Linux

    This is not immediately obvious, as there is no built-in GUI for managing fonts in Xubuntu 11.10. 1. Open Thunar in sudo sudo thunar 2.

  • 4 column wordpress theme4 column wordpress theme

    4 column wordpress theme

    Templates & Themes Website Design & Development Wordpress

    So you’ve got your WordPress blog up and running and have started to think about the look of it and themes? In this series we’ll