How to Track Ecommerce Transactions with Google Analytics

Craig Buckler
Craig Buckler
Share

Google Analytics is a really useful tool for tracking visitors on your website, but few developers go beyond adding the standard tracking code, which consists of a JavaScript snippet.

Other than custom Events (see the link above if you’re curious what Events are), we can also track ecommerce transactions in Google Analytics using some extra code.

Why Track Ecommerce Transactions in Google Analytics?

Most ecommerce systems offer detailed transaction statistics, but you can’t necessarily relate them to sessions, devices, demographics or other important metrics. For example, was an increase in sales caused by UX improvements, a successful marketing campaign, or seasonal variations? Who knows.

Recording transactions in Google Analytics means that:

  • product and user reports are richer and more insightful
  • you can relate transactions directly to AdWords and other campaigns systems
  • you can assess the success of campaigns and concentrate on those that lead to a high return on investment
  • you can award access to these reports to those in your company without having to add them to the ecommerce system.

Let’s dive in.

The Overall Concept

Typically, your users will add items to a cart, check out, possibly register, pay an amount, and then be redirected to a confirmation screen. The transaction details will then show up in Google Analytics, once the user has officially checked out.

Ecommerce transaction data is usually stored in your back-end system or the back-end system of a payment gateway. For this information to show up in Google Analytics, we need to update our JavaScript code so Google Analytics can track this data too.

How to Enable Ecommerce Tracking

Proceed to the Admin area (cog icon) on the left-hand side, select your PROPERTY (your website), and then select Ecommerce Settings. Switch the “Status” to ON.

Note: the Related Products feature was deprecated in April 2017, so avoid this entirely.

Enhanced Ecommerce Settings allows you to record product impressions, promotions and actions such as coupon usage. I recommend that you implement standard ecommerce tracking first, then consult the Enhanced Ecommerce Documentation when you want to move on to more advanced ecommerce tracking later down the line.

Finally, open the View Settings menu and change the Currency displayed as value if necessary.

Enable Ecommerce Transactions

Next, use this snippet to enable ecommerce transactions:

ga('require', 'ecommerce');

Note: this must be run after the page tracking snippet and before any ecommerce functionality is used.

Starting a New Transaction

Next, the transaction is initiated with this JavaScript code:

ga('ecommerce:addTransaction', {
  'id': '[transactionId]',
  'affiliation': '[storeName]',
  'revenue': '[totalCost]',
  'shipping': '[shippingCost]',
  'tax': '[taxCost]',
  'currency': '[currency]'
});

Let’s look at each of the lines in this object individually …

[transactionId] (required)

The unique transaction identifier, e.g. 'ABC-123'.

[storeName] (optional)

The affiliation or store name, e.g. 'My Online Shop'.

[totalCost] (optional)

The total cost including shipping and taxes. A string-encoded number without currency symbols must be used, e.g. '12.99'.

[shippingCost] (optional)

The (numeric) cost of shipping, e.g. '2.99'.

[taxCost] (optional)

The (numeric) tax, e.g. '1.64'.

[currency] (optional)

We already set up the default currency earlier, but you can alternatively specify a 3-character ISO 4217 country code such as EUR (Euros) or GBP (British Pound) if the shopper opts to pay using a different currency.

Adding Items to a Transaction

Items are added to an existing transaction with this snippet:

ga('ecommerce:addItem', {
  'id': '[transactionId]',
  'name': '[productName]',
  'sku': '[productCode]',
  'category': '[categoryName]',
  'price': '[price]',
  'quantity': '[quantity]'
});

Again, let’s look at each of the lines in this object individually …

[transactionId] (required)

The unique transaction identifier, e.g. 'ABC-123'. This must match the code used in the transaction above, to clarify that this item relates to the basket the user already has.

[productName] (required)

The product name, e.g. 'mens blue shirt'.

[productCode] (optional)

A unique product code or stock-keeping unit (SKU), e.g. 'MBS-00001'.

[categoryName] (optional)

A category or variation, e.g. 'mens shirts'.

[price] (optional)

The numeric price of this item, e.g. '9.99'.

[quantity] (optional)

The quantity purchased, e.g. '2'.

Clearing Items in a Transaction

In case the user empties their cart, the following JavaScript command clears all items from the transaction:

ga('ecommerce:clear');

Submit the Transaction to Google Analytics

The final transaction with all items must then be submitted with the following JavaScript command:

ga('ecommerce:send');

Naturally, this would happen on the confirmation screen.

The Final Code

The HTML code in your final confirmation screen could therefore look something like this:

