How to Set Up GA4 Tracking Using Google Tag Manager Server-Side Container

Jun 3, 2024
Nov 12, 2020
Also available in

In this blog post, you will explore some advanced features of Google Analytics 4 and show how to configure server-side GA4 using Google Tag Manager. You will learn how to make it resistant to AdBlockers and tracking restrictions with the help of Stape custom loader power-up.

GA4 server-side tracking has a lot of benefits. The main ones are:

  • Compliance with data regulations. Server-side tracking gives you more control over data collection, storage, and procession, so you can align your data handling operations with the data regulations that affect you.
  • Data anonymization. You can anonymize sensitive data before sending it to GA4.
  • Improved data accuracy and quality. Ad blockers, browser limitations, and user actions negatively impact client-side tracking, while GA4 server-side tracking is less sensitive to such issues.
  • Improved attribution and data tracking. Server-side GA4 provides more reliable attribution models and more accurate conversion data, as some server-side events might be missed by client-side scripts.
  • Enhanced data protection. Data sent from the server to GA4 can be secured using HTTPS, which reduces the risk of data interference.
  • Improved performance. Server-side tracking reduces the load from the client side. It also lets you fine-tune data flows before it gets to GA4.
  • Scalability. Server-side tracking handles high traffic volumes better than client-side tracking and also handles complex tracking requirements better.

Check this before we start

Before we get to the GA4 setup steps, let’s cover the basics of GTM server-side tagging. With client-side tracking, the browser sends a request directly to a third-party tracking service like Google Analytics. The interaction takes place between the browser and the third-party service. 

With server-side tracking, an intermediate point is added. In this case, it is the Google Tag Manager cloud server. The request goes first to the cloud server, which processes it and sends it to a third-party system (Google Analytics). This means that the cloud server processes the request instead of the client browser.

how server-side tracking works

It has many benefits, including a prolonged cookie lifetime, increased tracking accuracy, improved page speed, better data security, bypassing AdBlockers and ITPs, and many more. 

You need an active GA4 account before proceeding. Let’s set up a GTM server container using the Stape service.

Step 1. Create a Google Tag Manager server container

1. Select the needed account → Click Admin → Click + next to the Container name.

Create a Google Tag Manager server container

2. Add Container Name → Under Target platform, choose Server.

Create a Google Tag Manager server container

3. Click Create.

create a google tag manager container at stape

4. Choose Manually provision tagging server.

create a google tag manager containner at stape

Step 2. Create a Stape account to host your server GTM container

1. Go to and create an account or log in. You can try our service for free and explore the benefits of Stape hosting for the GTM server.

create a stape account

2. Enter your container details:

  • Container name. The name does not necessarily have to be the same as the container name in your server GTM.
  • Container configuration. Paste the Container Config you copied from your Google Tag Manager Server Container.
  • Server location. Select your server location based on the location of most of your clients. The location can not be changed later.
enter container details on stape

3. Click Create Container.

4. Choose a plan for your container. You can start with a Free plan. Click Continue with a Free plan.

stape billing plans

You will see the status of your container, container config, and plan name. It takes around five minutes to deploy a server container. Please reload the page to update the status. If the setup was done correctly, you should see the status “Running.”

running container on stape

Step 3 Create a Custom tagging server URL

The critical feature of server-side tagging is setting first-party cookies. You need to use a custom tagging server URL to enable this feature. Custom tagging server URL or custom domain should share the same domain as your website.

Due to cookie restrictions, configuring custom domains for sGTM has become more difficult. The main driver is ITP, which affects Safari and Firefox. It limits the lifetime not only of third-party cookies but also of first-party cookies if these cookies were set not from the same IP as your website IP. Let’s discuss ways to configure a custom domain for your sGTM tagging server URL.

  • Same origin
  • Subdomain
  • Default domain
types of custom domain configuration

From our point of view, configuring a custom domain with the same origin domain is the best option because it helps to prolong cookie lifetime in all browsers. However, this method is technically advanced and requires CDN or a load balancer. If you want to configure the same origin domain, follow this guide on our blog.

