How to Track Ecommerce Transactions with Google Analytics

Original Source:

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:


Submit the Transaction to Google Analytics

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


Naturally, this would happen on the confirmation screen.

Continue reading %How to Track Ecommerce Transactions with Google Analytics%

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *