dan1978@gmx.net | My favorites ▼ | English ▼ | Sign out
Skip to page content Skip to main navigation
e.g. "adwords" or "open source"

# 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 implement the `ga.js` ecommerce tracking methods in your shopping cart pages or through your ecommerce software. The collection of ecommerce methods work together to send each user's transaction information 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.

## General Process

The basic process for tracking ecommerce using Google Analytics can best be described by summarizing the three methods required for tracking ecommerce transactions on your site. These methods are described in the order in which you should invoke them in your shopping cart or ecommerce software.

1. Create a transaction object.

   Use the `_addTrans()` method to intialize a transaction object. The transaction object stores all the related information about a single transaction, such as the order ID, shipping charges, and billing address. The information in the transaction object is associated with its items by means of the order IDs for the transaction and all items, which should be the same ID.

2. Add items to the transaction.

   The `_addItem()` method tracks information about each individual item in the user's shopping cart and associates the item with each transaction via the `orderId` field. This method tracks the details about a particular item, such as SKU, price, category, and quantity.

3. Submit the transaction to the Analytics servers.

   The `_trackTrans()` method confirms that a purchase has occurred, and all data that has been built up in the transaction object is finalized as a transaction.

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.

## Guidelines

Keep in mind the following when implementing ecommerce tracking.

- The SKU code is a required parameter for every item that is added to the transaction.
  If a transaction contains multiple items and the SKU is not supplied for every item, a GIF request is sent only for the last item added to the transaction for which a SKU is provided. In addition, if your inventory has different items with the same SKU, and a visitor purchases both of them, you will receive data for only the most recently added. For this reason, you should make sure that each item you offer has a unique SKU.
- The argument list for `_addTrans()` and `_addItem()` is matched by position.
  While not all arguments are required, you should supply an empty placeholder for unspecified arguments to avoid errors. For example, you would add an item containing only order ID, sku, price, and quantity like this:

      _addItem("54321", "12345", "", "", "55.95", "1");

- The values for the `price` and `total` parameters must be supplied as integers, and are not affiliated with any currency value.
  For these parameters, either a comma or a period in the value indicates a fractional value. So, for example, if you provide `1,996.00` as the value for the `total`, it is recorded as `1.996`, not as $1,996.00. This is because the format for this parameter is a simple integer format and commas and other currency indicators are not recognized. In addition, because these values are recorded as integers, there is no association with any currency. Consequently, any currency conversion that you require must be first handled by your ecommerce software before you pass the data to Analytics.
- 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 "Cross Domain Tracking" for details.
- While not strictly required, it is a good idea to call `_trackPageview()` on your receipt page if you want to associate that particular page with the transaction data.

## Complete Example

The following example illustrates a sample configuration of ecommerce tracking on a receipt page using all three methods. The use of `_trackPageview()` associates the transaction with the page entitled Receipt for your clothing purchase from Acme Clothing.

Asynchronous Syntax (recommended)

    <html>
    <head>
    <title>Receipt for your clothing purchase from Acme Clothing</title>
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXX-X']);
      _gaq.push(['_trackPageview']);
      _gaq.push(['_addTrans',
        '1234',           // order ID - required
        'Acme Clothing',  // affiliation or store name
        '11.99',          // total - required
        '1.29',           // tax
        '5',              // 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
      _gaq.push(['_addItem',
        '1234',           // order ID - required
        'DD44',           // SKU/code - required
        'T-Shirt',        // product name
        'Green Medium',   // category or variation
        '11.99',          // unit price - required
        '1'               // quantity - required
      ]);
      _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

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

    </script>
    </head>
    <body>

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

    </body>
    </html>

Traditional Syntax

    <html>
    <head>
    <title>Receipt for your clothing purchase from Acme Clothing</title>
    </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">
    try{
      var pageTracker = _gat._getTracker("UA-xxxxx-x");
      pageTracker._trackPageview();
      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 - necessary to associate item with transaction
          "DD44",           // SKU/code - required
          "T-Shirt",        // product name
          "Olive Medium",   // category or variation
          "11.99",          // unit price - required
          "1"               // quantity - required
       );

       pageTracker._trackTrans(); //submits transaction to the Analytics servers
    } catch(err) {}
    </script>
    </body>
    </html>

Back to Top
English Español 日本語한국어 Português (Brasil)Pусский 中文 (简体)中文 (繁體)
Suggestions

Find developer products...Find open source projects...