Google eCommerce Tracking with osCommerce

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:

1
$orders_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' order by date_purchased desc limit 1");

Change to:

1
$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:

1
2
3
4
5
6
7
$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:

1
2
3
4
5
6
7
8
9
$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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<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.

Post written by Ed Reckers

Founder and lead web development consultant at Red Bridge Internet : San Francisco WordPress Developers and Consultants.

11 Responses to “Google eCommerce Tracking with osCommerce”

  1. aehageman

    First 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.

  2. ed

    Hey 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.

  3. Eric Hackathorn

    For me when I tried to cut and paste the code above the funny double and single quotes threw errors in the PHP parser.

  4. ed

    Eric, I believe the issues with the cutting and pasting are now resolved.

  5. Jonathan Hochman

    Thank you for this code!

    It looks like your blog is implementing smart quotes. I got your code to work by running it through my smart quote replacement filter, http://www.hochmanconsultants.com/tools/replace-smart-quotes.shtml. It replaces funky quotes with standard quotes.

  6. Joe

    I don’t think the copy-paste issues are resolved yet – it’s still giving me funny quotes

  7. iJames

    Yes, the quotes are a different encoding than what the text on my php pages likes. It’s both the double quotes and the single quotes. I did a fine and replace (three passes for double-quote, left, and right single quotes.

    Than I believe the code was correct. Still testing…

  8. Mark Matanza

    Disregard my previous comment, my code got stripped out.

    I was trying to test to see if my values got passed to the page by using:

    print_r ($orders);
    print_r ($addTrans);
    print_r ($products_array);

    But only the $products_array is being printed out.

    I’m still learning PHP, but im thinking is it because there is a $products_array and no $orders_array?

    Can we do something like this? Would this be the same thing??

    while ($orders = tep_db_fetch_array($orders_query)) {
    $orders_array[] = array(

    }

    );

    pageTracker._addTrans(
    // Order ID
    // Affiliation – Mountain View
    // Total
    // Tax
    // Shipping
    // City – San Jose
    // State – California
    // Country – USA
    );

    )

    Also I noticed the Affiliation is using the customers_city value, shouldn’t it be the store name??

    Any help appreciated!

    Thanks,
    Mark

  9. Mark Matanza

    Hi Ed,

    Great notes!

    Would the below code work as well?

    pageTracker._addTrans(
    “”, // Order ID
    “”, // Affiliation
    “”, // Total
    “”, // Tax
    “”, // Shipping
    “”, // City
    “”, // State
    “” // Country
    );

    }
    }

  10. Chris

    This is epic! I found another solution too, but that one was more outdated. This code is also a tiny bit outdated, since the code doesn’t use the async methods, but this is still the cleanest solution out there. I like how you reuse the original sql and just add the extra fields to it.

    The last bit could have been a bit cleaner though, without the need to jump in and out between php and js. For example:

    $s = "\r\n" . "var _gaq = _gaq || [];\r\n" .
    ..........
    ..........
    ..........
    ..........
    $s .= "_gaq.push(['_trackTrans']);\r\n\r\n" .
    "(function() {\r\n" .
    "var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\r\n" .
    "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\r\n" .
    "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\r\n" .
    "})();\r\n" .
    echo $s;

  11. oscommerce web development

    First 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. finally your code does not work for me either.
    several errors in the footer.

Leave a Reply