Below are some notes on how I implemented Google eCommerce Tracking with my osCommerce store. You can view Google’s instructions on enabling e-commerce tracking here:

In order to add Google transactions tracking to my osCommerce store I needed to edit both the checkout_success.php and footer.php files. I edited both these files because I’m already using Google Analytics to track users and there is a dependency in the files which must come before the e-commerce tracking portion.

Therefor, I edit checkout_success.php in order to capture the correct information and keep the Google Analytics javascript code in the footer so that it continues to be global.

First edit checkout_success.php

Your first order of business is increasing the information (elements) in the ‘orders’ array.

Find the following line:

$orders_query = tep_db_query(”select orders_id from ” . TABLE_ORDERS . ” where customers_id = ‘” . (int)$customer_id . “‘ order by date_purchased desc li
mit 1″);

Change to:

$orders_query = tep_db_query(”select orders_id, customers_city, customers_state, customers_country from ” . TABLE_ORDERS . ” where customers_id = ‘” . (int)$customer_id . “‘ order by date_purchased desc limit 1″);

You’ll now want to increase the information (elements) in the products_array.

Find the Following lines:

$products_query = tep_db_query(”select products_id, products_name from ” . TABLE_ORDERS_PRODUCTS . ” where orders_id = ‘” . (int)$orders[’orders_id’] . ”
‘ order by products_name”);
while ($products = tep_db_fetch_array($products_query)) {
$products_array[] = array(’id’ => $products[’products_id’],
‘text’ => $products[’products_name’]);
}

Change to:

$products_query = tep_db_query(”select products_id, products_name, products_model, products_price, products_quantity from ” . TABLE_ORDERS_PRODUCTS . ” where orders_id = ‘” . (int)$orders[’orders_id’] . “‘ order by products_name”);
while ($products = tep_db_fetch_array($products_query)) {
$products_array[] = array(
‘id’   => $products[’products_id’],
’sku’  => $products[’products_model’],
‘cost’ => number_format($products[’products_price’], 2, ‘.’, ‘’),
‘num’  => $products[’products_quantity’],
‘text’ => $products[’products_name’]);
}

You’ll then want to add a new array called addTrans. Add the following code block before the end of the closing php tag directly below the code above. This will be around line 66:

/* ADDED TO GET ORDER TOTAL FOR GOOGLE ADWORDS CONVERSION TRACKING CODE VALUE */
/* by Ed Reckers */
/* October, 2008 */

// get transaction data for ._addTrans
$addTrans = array();
$addTrans[’orders_id’] = (int)$orders[’orders_id’];
$orderstotal_query = tep_db_query(”select class, value from ” . TABLE_ORDERS_TOTAL . ” where orders_id = ‘” . (int)$orders[’orders_id’] . “‘”);
while ($order_total = tep_db_fetch_array($orderstotal_query)) {
$addTrans[$order_total[’class’]] = number_format($order_total[’value’], 2, ‘.’, ‘’);
}

/* End GOOGLE ADWORDS (see below for Google’s Script) */

Now edit footer.php

At the foot of the document, above the final closing php tag and below the html comment (end pageContainer); add the following:

<script type=”text/javascript”>
var gaJsHost = ((”https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(”%3Cscript xsrc=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>

<script type=”text/javascript”>
var pageTracker = _gat._getTracker(”UA-XXXXX-1″);
pageTracker._trackPageview();

<?php
if ( $_SERVER[SCRIPT_NAME] == “/checkout_success.php” ) {
?>
pageTracker._addTrans(
“<?=$orders[’orders_id’]?>”,                // Order ID
“<?=$orders[’customers_city’]?>”,           // Affiliation - Mountain View
“<?=$addTrans[’ot_subtotal’]?>”,            // Total
“<?=$addTrans[’ot_tax’]?>”,                 // Tax
“<?=$addTrans[’ot_shipping’]?>”,            // Shipping
“<?=$orders[’customers_city’]?>”,           // City - San Jose
“<?=$orders[’customers_state’]?>”,          // State - California
“<?=$orders[’customers_country’]?>”         // Country - USA
);

<?php
for ($i=0, $n=sizeof($products_array); $i<$n; $i++) {
?>
pageTracker._addItem(
“<?=$orders[’orders_id’]?>”,                // Order ID
“<?=$products_array[$i][’sku’]?>”,          // SKU
“<?=$products_array[$i][’text’]?>”,         // Product Name
“”,                                         // Category
“<?=$products_array[$i][’cost’]?>”,         // Price
“<?=$products_array[$i][’num’]?>”           // Quantity
);
<?php
echo “\n”;
}
echo “  pageTracker._trackTrans();” . “\n”;
}
?>
</script>

In a nutshell, that’s pretty much how I was able to implement Google eCommerce Tracking with my osCommerce store

Below are some links I found helpful in this work:

If you have any questions regarding the implementation of Google eCommerce Trracking with your osCommerce store feel free to email me any specifics along with some contact information from my contact form at my business Web site Kliky.Com.