<!DOCTYPE html>
<html>
<head>
<title>Thank you for your order</title>
</head>
<body>

<h1>Thank you for your order</h1>
<p>A receipt has been sent to you@youremail.com</p>

<!-- standard GA code -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>

<script>
// enable the ecommerce features
ga('require', 'ecommerce');

// start the transaction
ga('ecommerce:addTransaction', {
  'id': '10001',
  'affiliation': 'My Cool Store',
  'revenue': '54.97',
  'shipping': '5.00',
  'tax': '0.00'
});

// add item 1
ga('ecommerce:addItem', {
  'id': '10001',
  'name': 'Learn HTML',
  'sku': 'BOOK00001',
  'category': 'book',
  'price': '14.99',
  'quantity': '1'
});

// add item 2
ga('ecommerce:addItem', {
  'id': '10001',
  'name': 'Learn CSS',
  'sku': 'BOOK00002',
  'category': 'book',
  'price': '14.99',
  'quantity': '1'
});

// add item 3
ga('ecommerce:addItem', {
  'id': '10001',
  'name': 'Learn JavaScript',
  'sku': 'BOOK00003',
  'category': 'book',
  'price': '19.99',
  'quantity': '1'
});

// send transaction
ga('ecommerce:send');
</script>
</body>
</html>

Within a few hours, you’ll be able to view transaction statistics in the Conversions → Ecommerce section of Google Analytics (among other reports as well).

For more information, refer to the Google Analytics documentation:

Frequently Asked Questions about Ecommerce Transactions Tracking with Google Analytics

How can I set up Google Analytics for my ecommerce website?

Setting up Google Analytics for your ecommerce website involves a few steps. First, you need to create a Google Analytics account if you don’t have one. After creating an account, you will be provided with a tracking code that you need to add to your website. This code allows Google Analytics to collect data from your site. Next, you need to enable ecommerce tracking in your Google Analytics account. This can be done in the Admin section under Ecommerce Settings. Once enabled, Google Analytics will start tracking ecommerce transactions on your site.

What kind of data can I track with Google Analytics ecommerce tracking?

Google Analytics ecommerce tracking allows you to track a wide range of data related to ecommerce transactions. This includes product and transaction information, average order value, ecommerce conversion rate, time to purchase, and other data. You can also track the shopping behavior of your customers, such as how many customers added items to their carts, how many reached the checkout process, and how many completed a purchase.

How can I use the data from Google Analytics to improve my ecommerce business?

The data from Google Analytics can provide valuable insights into your ecommerce business. For example, you can identify which products are selling well and which ones are not. You can also identify any issues in your checkout process that might be causing customers to abandon their carts. By analyzing this data, you can make informed decisions to improve your ecommerce business.

Can I track ecommerce transactions on mobile devices with Google Analytics?

Yes, Google Analytics allows you to track ecommerce transactions on both desktop and mobile devices. This can provide valuable insights into the shopping behavior of your mobile customers, which can be quite different from desktop customers.

How can I track individual products with Google Analytics ecommerce tracking?

To track individual products with Google Analytics ecommerce tracking, you need to send product data with your ecommerce tracking code. This includes the product ID, name, category, and price. Once this data is sent, you can view product performance reports in your Google Analytics account.

Can I track multiple ecommerce transactions from the same customer with Google Analytics?

Yes, Google Analytics allows you to track multiple ecommerce transactions from the same customer. This can provide valuable insights into the buying behavior of your repeat customers.

How accurate is the data from Google Analytics ecommerce tracking?

The accuracy of the data from Google Analytics ecommerce tracking depends on several factors, including the correct implementation of the tracking code and the reliability of the data sent with the code. However, Google Analytics is known for its high data accuracy, making it a reliable tool for ecommerce tracking.

Can I use Google Analytics ecommerce tracking with other Google products?

Yes, Google Analytics ecommerce tracking can be used in conjunction with other Google products, such as Google Ads and Google Search Console. This allows you to gain a more comprehensive understanding of your ecommerce business.

How can I troubleshoot issues with Google Analytics ecommerce tracking?

If you’re experiencing issues with Google Analytics ecommerce tracking, there are several troubleshooting steps you can take. First, check if your tracking code is correctly implemented on your website. Next, check if ecommerce tracking is enabled in your Google Analytics account. If you’re still experiencing issues, Google provides several resources for troubleshooting, including a help center and community forums.

Can I customize the data I track with Google Analytics ecommerce tracking?

Yes, Google Analytics allows you to customize the data you track with ecommerce tracking. This includes custom dimensions and metrics, which allow you to track data that is specific to your ecommerce business.