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.
4 Responses
aehageman
October 23rd, 2008 at 3:40 am
1First let me thank you for posting this article.
Second I can not manage to get any google code to work. Even the default code.
third no ones code seems to work. (for me)
finally your code does not work for me either.
several errors in the footer. Probably something I did.
Thanks
If you want to help me more, PLEASE contact me.
ed
October 23rd, 2008 at 8:27 am
2Hey there. If you’re having problems with just the default Google code, then you should take a few steps back and figure out what’s going on.
Google analytics basic code is just JavaScript so it shouldn’t be giving your site too hard of time. If you want to contact me you can email me a quick description of your error and email address from my contact form at my other Website kliky.com.
Eric Hackathorn
November 17th, 2008 at 2:46 pm
3For me when I tried to cut and paste the code above the funny double and single quotes threw errors in the PHP parser.
ed
January 5th, 2009 at 8:00 pm
4Eric, I believe the issues with the cutting and pasting are now resolved.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Clients and Projects
Recently Written
Calendar
Recent Entries
Recent Comments
Most Commented
Kliky.Net - Web Development Blog is proudly powered by WordPress - BloggingPro theme by: Design Disease