English | Site Directory

Google Analytics Custom Tracking

Ecommerce Tracking

Before Google Analytics can report ecommerce activity for your website, you must enable ecommerce tracking on the profile settings page for your website. After that, you must ensure that the tracking code is implemented on your order confirmation page, so that each user's transaction is sent to the Google Analytics database as it occurs. In this way, Analytics can link a specific referral source to a conversion or purchase. Most template-driven ecommerce engines can be modified to include this information hidden in the order confirmation page.

The basic process for tracking ecommerce using Google Analytics is:

  1. The website visitor submits the final purchase page.
  2. Information from this purchase has been captured by the ecommerce engine.

    There are many ways that this information can be retrieved from the ecommerce engine. Some ecommerce engines write the purchase information to a hidden form that you can use, others keep the information in a database that you can retrieve, and others store the information in a cookie. Some of the more popular ecommerce engines that recognize Google Analytics provide their own modules to simplify order tracking for Analytics.

  3. The purchase information is passed to the confirmation page and stored in a hidden way on that page.
  4. An onload() method in the <body> tag of the page submits the information in the form to the Google Analytics reports.

    Alternately, you can submit the form data using another means if you don't have access to the <body> tag of the page. In either case, it's important to ensure the hidden order information is submitted after it is has been printed to the confirmation page, but before the user exits the confirmation page.

The are three basic methods to use when setting up ecommerce tracking on your receipt page. You can view detailed information for all three.

The following code snippet illustrates a sample configuration of ecommerce tracking on a receipt page using all three methods.

<html>
<head>
</head>
<body> 

Thank you for your order.  You will receive an email containing all your order details.


<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-xxxxx-x");
pageTracker._initData();
pageTracker._addTrans(
"1234", // order ID - required
"Womens Apparel", // affiliation or store name
"11.99", // total - required
"1.29", // tax
"15.00", // shipping
"San Jose", // city
"California", // state or province
"USA" // country
); // add item might be called for every item in the shopping cart // where your ecommerce engine loops through each item in the cart and // prints out _addItem for each
pageTracker._addItem(
"1234", // order ID - required
"DD44", // SKU/code
"T-Shirt", // product name
"Olive Medium", // category or variation
"11.99", // unit price - required
"1" // quantity - required
); pageTracker._trackTrans(); //submits order info in hidden form after page load

</script> </body> </html>

Finally, if you are implementing ecommerce tracking and using a 3rd-party shopping cart, you will likely need to configure cross-domain tracking as well. See the section on "Multiple Site Configuration" for details.

Back to Top