Implementing E-Commerce Tracking via Google Tag Manager

This article is in conjunction with the article e-commerce tracking in Google Analytics – complete guide where I introduce the concept of e-commerce tracking in Google Analytics and explain various e-commerce terms in great detail.

In the current article I will show you how to install e-commerce tracking via Google Tag Manager (GTM).

Follow the steps below to implement ecommerce tracking via GTM:

Step-1: Integrate your shopping cart with Google Tag Manager

This involves adding a ‘GTM container code’ on each page of your ecommerce website, immediately after the opening body tag <body>.

If you are brand new to Google Tag Manager and wish to learn more about it, then read this article: Beginners guide to Google Tag Manager

If you are using a custom shopping cart then ask your developer, to add the GTM container code, immediately after the opening body tag <body>, on all website pages.

If you are using a third party shopping cart like ‘Shopify’, then you may need to refer to their help documentation or ask their customer support about, “where exactly to add the GTM container code?”. Most likely, you would need to edit some template file and add your GTM code there.

Then there are shopping carts like ‘Magento’ where you do not need to add any GTM container code. You just supply your container ID and the GTM is automatically installed on your website.

Bear in mind that there are many shopping carts which still do not integrate with GTM.

If you are using one of such shopping carts, then you should stop reading this article right now and switch your shopping cart to the one, which does integrate with GTM, preferably ‘Shopify’.

 

Step-2: Deploy Universal Analytics tracking via GTM

Create and publish a ‘Universal Analytics‘ tag on your website via GTM and then remove the corresponding hard coded ‘Google Analytics tracking code’ from all of the website pages:

universal-analytics-tag

It is important that you remove the hard coded GA Tracking Code, otherwise your GA tracking code will fire twice, once via GTM and once via hard coded tag, which could inflate your GA data.

If you are not sure, about the difference between ‘Google Analytics’ and ‘Universal Analytics’, then read this article: Difference between Google Analytics and Universal Analytics

Some shopping carts like ‘shopify’ do not let you remove the hard coded Google Analytics tracking code from your website.

In that case, do not deploy ‘Universal Analytics’ tag on your website via GTM, otherwise your GA tracking code will fire twice, once via GTM and once via hard coded tag, which could inflate your GA data.

So before you deploy ‘Universal Analytics’ tag on your website via GTM, make sure that you can remove the hard coded GA tracking code from your website.

 

Step-3: Turn on e-commerce tracking in Google Analytics

Google Analytics will report e-commerce data in your reporting view, only when the e-commerce tracking has been turned on at the view level. Follow the steps below:

Step-3.1: Login to your Google Analytics account and then navigate to the ‘Admin’ section of your view:

admin-google-analytics

Step-3.2: Click on ‘Ecommerce settings‘ link (under the ‘view’ column):

ecommerce-settings

Step-3.3: Switch the toggle button to ‘ON’  (under ‘Enable Ecommerce Status’) and then click on the ‘Next Step’ button:

enable-ecommerce

Step-3.4: Click on the ‘Submit’ button:submit

Your GA reporting view is now ready to collect and report on e-commerce data.

 

Step-4: Enable e-commerce tracking in your shopping cart (optional)

Many shopping carts these days provide built-in e-commerce tracking. This tracking is either automatically enabled or you need to manually enable it.

For example in case of ‘Shopify’, the e-commerce tracking is already enabled for all shopify customers.  Shopify does not use any data layer for e-commerce tracking. So whether or not you use GTM, your e-commerce tracking is already installed, up and running.

Once you have turned on e-commerce Tracking in Google Analytics, you should start seeing the e-commerce data from ‘Shopify’ in your GA reports. You don’t need to do anything else now.

Then there are shopping carts like ‘Magento’ where you may need to purchase an extension, in order to install e-commerce tracking via GTM. For example, ‘Magento’ provides built in data layer support for transaction data via Google Tag Manager Magento Connect extension:

enable-data-layer
Once you have installed this extension and enabled it, it will automatically create a data layer for you, on the order confirmation page and populate it with transaction data. That’s it. Your e-commerce tracking is now enabled via GTM. You don’t need to do anything else.

However if your shopping cart does not provide ‘built in data layer support’ for transaction data or pre-built e-commerce tracking or you use a custom shopping cart, then follow the next step.

 

Step-5: Create and add a e-commerce data layer to your website

#5.1 Copy-paste the following data layer code in your notepad file:

 

#5.2 Send your notepad file to your developer and ask him to write a server side script (like PHP) which automatically retrieves e-commerce data from your shopping cart software and populate the various fields (server variables) of your data layer code with the actual transaction data.

Ask your developer to add the modified data layer code (data layer code with server side script) just after the opening body tag (<body>) and before the ‘Google Tag Manager container code’, on the ‘order confirmation page’ (or any page which is shown to a user ,once the order has been successfully placed).

Tell him to keep following points in mind, while working on the data layer code:

#1 Data layer variable names are case sensitive. So keep names of the data layer variables intact. For example use ‘transactionId’ instead of ‘TransactionId’, ‘TransactionID’ or something else. Otherwise e-commerce tracking may not work.

#2 Do not use currency identifier (like $, £, € etc) in the numeric fields and/or separate numbers in the numeric fields by comma. This can create tracking issues.

#3 Make sure that the data layer code is not placed after the Google Tag Manager container code, as in that case, e-commerce tracking may not work.

Once your developer has modified the data layer with server side script, it will look very different than the data layer code you first sent to him via notepad file.

Something like the one below:

data layer for ecommerce tracking

Note: You can’t see this data layer with PHP code added to it, through ‘view page source’ in your web browser:

view-page-source

If you view the source code of the ‘order confirmation page ‘ via your web browser, you will only see, one of the output of the modified data layer code .

This output may look something like the one below:

ecommerce data layer output

Your final code setup on the ‘order confirmation page’ may look something like the one below (via web browser):

ecommerce-data-layer

 

Step-6: Configure your Google Tag Manager for e-commerce tracking

#6.1 Log into you Google Tag Manager account and create a new tag with following specifications:

Tag Name: Ecommerce Tracking – Order Confirm Page

Tag Type: Universal Analytics

Tracking ID: <enter your property ID here>

Track Type: Transaction

ecommerce-tag-gtm

Triggering:

Trigger Name: Check for order confirmation page

Trigger Type: Pageview

This Trigger fires on: Some Pageviews

Fire this trigger when an Event occurs and all of these conditions are true

url equals <enter the URI of your order confirmation page here>

trigger-configuration

trigger

Your final tag may look like the one below:

final-tag-1

final-tag-2

#6.2 Click on the ‘save’ button to create your new tag.

#6.3 Preview and debug your new tag, create a new container version and then publish the new container.

#6.4 Make sure that you are getting all of the e-commerce data in your GA report and the data is accurate.

Announcement about my new books

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues.
If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Certified web analyst (master level) from MarketMotive.com
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books: