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


  • Convert DVD to mpg for use on DLNA Sony Bravia TVConvert DVD to mpg for use on DLNA Sony Bravia TV

    Convert DVD to mpg for use on DLNA Sony Bravia TV

    Media

    Short and sweet this one, how to get a DVD into a format suitable for streaming to a DLNA enabled Sony Bravia TV (specifically a

  • Install Boxee in Ubuntu Server

    Linux

    This short tutorial will help you install Boxee HTPC software in Ubuntu Server 10.04 LTS 32-bit Related posts: Linux alternative to Windows Server This will

  • Find a micro-niche for affiliate marketingFind a micro-niche for affiliate marketing

    Find a micro-niche for affiliate marketing

    Website Design & Development

    I found a post that described an excellent way of discovering micro-niche’s suitable for affiliate marketing strategies I can’t remember the website I found this