If you can't configure the same origin domain, we have solutions for prolonging cookie lifetime in Safari: Own CDN and Cookie Keeper.

Below is an example of a custom subdomain setup:

1. Open the sGTM container on Stape, scroll to the domains section, type the domain name, and click Add domain.

add a custom domain on stape

You will see DNS records that you should add. The DNS record depends on the CDN option you choose:

  • Disable CDN - choose not to use any type of CDN.
  • Stape Global CDN - loads scripts from a server closer to the user visiting your website.
  • Own CDN - helps to tag server URLs from the same IP as your website. It gives the ability to set cookies that Safari can not shorten.
cdn options

2. DNS settings depend on the custom domain configuration you choose. Please carefully copy the records you see on Stape and add to your DNS provide, below is an example of settings for Shopify or Wix.

You must set A and AAAA records if you do not use Global CDN.

You must set A, AAAA and CNAME records, if you use Global CDN.

DNS settings 
dns settings

Adding custom subdomain with a CNAME record:

This can be useful when DNS providers restrict adding duplicate records. A common situation is with domains hosted on Shopify or Wix.

You must set CNAME record if you do not use Global CDN.

You must set two CNAME records, if you use Global CDN.

dns setup
dns setup

Once done, you will see DNS records that you should add.

3. Log in to your DNS account and add the records you see in your account. Below is an example of a DNS setup for Cloudflare. Open the DNS setting of the required domain and click Add record.

dns setup

 4. Write the records you see in your account.

dns records on stapee account

5. It usually takes between two and three hours to verify a custom domain, but with some DNS providers it might take up to 72 hours. Once the custom domain is verified, you will see the status Ready in the Stape admin and we will also send you an email if, for any reason, the domain can’t be verified. If the domain can’t be verified, follow this guide on how to troubleshoot the set up.

dns records on stape

6. Once custom domain is verified, Go back to the Google Tag Manager Server container → Choose your server container → Admincontainer settings →paste tagging server URL.

tagging server url

7. Updating the Web GTM script on your website is highly recommended if you use a custom domain. This tweak will load gtm\.js from your domain. To do so, replace the default domain, with the custom domain you set up in the previous step.

update the Web GTM script

Step 4 Create a Google Tag inside the Web GTM container

Open your Web container, create a new tag - Google Tag. Add your Google Tag ID.

In the section Configuration settings add the following parameters:

  • Name: server_container_url
  • Value: Enter the URL you've created

Add trigger to GA4 tag. Normally it should trigger on all page views.

Add trigger to GA4 tag

Optional: You can also create a Google tag: Configuration settings variable that will predefine Google Tag settings if you need to use multiple Google Tags on your website and do not want to add settings for each of the tags manually.

Google tag: Configuration settings variable

These parameters can be, for example, a setting that defines whether you want to send a page view event every time a Google Tag triggers, set UTM parameters, client ID, etc. There is a list of standard Google Tags configuration parameters.

Step 5. Create Google Analytics: GA4 Event tag

To set up the GA4 event, go to the tags section and create a new tag with the tag type Google Analytics: GA4 Event. Add your GA4 ID and the event name; there is a list of standard event names.

ga4 event setup

Step 6. Create GA4 client inside Server GTM container

Open Google Tag Manager Server container → click Client → create GA4 client and save it → go to Tags and set up GA4 tag. Remember that GA4 automatically sends not only pageviews but also some other events.

Inside Event Name, choose variable {{Event Name}} → add trigger Client Name equals GA4.

Create GA4 client inside Server GTM container
Create GA4 client inside Server GTM container

Step 7. Check and debug your setup

Open the Web and Server container preview modes and check that you see GA4 requests. Then, publish the update inside the server and web Google Tag Manager containers.

Check and debug your setup

Step 8: Set up a custom GA4 loader

You can make Google Analytics 4 server-side tracking even more accurate by using a custom GA4 loader. This will allow GA4 to work around any blocking tools, such as ad blockers and other privacy-related technologies that aim to prevent trackers from gathering data about visitors' activities. As a result, you'll see the complete picture of users visiting your site and have more data for ad optimization and custom audiences.

