Stape
Search
Try for free

How to set up GA4 tracking using Google Tag Manager server-side container

Updated
Nov 25, 2024
Published
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.

Introduction to server-side tracking

Server-side tracking collects data from a website or app by sending it to a server. The server processes and sends the data to Google Analytics. This approach offers several benefits, including improved data accuracy, enhanced data protection, and better performance. Businesses that want to reduce the impact of client-side tracking on page loading speed while still collecting valuable data benefit from server-side tracking.

Unlike client-side tracking, where data is collected and processed directly on the user’s device, server-side tracking shifts the data collection and processing to a server. This enhances data security and ensures that the data collected is more accurate and less prone to manipulation.

By utilizing a data layer, businesses can further enhance the accuracy and reliability of the data collected through server-side tracking.

By leveraging server-side tracking, businesses can gain deeper insights into user behavior, optimize their marketing strategies, and ensure compliance with data privacy regulations. Whether you’re using Google Analytics 4 (GA4), server-side tracking can be a game-changer for your data collection strategy

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. 

Before proceeding, you need an active GA4 account. Let’s set up a GTM server container using Stape service.

Step 1. Create a Google Tag Manager server container

1.1 Select your GTM account → Click Admin → Click + next to the Container name.

set up server gm

1.2 Add Container Name → Under Target platform, choose Server. Click Create.

set up server gm

1.3 Choose Manually provision tagging server. Copy your container config. We will need it in the following steps.

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

2.1 Go to stape.io 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.

2.2 Press the button Create container in the top right corner.

2.3 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.

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

You will see the status of your container, container config, and plan name. It takes several 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.”

Step 3. Create a Custom tagging server URL

The critical feature of server-side tagging is setting first-party cookies, which requires a server-side setup. You need to use a custom tagging server URL or domain to enable this feature. The 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 complex. Intelligent Tracking Prevention affects user tracking in Safari and Firefox.

To improve your tracking, you need to set up a custom domain. It will help you prolong your cookie lifetime in all browsers.

Let’s discuss ways to configure a custom domain for your sGTM tagging server URL.

  • Same origin
  • Subdomain
  • Default domain

However, “Same origin” method is technically advanced and requires a CDN or a load balancer. If you want to configure the same origin domain, follow this guide on our blog.

types of custom domain configuration

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

By default, when you add your custom subdomain to your Stape container, it will be verified by the CNAME record.

Below is an example of a custom subdomain setup:

3.1 Log in to your Stape account, choose a container, scroll down and click Add custom domain.

3.2 You will see DNS records that should be added to verify the custom domain.

The DNS record depends on the server location and 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. This feature is free to use and is available for all Stape sGTM containers.
  • 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.

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

cdn settings on stape

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

cdn settings on stape

3.3 Log in to your domain name service (e.g. Cloudflare) and create a new DNS record for the subdomain you want to use.

If you use Global CDN, set additional CNAME record.

3.4 Once your custom domain is verified, you should see the status ready next to the tagging server URL in the stape admin. If you have any problems verifying a custom domain, please follow this guide on troubleshooting a custom domain.

3.5 Go to your Google Tag Manager Server container → Admin → Container Settings → Change Tagging server URL to your subdomain.

Adding custom subdomain with A and AAAA records

If you want to verify your custom subdomain using A and AAAA records, you can do so by clicking Advanced settings after adding the custom subdomain to the container and ticking the “Use A records instead of CNAME records” box.

1. Open your sGTM container on Stape, scroll down to “Domains” and click Add custom domain.

2. Write a custom domain. Click Advanced settings after adding the custom subdomain to the container and ticking the “Use A records instead of CNAME records” box.

3. The DNS records you should configure vary depending on the server location. You will see DNS records that you should add inside your stape.io container.

4. Log in to your DNS account and add records you see in your stape.io account. Below is an example of DNS setup for CloudFlare. Open the DNS setting of the required domain and click Add record.

Then, write the records you see in your stape.io account. Ensure that the proxy is turned off.

Usually, it takes 2-3 hours to verify the custom domain. For some DNS providers, verifying records might take up to 72 hours. We will email you once the custom domain is verified or any error is detected. If there is an error with the verification, please follow this instruction on how to verify custom domain verification.

