Server Side Tracking in Magento 2 — Better Way to Collect Data

Browsers continue to restrick more user's data which results in big data gaps and makes it hard to track customers' journeys. Besides, it's not a secret a lot of users use ad blockers or simply don't accept cookies while in your store.

So, even if you set up Google Analytics 4 for your Magento website, it doesn't mean you will be collecting all the data. But we have a solution.

Today you'll learn about the future of data tracking — server side tracking — and find out how to set up server side tracking in Magento. Spoiler alert: we'll use the Magento 2 Google Tag Manager Extension for that.

It's not an entirely new concept, but still, before we jump to the configuration steps, let's define what is server side tracking.

What is Server Side Tracking?

Server side tracking or server-to-server tracking is a tracking that runs in the server side environment. It enables you to fire tracking pixels and codes from your server instead of the users' browser.

Thus, your server fires tracking pixels and communicates directly with Google Analytics through solutions like GTM. 

Important: when you use server side tracking you DO NOT fire third-party JS code on your website and set up the third-party cookies in the users' browsers. 

Correspondingly, you don't run GA4 in users' browsers — you run GA from your server.   

Confused already? 

Let's also talk about client-side tracking to help you understand server side tracking better. 

What is Client-Side Tracking?

Currently, you use client-side tracking or browser-based tracking. In this case, the browser fires tracking pixels and codes and communicates with Google Analytics 4 through GTM. This way, once the GTM JS is executed, it stores third-party cookies in the users' web browser. Then these cookies send the collected data to GA4. 

It is the most common way of tracking data, But there is a downside. When users don't consent to cookies or use ad blockers, there is a high chance their data won't be collected and sent to GA. 

The Benefits of Server Side Tracking?

Now that you understand why some data might be missing from your GA reports, you must be more eager to set up server-side tracking for your Magento. So, let's sum up all the benefits it offers. 

  • Web browsers and ad blockers can't block server-side tracking which means all data will be collected properly.
  • You get more control over what data is sent to Google with the server-side tracking.
  • Server-side tracking helps to fill the data gaps created by browser-based tracking.
  • Website speed is improved since no tracking codes need to be fired from users' browsers.
  • You can implement API-based conversion tracking to track conversion more accurately.

How to Enable Magento Server Side Tracking?

We agree that server side tracking seems like a complex concept hard to wrap one's head around. However, you don't have to worry about how it is processed as long as you can collect all the data you need in Google Analytics. Agree? 

Thus, we come to the main section of our guide. You can set up server side tracking in Magento 2 using GA4 Measurement Protocol, GTM server side tagging or custom GTM loaders. Let's cover all of them in more detail.

Important! You have to choose only one option — GA4 Measurement Protocol or GTM Server Container. Once you choose it just jump to the corresponding section below. 

Server Side Tracking via Measurement Protocol

One of the easier and cost-free ways to enable server side tracking for Magento is GA4 Measurement Protocol (MP). It is a set of rules you must follow to send raw event data from your server to your GA4 properly.

In short, GA4 Measurement Protocol allows you to send events directly from your server to Google Analytics servers through HTTP requests. That's exactly how server side tracking works. And it's really easy to set up without any technical knowledge. 

To set up server side tracking in Magento using Measurement Protocol:

1. Create Measurement API Secret

Go to your Google Analytics account > Admin > Data Streams, choose a data stream and find the Measurement Protocol API secrets

google analytics data streams

Click on the Create button while on the Measurement Protocol API secrets panel. 

create measurement protocol api secret

Give your API Secret a Nickname. and click on the Create button. 

create new api secret in ga4

Once you create your API Secret it will appear in the grid. Copy the Secret Value and move to the next step. 

measurement api secrets google analytics

2. Set up Google Analytics 4 in Magento

First, navigate to your Magento admin panel. Then go to Stores > Configuration> Google Tag Manager and Analytics > Google Analytics 4

Here you need to enable Google Analytics 4 and set your Measurement ID.

set up google analytics in magento 2

Then move down to the Measurement Protocol (GA4 Server Side Tracking) option, enable it and paste your API Secret in a corresponding field. 

server side tracking in magento 2

Don't forget to Save the settings, and it's basically it. You've enabled the server-side tracking. Now, all you need is to export the Web Container JSON file with the ready-made tags to GTM.

Note: you can use GA4 Measurement Protocol parallel to the browser tracking. So once you set up GTM for Magento and Measurement Protocol you can start testing the tags through GTM debug mode.

Do not configure the GTM server container if you decide to use the Measurement Protocol.

Server Side Tracking via GTM Server Container

Setting up GTM server container for Magento is slightly harder. However, it allows you to extend the measurement data distribution. So the data goes not only to Google Analytics 4 but Google Ads, Facebook and other channels. 

1. Create GTM Server Container

The first step is to navigate to your GTM account to create the server container and follow the steps GTM asks you to.

set up server container gtm

2. Configure GTM server container in Magento

The rest of the setup is the same as for the web container.

Navigate to the Stores > Configuration > Magefan Extensions > Google Tag Manager and Analytics > Server Container and paste your GTM IDs there. Additionally, you need to grab your tag server URL and also paste it there.

gtm server container magento

Same as in the previous section, the last step is to export the JSON file with preset tags to your GTM account. The only difference is you do that in the Export Server Container section on the extension configuration. 

Server Side Tracking via Custom GTM Loaders

Finally, if you use some custom GTM loaders you can still enable server side tracking in Magento 2. For that jump to the Web Container section of the GTM extension settings and enable the Head Script and Body Noscript as the Installation Method for GTM.

Then paste your custom Head Script and Body Noscript in the corresponding fields.

gtm head and body containers magento

Besides, you also need to specify your Account ID and Container ID from the GTM account. 

google tag manager account and container ids

Finally, enable Google Analytics 4 and paste the Measurement ID for it, just like we did for the Measurement Protocol setup.

Save the settings, export web container JSON file to your GTM account and give Google Analytics some time to collect data. 

Note that depending on the way you set up server-side tracking for Magento, you can use different ways to debug Google Analytics 4. Usually, you can just enable the DebugView in GA4 or test the events fired on your website in the Reports > Realtime section.

However, testing the server-side tracking and custom GTM loaders is slightly different.