Quantcast
Channel: Tiffany B. Brown » PHP
Viewing all articles
Browse latest Browse all 15

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 15

Trending Articles