A few days ago, I was working on an existing osCommerce webshop and decided to install the new Google Analytics Tracking Code. Changing the script in the footer wasn’t all that had to be done (unfortunately). Since almost all functions have changed, the e-commerce script that registers the transaction also needed a few changes.
![]()
I took a look around and couldn’t find the adjusted osCommerce code. The only solution was to program it myself and in order to spare you some valuable time, here it is…
The code below needs to be implemented on the checkout_success.php page.
First thing to do is build a transaction line and one or more item lines that will be used in the new pageTracker._addTrans() and pageTracker._addItem() functions.
// ############## Google Analytics - start ###############
// by Joris Roebben www.queromedia.com
// Is partner cookie set ?
if (isset($_COOKIE["PartnerCookie"])) {
$partner = $_COOKIE["PartnerCookie"];
} else {
$partner = 'none';
}
// Get order id
$orders_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' order by date_purchased desc limit 1");
$orders = tep_db_fetch_array($orders_query);
$order_id = $orders['orders_id'];
// Get order info for Analytics "Transaction line" (affiliation, city, state, country, total, tax and shipping)
// Set value for "affiliation"
$analytics_affiliation = $partner;
// Get info for "city", "state", "country"
$orders_query = tep_db_query("select customers_city, customers_state, customers_country from " . TABLE_ORDERS . " where orders_id = '" . $order_id . "' AND customers_id = '" . (int)$customer_id . "'");
$orders = tep_db_fetch_array($orders_query);
$totals_query = tep_db_query("select value, class from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$order_id . "' order by sort_order");
// Set values for "total", "tax" and "shipping"
$analytics_total = '';
$analytics_tax = '';
$analytics_shipping = '';
while ($totals = tep_db_fetch_array($totals_query)) {
if ($totals['class'] == 'ot_total') {
$analytics_total = number_format($totals['value'], 2);
$total_flag = 'true';
} else if ($totals['class'] == 'ot_tax') {
$analytics_tax = number_format($totals['value'], 2);
$tax_flag = 'true';
} else if ($totals['class'] == 'ot_shipping') {
$analytics_shipping = number_format($totals['value'], 2);
$shipping_flag = 'true';
}
}
// Prepare the Analytics "Transaction line" string
$transaction_string = 'pageTracker._addTrans("' . $order_id . '","' . $analytics_affiliation . '","' . str_replace(",", "", $analytics_total) . '","' . $analytics_tax . '","' . $analytics_shipping . '","' . $orders['customers_city'] . '","' . $orders['customers_state'] . '","' . $orders['customers_country'] . '");';
// Get products info for Analytics "Item lines"
$item_string = '';
$items_query = tep_db_query("select products_id, products_model, products_name, final_price, products_quantity from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . $order_id . "' order by products_name");
while ($items = tep_db_fetch_array($items_query)) {
$category_query = tep_db_query("select p2c.categories_id, cd.categories_name from " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where p2c.products_id = '" . $items['products_id'] . "' AND cd.categories_id = p2c.categories_id AND cd.language_id = '" . (int)$languages_id . "'");
$category = tep_db_fetch_array($category_query);
$item_string .= 'pageTracker._addItem("' . $order_id . '","' . $items['products_id'] . '","' . $items['products_name'] . '","' . $category['categories_name'] . '","' . str_replace(",", "", number_format($items['final_price'], 2)) . '","' . $items['products_quantity'] . '");';
}
// ############## Google Analytics - end ###############
// To test your installation, uncomment next line
//echo $transaction_string . $item_string;
?>
Next thing we need is a call to the new Google Analytics Tracking Code, using the transaction string and item string to register the total transaction and all items included in the transaction:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();
pageTracker._trackPageview();
<?php echo $transaction_string.$item_string; ?>
pageTracker._trackTrans();
</script>
That’s it ! Give it a try and should you encounter any problem, let me know.
Leave a Reply