GA4 E-commerce Data Layer: How We Set Up Tracking On a Webshop With Two Shopping Carts
In today’s digital age, tracking user behavior on an online store is crucial for optimizing sales. It’s not a common scenario, but if our online store includes both buying and selling options, and on top of that, the purchase cart is separated from the sales cart, as well as the checkout page for purchases from sales, you might wonder how all of this can be tracked in a single Analytics 4, just like for ‘normal’ online stores that only have sales or purchases. The process is similar to that for regular online stores; the most important thing is to have a well-structured DataLayer. The DataLayer is essential for setting up Enhanced E-commerce Tracking.
If you want to learn the Fundamentals of Digital Analytics and How to Make Data-driven Decisions, you can read about it in our blog post.
What is a Data Layer?
A Data Layer is a structured object on a website that presents data used for analytics. It acts as a bridge between the website and tracking tools, such as Google Tag Manager or other analytics platforms. The Data Layer can be set to load on the page before the Tag Manager container code (e.g., on a product page), and it can also be triggered when an action occurs (e.g., clicking the Add to Cart button).
Through the Data Layer, we can define key information about users, their activities, products, and other events on the website. For example, each time a user adds a product to the cart, the Data Layer can provide data to the analytics platform, such as the event that occurred (Add to Cart), product ID, product name, price, and quantity. We use the data from the Data Layer through analytics platforms to track or record the information that is important to us.
Now that we’ve explained the Data Layer and provided an example, you can see why it’s important to have a well-structured one. A good Data Layer also depends on us and what we want to track. We’ve concluded that the Data Layer should contain all the data we want to use for analytics.
Why GA4 Enhanced E-commerce Tracking?
Standard vs. Enhanced E-commerce?
Standard E-commerce provides basic information about transactions, such as:
- Revenue
- Number of orders
- Traffic sources that led to purchases
Enhanced E-commerce, on the other hand, offers a much more detailed view of overall user behavior throughout the shopping process. Enhanced E-commerce uses the Data Layer and provides all the information available in Standard E-commerce, plus additional details:
- Product views
- Adding products to the cart
- Removing products from the cart
- Beginning of the checkout process
- Completion of the purchase
- Use of coupons/promotions
Google Analytics 4 Enhanced E-commerce Tracking is a powerful tool that comprehensively tracks every user step on your site, from product views to purchase completion.
Implementing GA4 Enhanced E-commerce Tracking is a technical step and a strategic move that can enhance your online store’s performance. With precise data and advanced analytical capabilities, you can make better decisions and achieve greater market success.
GTM setup for Enhanced E-commerce Tracking
Setting up Enhanced E-commerce tracking using Google Tag Manager (GTM) allows us to monitor all key interactions with the online store. The programmer’s task is to configure the Data Layer on the page, and you can guide them on which actions are important to you and what data you need in the Data Layer. Therefore, it is crucial to understand the Data Layer to accurately specify your requirements.
There are plugins that add a Data Layer to out-of-the-box e-commerce solutions, which is usually sufficient for basic needs. The programmer will need to work on the Data Layer for additional requirements or on custom platforms. You can read more about properly setting up the Data Layer here: Google Tag Manager Data Layer.
Setting up Google Tag Manager
After correctly setting up the Data Layer, you must set up GTM on your online store. There are several methods for setting it up, such as using various plugins where you mostly just enter your GTM tag. This is the easiest and quickest method. If you don’t have a plugin or prefer not to use one, you can manually set up Google Tag Manager (GTM).
Within GTM, click on your tag (located at the top) and start with GTM, where you will find instructions for adding the code to your site. There are two codes to be added within the <head> tag and the other after the opening <body> tag. It is important that the code is present on every page so that Tag Manager is always loaded.
In summary, GTM links your online store and other analytical and marketing tools, so you don’t have to add code separately for each tool and potentially slow down your site. Instead, you add only the GTM code, and through GTM, you collect data, process it, and decide what to forward to other analytical or marketing tools.
Setting up Variables
In GTM, you can store data such as the GA4 Measurement ID in static variables and use this variable whenever you need it. Variables can also be dynamic, pulling data from sources like the current page’s URL, the Data Layer, and more.
Variables in GTM are categorized into Built-In Variables and User-Defined Variables. Built-in variables are preconfigured by default, while User-Defined Variables are those you create yourself. You can create many types of variables, and here, we will use the Data Layer Variable, which is useful for retrieving data from the Data Layer. Variables can be used in both Tags and Triggers.
In the bottom right corner of User-Defined Variables, click the “New” button. Then, give the variable a descriptive name so you know its purpose, click on “Variable Configuration,” and select “Data Layer Variable.” In the “Data Layer Variable Name” field, enter the path to your data in the Data Layer.
Below is an example of a Data Layer for a product. If you want to capture the Item ID of the product in a variable, you will enter ecommerce.items.item_id in the “Data Layer Variable Name” field.
Create variables for all the data you need in a similar manner.
Recommended Tags
This question depends on what you or your client want to measure and what insights you wish to gain. Here, we will list the recommended tags that are most commonly used.
Before we proceed with the tags, it’s important to note that the naming of tags in Tag Manager is entirely up to you and should be organized in a way that makes it easier for you to manage. However, we recommend using the names suggested by Google for the “Event name” within the tags you send to Analytics. Using the recommended event names will help Google Analytics 4 better understand and accurately represent the data.
- Google Tag – First, we connect Google Analytics and other Google tools through the Google Tag. Add the Google Tag and enter the “MEASUREMENT ID,” then set the Trigger to “All Pages.” This connects Analytics to the website and will appear on all pages where Tag Manager is set up.
- GA4 Event—Set up a Tag to send Events to GA4. You can set up a single Tag to send all the Events you choose to GA4, but here, I will set up each Event with its own Tag to better understand the setup.
- Product List – We use the Event name view_item_list – This event shows when a user has viewed a group of products, usually a product category. The Data Layer should appear and contain product data, including product ID, product name, product category, price, google_business_vertical, and index (position in the list).
- Product View—We use the Event name view_item. This event shows when a user has viewed a product. The Data Layer typically contains data such as value, currency, item_id, item_name, price, item_category, and google_business_vertical.
- Add to Cart—We use the Event name add_to_cart. This event shows when a user has added a product to the cart. The Data Layer generally contains data such as value, currency, item_id, item_name, price, item_category, google_business_vertical, and quantity.
- Remove from Cart – We use the Event name remove_from_cart – This event shows when a user has removed a product from the cart, and the Data Layer contains the same data as for Add to Cart.
- View Cart – We use the Event name: view_cart – This event shows when a user has viewed the cart. The Data Layer typically contains data such as: value and currency of the total cart, followed by all products in the cart, each including item_id, item_name, item_category, quantity, currency, price, google_business_vertical.
- Begin Checkout – We use the Event name: begin_checkout – This event shows when a user has reached the Checkout page. The Data Layer contains the same data as for View Cart.
- Add Shipping Info – We use the Event name: add_shipping_info – This event shows when a user has entered shipping information.
- Add Payment Info – We use the Event name: add_payment_info – This event shows when a user has entered payment information.
- Purchase – We use the Event name: purchase – This event shows when a user has completed an order. The Data Layer contains data such as: value and currency of the total order, followed by all products in the order, each including item_id, item_name, item_category, quantity, currency, price, google_business_vertical.
Triggering Configuration
Triggering in GTM sets rules that determine when a specific Tag will activate to send data. We also use the Data Layer for triggers, which contains the event and its name. We can also use another type of Trigger, and here is a list of basic types.
Basic types of triggers
- Page View is used when we want to trigger an event when a user lands on a specific page or pages containing a certain part of the URL.
- Click Trigger – used when we want to trigger an event when a user clicks on a specific element on the website.
- Form Submission – used when we want to track when a user fills out and submits a contact form.
- Custom Event—the most common trigger for Enhanced E-Commerce; it tracks events (add to cart, remove from cart, etc.). Events are sent through the Data Layer, and using the Data Layer, we know when each event occurred.
Custom Event triggers
Custom Event triggers are most commonly used for Enhanced E-commerce, and they also require a good Data Layer that contains all the necessary information. The Data Layer must include an event with the format event: “event name” when a specific event occurs. The event name is used as the trigger for firing Tags.
In the menu, go to Triggers, then click the “New” button on the right side, and name the Trigger with a name that indicates its purpose. After entering the name, click on Trigger Configuration and select Custom Event. Under the Event name, enter the value from the Data Layer for the Event.
After creating the Trigger, go to Tags and assign the Trigger to each Tag, specifying when the Tag will be activated using the Trigger.
What is needed for two carts
Everything discussed so far was for standard Enhanced E-Commerce tracking. Now, we will add a few things to the existing setup to track two carts and other actions separately.
Let’s take an example where we perform both purchases and sales on the same online store and want to track both with all the relevant information we can obtain. We can do this in several ways, and we will describe one.
First, we start with the Data Layer. In the Data Layer, we need to modify event names by appending “-sale” to the events related to sales and “-purchase” to the names of the events related to purchases. Another way is to add an event category within the event in the Data Layer and set the value to “sale” if the event is related to sales or “purchase” if the event is related to purchases. This way, we adjust the Data Layer for all the events we track by distinguishing events with names like “purchase” and “sale” or keeping the same name but adding the event_category value to differentiate the events.
- Once we have updated the Data Layer, we will set up data to send to GA4 in Tag Manager. If we modify the event names in the Data Layer, we create all configurations (Trigger and Event) separately for sales and purchases. Then, in each Tag that sends an Event to GA4, under the “Event Parameters” option, we add the event_category event_parameter and set the Value to “purchase” for purchase events or “sale” for sales events.
- If we opted for the alternative approach where we did not change the event names in the Data Layer and only added the event_category with values, we used a Variable to retrieve the data from the Data Layer. In each Tag that sends an Event to GA4, under the “Event Parameters” option, we add the Event Parameter event_category and set the Value to the Variable that contains the information indicating whether it is a purchase or a sale.
We are done with the setup; we need to test everything to ensure it works correctly. Start Preview mode in GTM and check if the events are triggering properly and sending the correct data.
How to track separate carts in GA4
All data will be aggregated for purchases and sales when no filters are applied. To separate and view data for either purchases or sales only, follow these steps:
- On any report, click on “Add filter.”
- A filter will appear on the right side. Click on “Select dimension” and choose “Event Category.”
- Under “Select match type,” choose “Exactly matches.”
- In the Value field, select what you want to track (e.g., “purchase” or “sale”).
- Click “Apply” to view data for events related to the specific cart category.
Setting up separate tracking for purchase and sale carts is not difficult or complicated; it only requires adding the event_category for filtering in GA4. We use standard event names for purchases, adding to cart, and similar actions to ensure GA4 displays correct results. Therefore, we add the event_category and filter events based on it. The key is to configure events and data in the Data Layer correctly. Now that we have set up analytics, we can start advertising, and the first step is linking Analytics with the Google Ads account.