5. Once the custom domain is verified, go back to the Google Tag Manager Server container → Choose your server container → Admin → container settings → paste tagging server URL.

6. If you use a custom domain, updating the Web GTM script on your website is highly recommended. This tweak will load gtm.js from your domain. To do so, replace the default domain googletagmanager.com with the custom domain you set up in the previous step.

Step 4. Configure Google Analytics 4

There can be two scenarios:

4.1 If you already configured Google Analytics 4 inside your web GTM container, all you need to do is:

4.1.1 Add your server container URL to Google Tag settings. In the configuration settings, add the configuration parameter server_container_url and add your tagging server URL as a value. 

4.1.2 Create Google Analytics 4 client in server GTM container. To do so, open the clients’ section → Create New client → Select client type Google Analytics: GA4 (Web) → Add client name and click Save

4.1.3 In the server GTM container create a new tag with the tag type Google Analytics: GA4

4.1.4 Add Measurement ID and Event Name.

Measurement ID -  Follow this guide to find GA4 ID.  You can add it as variable or if the event came from a GA4 web tag, you can leave this field blank to inherit the measurement ID of the event.

Event Name - the event name to send to Google. See the recommended events for more information. If this field is blank, the value of the event_name parameter will be sent.

4.1.5 Click Triggering and configure a trigger with the trigger type Custom that will fire every time when client name equals GA4 (or the name of the GA4 client that you’ve specified on the step 2.b) → Click Save

4.2 If you do not have GA4 configured, follow the below steps:

4.2.1  Inside the Web container, create a new tag of the tag Type Google Tag. Add your Google Tag ID.

Add trigger to GA4 tag. It usually should trigger on all page views. 

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 tag manually.

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

4.2.2 To set up the GA4 event tracking, 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

4.2.3 Go to your Google Tag Manager Server container. Click Clients and New.

4.2.4 Choose Google Analytics: GA4 (web) and click Save.

4.2.5 Go to Tags and click New.

4.2.6 Choose Google Analytics: GA4.

4.2.7 Add Measurement ID and Event Name.

Measurement ID -  Follow this guide to find GA4 ID.  You can add it as a variable or if the event came from a GA4 web tag, you can leave this field blank to inherit the measurement ID of the event.

Event Name - the event name to send to Google. See the recommended events for more information. If this field is blank, the value of the event_name parameter will be sent.

4.2.8 Click Triggering

4.2.9 Create triggers for the tag. Client name should equal GA4. Example:

Click +

Choose Custom type →  click Some events →  choose Client Name in built-in variables →  set Equals GA4 (the name of your GA4 client) →  click Save

4.2.10 Open web and server GTM debuggers and test the setup. 

Open the Server container preview mode and check that you see GA4 requests. Publish updated inside server and web Google Tag Manager containers. 

Step 5. Set up a Custom Loader [optional]

Custom Loader modifies gtag.js and gtm.js loading paths to resist ad blockers and ITPs, potentially increasing data volume by up to 40%.

5.1 Find and copy your WEB GTM ID in GTM. To find GTM ID log in to your Google Tag Manager account and open a Web GTM container. In the top right corner (next to the Submit and Preview buttons) you’ll see some short text that starts with "GTM-" and then contains some letters/numbers.

 5.2 Open your sGTM container on Stape →  Click Power-Ups.

Find Custom Loader and click Use.

5.3 Add the following settings:

  • Select the domain from the dropdown. Select the custom domain if you configured a custom domain for your sGTM container. Otherwise, select the default one.
  • Add the web GTM ID that you copied on the first step.
  • Select the CMS your website uses or select Other. This configuration affects only the set-up instructions. Stape has WordPress, Magento, and Shopify plugins that simplify server-side tagging configuration.

If you do not use any of these CMS or do not want to use the Stape plugins, select Other. You will see updated web GTM code → Copy this web GTM script and add or replace the existing web GTM script on your site.

Step 6. Update the tagging server URL of your Google Tag in the web GTM

Add the following record inside your Google Tag in the web Google Tag Manager container. 

Configuration parameter: server_container_url

Value: is your domain name in stape.io admin

Step 7. How to test 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. The GA4 Measurement Protocol API allows developers to send data directly to GA4 properties through HTTP requests, enhancing data collection and integration with other server-side options.

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.

Conclusion

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

Try Stape for all things server-sideright now!