How to set up a custom loader:

1. Log in to your account.

2. Open the sGTM container → Click Power Up.

3. Click Config Custom loader power-up → add your WEB GTM ID → You will see updated web GTM code → Copy this web GTM script and add or replace existing web GTM script on your site.

coonfigure custom loader power-up
coonfigure custom loader power-up

4. Once you've updated the web GTM code and set up server-side GA4, you can check modified GTM and GA4 requests in the network.

Google Tag Manager request URL should look like this:

Tagging server URL/stape container identifier.js?st=web GTM ID without letters GTM.

In our case, it's

Google Tag Manager request URL

GA4 request URL should look like this:

Tagging server URL/gt+stape container identifier.js?id=GA4 identifier.....

In our case, it's

GA4 request URL

Advanced GA4 Features

1. Event-driven data model.

The event-driven data model allows you to create an event for any website activity you want to record inside GA4. You can also use parameters that add more value and description to each event. Google prepared the list of recommended event names to use inside GA4, but you can also create custom events and custom parameters.

2. Cross-device reporting

When it comes to traffic and sales analysis, cross-device user/conversion tracking is the main point. Let’s say a user saw your Adwords ad on the mobile and clicked on it. For some reason, they decided to proceed with the checkout on the desktop. The user, however, did not log into any of the Google services. So, Google cannot recognize them and give credit to that particular Adwords click. As a result, the conversion source is lost, and the information for Adwords optimization is incomplete.

To solve this problem, Google released Signals. Google integrated Signals functionality into GA4 and added machine learning. It is designed to identify users who are not logged into any Google services. Machine learning fills all the gaps if a user is not logged into the Google services or their device cannot be identified via fingerprinting.

By default, Google signals data collection is not enabled inside GA4. You need to enable it manually by going into settings → click data settingsdata collection.

Another essential setting (and a new one) that you should consider checking is how GA4 identifies users.

They have two options:

  • by device only
  • by user-id, Google Signals, then device

3. Predefined events

Google identified the most commonly used events that marketers and business owners track inside Google Analytics and GTM and added them to the enhanced measurement functionality. 

Now, page views, scrolls, outbound link clicks, site searches, video engagement, and file downloads are tracked by default. 

This feature is typically enabled for all GA4 properties. But you need to ensure it’s turned on for your account. To do that, go to Data Streams → choose Webstream details, and check if all events are enabled.

4. Analysis Hub and integration with BigQuery 

Google completely redesigned the analytics/reporting hub inside GA4. It allows users to create pivots, tables, and filters easily. There is also a template gallery, similar to what they have in DataStudio. So you will be able to analyze data more efficiently and spend less time creating custom reports.

BigQuery export is available to all GA4 users. In a nutshell, you can export GA4 data to BigQuery, which can be integrated with other sources like your CRM.

5. Limit of Custom dimension and Custom metric

In GA4, the limit of custom dimensions and metrics increased. Google Analytics 4 allows to have 25 user scoped, and 50 events scoped custom dimensions. Custom metrics increased to 50. An increased number of custom metrics and dimensions gives more flexibility for the customization of GA4.

6. GA4 debug view

The GA4 debugger is extremely helpful for people setting up Google Analytics 4. The enabled preview lets you see what events, event properties, and user data GA4 processes. It makes configuring GA4 much faster and easier.

7.  Other advantages

You might be already tired of reading about why GA4 is so cool. So here’s a quick round-up of other benefits: audience evaluation based on their probability of buying in the next several days, a combination of web and app analytics, and lots more.


GA4 and GTM server tagging was created to fit current world tracking rules, web technologies, restrictions, and data security. You’ll need time to get used to the new interface and features and understand how they can better suit your website and business needs.

If migrating to the GA4 and GTM server containers is complex or you realize that you need assistance, feel free to contact us. We can answer your questions or set up for you.

Tagged with:Google Analytics

Host your GTM server at Stape