Using GA4 for E-commerce Tracking and Analysis

May 24, 2023

12 min read

As e-commerce continues to grow and become a more significant part of our daily lives, it’s more important than ever to have robust tracking and analysis tools at your disposal. Fortunately, Google Analytics 4 (GA4) offers just that, with a suite of features designed specifically for E-commerce businesses.

In this article, we’ll go in-depth on how to set up GA4 for eCommerce tracking. By the end of this guide, you’ll have a solid understanding of how to leverage GA4 to gain valuable insights into user behavior and make data-driven decisions to grow your business.

E-commerce Report Overview

First, let’s take a closer look at the e-commerce report in GA4. This report provides an overview of key metrics related to your eCommerce site, including revenue, conversion rates, and average order value. It also allows you to drill down into specific eCommerce interactions, such as add-to-cart clicks and checkout initiations.

In addition to the standard e-commerce report, you can also create custom e-commerce reports in GA4. To do so, click on “Custom Reports” in the left navigation and select “Create Custom Report”. From there, you can add e-commerce metrics and dimensions to your report and customize it as per your requirement.

What is an E-commerce Interaction?

An e-commerce interaction refers to any action a user takes on your e-commerce site, such as adding a product to their cart, initiating checkout, or completing a purchase. Tracking these interactions is crucial for understanding user behavior and identifying areas for optimization. 

To track e-commerce interactions in GA4, you need to set up enhanced e-commerce tracking for your data stream. This involves enabling enhanced measurement in your data stream settings, configuring your e-commerce settings, and setting up the appropriate tags in Google Tag Manager.

Once enhanced e-commerce tracking is set up, you can view e-commerce interactions in GA4 by navigating to the “Events” report and selecting the “e-commerce” event parameter. The report will display metrics such as revenue, conversion rate, average order value, and more, broken down by product and transaction.

What is a Data Layer?

To track e-commerce interactions, you’ll need to deploy a data layer on your website. A data layer is a piece of code that sends information about e-commerce interactions to GA4, including details such as product name, price, and quantity, as well as information about the user and their session. To set up a data layer in GA4, you need to add the data layer code to your website. This code typically goes in the head section of your website and is written in JavaScript. Once the data layer is set up, you can use Google Tag Manager to add tags and other tracking scripts to your website and use the data layer to pass custom data to GA4.

Here’s an example of how a data layer can be implemented within an e-commerce website:

<script>
  // Define the data layer object
  window.dataLayer = window.dataLayer || [];
  // Push e-commerce-related data to the data layer
  window.dataLayer.push({
    'event': 'purchase',
    'e-commerce': {
      'purchase': {
        'actionField': {
          'id': '12345', // Transaction ID
          'revenue': '99.99', // Total revenue
          'shipping': '5.00' // Shipping cost
        },
        'products': [{
          'name': 'Product A', // Product name
          'id': 'P12345', // Product ID
          'price': '49.99', // Product price
          'quantity': 2 // Quantity purchased
        }]
      }
    }
  });
</script>

In this example, the data layer is utilized to capture a purchase event. The relevant information, such as the transaction ID, total revenue, shipping cost, and details about the purchased product (name, ID, price, and quantity), is structured within the data layer object.
By implementing the data layer in this manner, GA4 can effortlessly retrieve and process this information for accurate e-commerce tracking and analysis. This enables businesses to gain valuable insights into purchase behavior, revenue generation, and product performance, ultimately empowering data-driven decision-making for enhanced e-commerce strategies.

Streamlining Analytics: Deploying the Data Layer on Your Website

To leverage the power of a data layer for enhanced analytics, deploying it correctly on your website is crucial. Follow these steps to seamlessly integrate the data layer into your website:

  • Identify the key data points:
    Determine the specific information you want to capture using the data layer. This may include events, user interactions, transaction details, or any other relevant data for your analytics needs.
  • Implement the data layer code snippet:
    Insert the data layer code snippet within the <head> section of your website’s HTML code or before the closing </body> tag. It is typically a JavaScript code block that initializes the data layer object.
<script>
  window.dataLayer = window.dataLayer || [];
</script>
  • Push data to the data layer:
    Once the data layer is initialized, you can start pushing data to it. Use JavaScript code snippets to push relevant data as objects into the data layer. For example, to track a specific event like a button click, you can use the following code:
<script>
  window.dataLayer.push({
    'event': 'buttonClick',
    'buttonId': 'myButton'
  });
</script>
  • Customize for e-commerce tracking:
    If you are implementing the data layer for e-commerce tracking, tailor the code snippets to capture essential transaction details. For instance, when a purchase occurs, push the relevant data to the data layer:
