Tiffany B. Brown

Collecting e-commerce conversion data with Zen Cart and Google Analytics

Google Analytics allows you to collect pretty robust data about how users move through your e-commerce site. Here’s how to make it work with Zen Cart, an open source shopping cart.

For this tutorial, you will need:

  • A Zen Cart-based shopping cart
  • A Google Analytics account and the tracking code for both conversions and page views.
  • Experience with PHP and MySQL programming

Also check out Google’s support article How do I track e-commerce transactions? and become BFFs with the Zen Cart wiki.

Creating a custom functions file

Create a file to house your custom functions. Name it whatever you’d like (with a PHP extension, of course). Put it in your includes/functions/extra_functions/ directory.

A word of caution: I created these functions with my own SQL, outside of the Zen Cart framework. There may be a better way to do it.

In the file you created above, add the following code:

# get the product ID out of session key
function custom_get_prodId($productkey){
	$pid = explode(':',$productkey);
	return $pid[0];
}

#get the product data
function custom_get_product($prodID,$return='all'){
	global $db;
	reset($data);
	$q = sprintf("SELECT 
			pd.products_name, 
			pd.products_description, 
			cd.categories_name, p.products_price
			FROM categories_description AS cd, products AS p, products_description AS pd
	 		WHERE  p.products_id = 183
   				AND p.master_categories_id = cd.categories_id
   				AND p.products_id = pd.products_id",$prodID);
	$res = $db->Execute($q);
	return $res->fields;
}
# get the current user's data
function custom_get_userinfo($custID){
	global $db;
	reset($data);
	$q = sprintf("SELECT 
			ab.entry_street_address, 
			ab.entry_postcode, 
			ab.entry_city, 
			ab.entry_state,
			c.countries_name,
			c.countries_iso_code_2
			FROM address_book AS ab, countries AS c
			WHERE customers_id=%d
			   AND ab.entry_country_id = c.countries_id
				  ",(int)$custID);
	$res = $db->Execute($q);
	return $res->fields;
}

Adjust the names of the tables if you have added a custom table prefix during your Zen Cart configuration. We’ll use these functions to get us the user data and product data we need.

Edit the checkout success and global footer files

Add the conversion tracking code — provided by Google Analytics — to the checkout success page template. It’s located in your templates directory, includes/templates/YOUR_TEMPLATE_DIR/templates/tpl_checkout_success_default.php.

Also add the regular Google Analytics tracking code to your footer file (found in includes/templates/YOUR_TEMPLATE_DIR/common/tpl_footer.php).

Below your Google Analytics code, but also in your footer file, add the code below.

<?php
# do this on the checkout success page only.
if($_GET['main_page'] == 'checkout_success'): 

$userdata = $_SESSION['cart'];
$moreud = custom_get_userinfo($_SESSION['customer_id']);
?>
<script type="text/javascript">
pageTracker._addTrans(
    "<?=$userdata->cartID; ?>", 
    "www.YOURDOMAINNAME.com",  
    "<?=$userdata->total; ?>",
    "",                      
    "<?=$_SESSION['shipping']['cost']; ?>", 
    "<?=$moreud['entry_city'];?>",
    "<?=$moreud['entry_state'];?>",
    "<?=$moreud['countries_name'];?>"
  );
<? 
# for each product ID key in the userdata session
foreach($userdata->contents as $k=>$v): 
	/*
	the product id gets stored as an array key as xxx:funkymd5key 
	ex: 183:fw920e8ktw327uio67xew9mn. custom_get_prodId extracts 
	the product id part of that key.
	*/
	$pid = custom_get_prodId($k); 
	$data = custom_get_product($pid,'all'); 
?>

 pageTracker._addItem(
    "<?=$userdata->cartID; ?>",                  	 
    "<?=getProdId($k); ?>",          			 	 
    "<?=strip_tags($data['products_name']);?>",   
    "<?=$data['categories_name'];?>",               
    "<?=number_format($data['products_price'],2);?>", 
    "<?=$v['qty']; ?>"                                          
  );
<? endforeach; ?>
pageTracker._trackTrans();
</script>

<?php endif;
} // flag_disable_footer
?>
Providing this code with the following disclaimers:

  1. It may not work for you.
  2. I’m not responsible if it does.
  3. I can’t offer personalized support.

In short: you’re on your own.

9 Responses to “Collecting e-commerce conversion data with Zen Cart and Google Analytics”

  1. adama zuman says:

    I tried installing the google analytics for zencart.
    It's a bit tricky because sometimes it take a few hours for google analytics to recognize the code.
    So I spent half a day turning everything upside down with no results then after I gave up and change everything back to defautlt, it started working the next day.

  2. adama zuman says:

    I tried installing the google analytics for zencart.
    It's a bit tricky because sometimes it take a few hours for google analytics to recognize the code.
    So I spent half a day turning everything upside down with no results then after I gave up and change everything back to defautlt, it started working the next day.

  3. Adam says:

    Where should we put the include or require statement for the function file?

  4. Adam says:

    Where should we put the include or require statement for the function file?

  5. Jdub says:

    didn't work for me. can anyone show how/where they implemented the code?

  6. Jdub says:

    didn't work for me. can anyone show how/where they implemented the code?

  7. Anonymous says:

    Design a e-commerce website with zen cart.

    The following is the process flow:
    Option 1 ( single address check )
    1 – User enters data & runs a query process ( this will be written in
    ASPX )
    2 – The cart is updated.
    3 – User repeats step 1-2 as many times as he wants
    4 – User proceeds to get his report ( results of all his queries ):
    a) User is 1st directed to the checkout
    b) Upon successful checkout user is provided with a stub ( token )
    to download his report ( PDF file ).

    Option 2 ( multiple addresses check )
    1 – User uploads data in Excel form & runs a number of queries
    ( this will be written in ASPX )
    2 – The cart is updated for every query ran.
    3 – User proceeds to get his report ( results of all his queries ):
    a) User is 1st directed to the checkout
    b) Upon successful checkout user is provided with a stub ( token )
    to download his report.
    4 – The report ( PDF file ) can be downloaded right away or within 10
    days using the stub provided to him during the checkout.

    As far as Zen Cart setup, Product = One Address Check

    In addition, the website needs to include:
    Google Analytics
    Google Site Map
    Keywords, Tags for SEO, etc.

    By best travel insurance company

  8. williamsacott says:

    Design a e-commerce website with zen cart.

    The following is the process flow:
    Option 1 ( single address check )
    1 – User enters data & runs a query process ( this will be written in
    ASPX )
    2 – The cart is updated.
    3 – User repeats step 1-2 as many times as he wants
    4 – User proceeds to get his report ( results of all his queries ):
    a) User is 1st directed to the checkout
    b) Upon successful checkout user is provided with a stub ( token )
    to download his report ( PDF file ).

    Option 2 ( multiple addresses check )
    1 – User uploads data in Excel form & runs a number of queries
    ( this will be written in ASPX )
    2 – The cart is updated for every query ran.
    3 – User proceeds to get his report ( results of all his queries ):
    a) User is 1st directed to the checkout
    b) Upon successful checkout user is provided with a stub ( token )
    to download his report.
    4 – The report ( PDF file ) can be downloaded right away or within 10
    days using the stub provided to him during the checkout.

    As far as Zen Cart setup, Product = One Address Check

    In addition, the website needs to include:
    Google Analytics
    Google Site Map
    Keywords, Tags for SEO, etc.

    By best travel insurance company

  9. Robin says:

    This unique seems to me dissimilar set of post…one who don’t grasp about present before may effectuate pertinent tidings from this post…well i wanna estimate that The wise how u tried to explain some posts at here seems to me different.
    Search marketing