How to Set up GTM Server Container in Magento 2?

Although browser-based tracking has proven to be effective in collecting data, it's not 100%. Due to 3rd-party cookies and adblockers customers use, some data still doesn't make it to your Google Analytics account. To avoid this, you need to find a way to bypass the 3rd party-cookies.

The only solution is to take the tracking to an entirely new level — server-side tracking. 

You can either set up server-side tracking in Magento using the GA4 Measurement Protocol or GTM Server Container. Today, we'll walk you through the latter and explain how to set up and use it properly. The tool you'll need for a smooth implementation is Magento 2 Google Tag Manager

However, before we move to the main part of the guide, let's define what is GTM server container.

What is a GTM Server Container?

GTM server container is a new way of using Google Tag Manager to send measurement data from your server to GTM servers. Once you collect that data you can choose how to shape it and where to route it from the GTM server (to GA4, Google Ads, etc.)

server side tracking in gtm

So, basically, you create tags, triggers and variables the same way you used to in the browser-based tracking. The only difference is that the tags won't rely on the page container to provide the data, they will receive the data directly from your server. 

Server Container vs GA4 Measurement Protocol

If you've already skimmed through the topic, you probably know that GA4 also provides server-side tracking. So, why bother setting it up in Google Tag Manager and how is it different?

When you use the GA4 Measurement Protocol, the data is sent from your server to the GA4 server through HTTP requests. That's it. 

When you use a GTM server-side container, the data is sent from your Magento server to GTM. Then you can send than data to GA4, Google Ads, Facebook and other channels. 

How to Set Up GTM Server Container in Magento 2?

Essentially, the GTM server container is no different from the web container. You use the same tags, triggers and variables as in the browser-based tracking. The main difference is you need to create the server-side property.

1. Create a server-side GTM container

Navigate to your GTM account and click on the three dots near your property to choose the Create Container option.

create gtm server container

Fill out the Container Name and choose Server as a Target Platform. Then click Save.

set up server container gtm

Since you need to host your new server container somewhere, you need to create a new project in the Google Cloud Platform. For that click on the Automatically Provision tagging server option (since it's much simpler than creating it manually).

install google tag manager

If you haven't used the Google Cloud Platform before, you'll need to create a billing account and fill out your card details. So, next, click on Create a Billing option to complete the steps you're asked to.

create billing account for google cloud

Note: don't get confused, the GTM server-side container is free and it doesn't cost anything in the testing configuration. However, once you start using it on the live website with decent traffic, you'll be charged. Thus, get familiar with the Google Cloud pricing to clear the doubts.

2. Get GTM public ID, account ID, container ID

Same as with the web container, you need to get the following details to set up the GTM server container in Magento:

  1. Public ID — find it in your container Workspace tab top right menu.
  2. Account ID — find it in your container browser address after /accounts/.
  3. Container ID — find it in your container browser address after /containers/.

GTM container details

3. Get tag server URL

The next important thing to get is the server URL since you need it to connect your Magento store to the Google Cloud server. For that go to your GTM account > Admin and click on the Install Google Tag Manager option. Then copy the Default URL

gtm tag server url

4. Paste GTM server details into your Magento

Once you have all the GTM server details, you need to paste them into your Magento. Navigate to Stores > Configuration > Magefan Extensions > Google Tag Manager and Analytics > Server Container and taste your GTM IDs there along with the Tag Server URL.

gtm server container magento

5. Configure Google Analytics 4

Additionally, you need to paste your GA4 Measurement ID into Magento to connect your store with GA4. So, while you're still in the configuration section, move to the Google Analytics 4 section, enable it and paste your Measurement ID there. 

set up google analytics in magento 2

Note: to get your Measurement ID, navigate to your GA4 property > Admin > Web stream details.

Don't forget to Save the settings once you finish setting up the GTM server details and GA4. 

6. Export ready-made tags to GTM

Since the Magento 2 GTM Extension has everything set up for you, the last step is to export the ready-made tags and import them to GTM. 

Scroll down to the Export Server Container section and press on the Generate JSON Container & Download File button. 

export server container magento

Then go back to the GTM account > Admin and click on the Import Container option.

import gtm server container

The process is entirely the same as with the web container. You upload the JSON file you've just exported from Magento. Then choose the workspace. preview the tags and click Confirm to finish the import. 

Once you finish, you can find all the tags created under the Tags tab in your GTM Workspace

gtm servr container setup

7. Check the GTM connection

After you set up the GTM server container, you need to make sure the events are tracked properly. There are two scenarios for testing: via web container or Google Analytics.

Test GTM Server Container via Web Container

If you also use the web container, navigate to it and find the Magefan GA4 - Ecommerce tag. Click on it to add your tag server URL as the event parameter, and Save the event. 

gtm event parameter

After that open the debug more (Preview) and trigger some events on your store. Back in the GTM click on the GA4 -E-commerce tag to see the event parameter. 

gtm event parameter value

Then open the server container in the other tab and also click on Preview. You should see some events triggered as you go through your store. Click on the event and switch to the Request tab. You'll see the HTTP request being passed to the GTM server.

gtm serve tagging request

This means you've set up the GTM server container properly. 

Test GTM Server Container via Google Analytics

In case you don't use the web container, you can use GA4 to test the connection. Simply navigate to your GA4 property > Reports > Realtime and find the events fired in your store within the last 30 minutes. That's about it.

Google analytics report

8. Publish GTM tags

Finally, after you checked the GTM server connection and finished the setup, Submit the tags.

submit gtm tags

Simply specify the Version Name and Version Description, and click Publish.

Submit changes to GTM

You can also review the Version Summary.

gtm versions

That's it. You've just finished setting up the GTM server container or GTM server-side tagging for your store. Now you'll be able to manage your marketing campaigns more effectively. Since we've also configured the GA4, data will be automatically sent to your Google Analytics 4 account.

However, you can also set up Dynamic Remarketing and Conversion Tracking and send collected data to Google Ads or other channels.