<script>
  window.dataLayer.push({
    'event': 'purchase',
    'e-commerce': {
      'purchase': {
        'actionField': {
          'id': '12345',
          'revenue': '99.99',
          'shipping': '5.00'
        },
        'products': [{
          'name': 'Product A',
          'id': 'P12345',
          'price': '49.99',
          'quantity': 2
        }]
      }
    }
  });
</script>
  • Test and validate data layer implementation: Verify that the data layer is correctly capturing and pushing data by testing it on your website. Use browser developer tools or Google Tag Assistant to inspect the data layer and ensure the data is being structured and sent accurately.

By following these steps, you can effectively deploy the data layer on your website, enabling seamless data capture and empowering advanced analytics capabilities. With a properly implemented data layer, you’ll have access to accurate, granular data for informed decision-making and optimization of your online presence.

Sending Data to GA4

Once the data layer is in place, you’ll need to configure GA4 to receive and process the e-commerce data. This involves creating a custom event that triggers when an e-commerce interaction occurs and mapping the data layer variables to GA4 fields.

To send data to Google Analytics 4 (GA4), follow these steps:

Obtain your GA4 Measurement ID:

  • Sign in to your Google Analytics account.
  • Go to the Admin section.
  • Select your desired GA4 property or create a new one.
  • In the Property column, click on “Data Streams.”
  • Create a new data stream or select an existing one.
  • In the Stream Details, note down the Measurement ID, which looks like “G-XXXXXXXXXX”. You’ll need this ID to initialize the GA4 tracking code.

Install the GA4 tracking code:

  • Within the <head> section of your website’s HTML code, add the following code snippet just before the closing </head> tag:
<script async src="https://www.googletagmanager.com/gtag/js?id=GA4_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());
  gtag('config', 'GA4_MEASUREMENT_ID');
</script>

Replace “GA4_MEASUREMENT_ID” with your unique GA4 Measurement ID.

Sending pageview data:

  • To track pageviews, add the following code snippet on each page you want to track, preferably before the closing </head> tag:
<script>
  gtag('event', 'page_view', {
    'page_title': 'Page Title',
    'page_location': 'https://www.example.com/page-url',
    'page_path': '/page-url'
  });
</script>

Customize the values of ‘page_title’, ‘page_location’, and ‘page_path’ to match the specific page being tracked.

Tracking custom events:

  • To track custom events, such as button clicks or form submissions, use the gtag() function with the desired event name and relevant parameters. For example:
<script>
  gtag('event', 'custom_event', {
    'parameter1': 'value1',
    'parameter2': 'value2'
  });
</script>

Customize ‘custom_event’, ‘parameter1’, and ‘parameter2’ with the appropriate event name and parameter values.

E-commerce tracking:

For e-commerce tracking, utilize the Enhanced Measurement feature in GA4. Ensure that the necessary product and transaction information is available in the data layer before the GA4 tracking code. Refer to the previous section on “What is a Data Layer?” for an example of how to structure e-commerce data in the data layer.

Verify data in GA4:

  • After implementing the tracking code, use the GA4 Realtime reports and the DebugView feature in Google Analytics to verify that data is being captured and processed correctly.

How to track adding or removing an item from a shopping cart using the data layer in GA4

  • Add the data layer code to your website:
    The data layer code initializes the dataLayer variable as an array, establishing an empty container that will later be populated with specific event data. This ensures that the data layer is available for capturing and processing information related to adding or removing items from the shopping cart.
  • Define the data layer variables:
    In Google Tag Manager, you need to define the data layer variables that you want to use to extract data from the data layer. For example, you can create a data layer variable for the product ID, product name, price, and quantity.
  • Create the tags and triggers:
    Once the data layer variables are defined, you can create tags and triggers in Google Tag Manager to track adding or removing items from the shopping cart. For example, you can create a tag that tracks adding items to the shopping cart and use a trigger that fires the tag when the “addToCart” event is detected in the data layer. Similarly, you can create a tag that tracks removing items from the shopping cart and use a trigger that fires the tag when the “removeFromCart” event is detected in the data layer.
  • Preview and publish the changes:
    Once you have created the tags and triggers, you need to preview and publish the changes in Google Tag Manager. This allows you to test the tracking on your website and ensure that the data is being collected correctly.

Summary

Setting up GA4 for e-commerce tracking involves deploying a data layer on your website, configuring GA4 to receive and process e-commerce data, and mapping data layer variables to GA4 fields. With e-commerce tracking in place, you can gain valuable insights into user behavior and make data-driven decisions to grow your business.
At SmartApp, we specialize in helping e-commerce businesses get the most out of GA4 and other analytics tools. Our team of experts can help you set up and configure GA4 for your e-commerce site, as well as provide ongoing support and analysis to ensure that you’re getting the most out of your data.
Get in touch with us today to discover more about our services and the ways we can contribute to the success of your business.

Ready to start?

Contact our CTO or fill out the form

    By entering your email, you agree with our Terms of use and Privacy policy