✓ 100% independent
New Metrics

Knowledge base article

Getting started with server-side Google Tag Manager (GTM): A Comprehensive Guide

In Tracking Implementation6 minutes reading time

Google Tag Manager (GTM) server-side tagging lets you collect data from your website or app and forward it to analytics and marketing platforms through your own tagging server. Instead of every vendor collecting data straight from the browser, tags send data to your server first. That makes data collection more reliable as browsers restrict third-party cookies. Below are all the steps you need to take to implement server-side GTM for the best results.

Step 1: Create a GTM Server Container

To begin, you need to create a new server container in your Google Tag Manager account. This is where you will configure and manage your server-side tags.

  1. Log in to Google Tag Manager and go to the account where you want the container.
  2. In the account row, open the overflow menu (three dots) and click Create Container.
  3. Give your container a name and select Server as the target platform.
  4. Click Create. Tag Manager now asks how you want to provision your tagging server.

Step 2: Provision Your Tagging Server

You have two options to provision your tagging server: automatic or manual. Let’s look at both of them.

Automatic Provisioning

Automatic provisioning is the simplest way to set up your server. Tag Manager creates a new Google Cloud project and deploys a preview server and a single tagging server on Cloud Run with a testing configuration. There are downsides though. Google says this setup should only be used for testing, because it doesn’t support a production workload. The region is also always us-central1 (Iowa, in the US). That can be a downside when your users are located elsewhere.

Still, for testing and for learning how server-side GTM works it is useful, and you can upgrade the same project to a production setup later. You can set it up as follows:

  1. Select Automatically provision tagging server after creating your container. This will set up your server on Google Cloud.
  2. Choose or create a billing account. If you don’t have one, you can create a new billing account from this menu. Note that you will need to enter your credit card details. While you only send test traffic, usage normally stays within the free tier.
  3. Follow the prompts to complete the setup. Google Cloud will handle the server configuration, which may take a few minutes.

Manual Provisioning

If you prefer more control over your server setup or want to host it on a platform like Stape or Taggrs, you can opt for manual provisioning:

  1. Select Manually provision tagging server after creating your container and copy the Container Config string. Every server you deploy needs it.
  2. Set up your own server environment. Google recommends Cloud Run, which you can configure yourself in the Cloud Run console or with the command line, choosing the region and the number of instances. You can also run the official Docker image on any other cloud provider, such as AWS or Azure, or paste the container config into a hosting service like Stape.
  3. If you host it yourself, you deploy the image twice: exactly one preview server (with RUN_AS_PREVIEW_SERVER set to true) and a tagging server cluster that points to the preview server’s URL. Both need an HTTPS address in front of them.

If you would like to learn more about alternatives to Cloud Run like Stape or Taggrs you can read our comparison article, or see our overview of Stape alternatives.

Step 3: Connect Billing Account and Set Up Server

If you chose automatic provisioning, you will need to connect a billing account to cover any costs associated with running your server.

  1. Select your billing account from the list or create a new one if you don’t have an existing one.
  2. Follow the prompts to add your billing information, including a credit card.
  3. Once the billing account is set up, confirm it and let Tag Manager create the server. Google Cloud will then set up your server, which may take a few minutes.

Keep in mind that a production setup does cost money. Google recommends at least 2 Cloud Run instances with 1 vCPU and 0.5 GB memory each, and estimates each instance at about $45 per month. We compare those costs with a hosted service in Google Cloud Run vs. Stape.io.

Step 4: Configure Your Server

After the server is provisioned, you will see a window with your server details, including the container configuration and the default URL. You can reopen this window later by clicking the container ID (GTM-XXXXXX) in the top bar of Tag Manager.

Viewing Server Details

  1. Open the Google Cloud console and select the project that Tag Manager created for you.
  2. Navigate to Cloud Run to view your tagging server (server-side-tagging) and your preview server (server-side-tagging-preview). Here, you can manage the scaling, security and other settings of your server.

Custom Domain Setup

Serving your tagging server from your own domain is crucial for ensuring that cookies are considered first-party and not blocked by browsers. On the default run.app address the server can’t set server-side cookies at all. Google describes two options:

  1. Same origin (Google’s best practice): serve the tagging server from a path on your own website, for example example.com/metrics. This requires a CDN or load balancer that forwards those requests to your server.
  2. Subdomain: use a subdomain such as metrics.example.com and point it to your server with DNS records at your domain provider. For Cloud Run, Google recommends a global external Application Load Balancer. Cloud Run’s own domain mapping is simpler but is in Preview, is only available in some regions and is not recommended for production.
  3. Add the custom URL in your GTM server container under Admin, Container Settings, and save. This replaces the default URL provided by Google.

Step 5: Set Up Data Forwarding

Data forwarding is the core function of GTM server-side tagging. You need to configure your setup to send data from your website or application to the GTM server container and then to the desired analytics or marketing platforms.

From Your Website or App to the GTM Server Container

Google recommends using the Google tag on your web pages to send data to the server container. The server container already contains a Google Analytics client that receives these requests. Here’s the general approach:

  1. In your web container, create a user-defined variable of the type Google tag: Configuration settings. Add the parameter server_container_url with your tagging server URL as the value.
  2. Select this variable in the Configuration settings of your Google tag and use the Initialization – All Pages trigger, so the tag fires before other tags.
  3. In your GTM server container, check the Google Analytics client under Clients. It is pre-installed and claims the incoming requests. For other sources, such as apps or your own backend, you can add a client like the Measurement Protocol client.

From GTM Server to Vendor’s Platform

Once the data is received by the GTM server container, you need to forward it to the desired platforms. Here’s how you can do it:

  1. Go to Tags in your GTM server container and create a new tag.
  2. Select the appropriate tag type (e.g., Google Analytics: GA4) and configure the tag settings.
  3. Set up triggers and variables as necessary. For example, you might set up a trigger based on a custom event or a built-in variable.
  4. Ensure that the tag is configured to send data to the correct endpoint and account for the vendor’s platform.

Step 6: Test and Publish

Testing is a critical step to ensure that your setup is working correctly. Do not publish any changes before doing this, as mistakes can cause downtime for all your current tracking and mess up advertising campaigns.

Using Preview Mode

  1. Use the GTM preview mode to test your setup. Open the preview mode in both the server and web containers to ensure data is being sent correctly.
  2. Verify that the data is being received by the GTM server container and forwarded to the intended platforms.

Health Check

Test the health of your tagging server by appending /healthy to your tagging server URL. You should see the text “ok” if everything is working correctly.

Step 7: Scale Your Server (Optional)

The automatically provisioned server is a testing setup. Before you send real traffic to it, or if you anticipate high traffic, you need to change it to production settings.

  1. Go to the Cloud Run section in your Google Cloud project and select the tagging server.
  2. Click Edit & Deploy New Revision. Google recommends a minimum of 2 instances to reduce the risk of data loss during an outage, and a maximum (for example 10) that caps your costs. Google expects 2 to 10 instances to handle 35 to 350 requests per second, depending on your tags.
  3. Use 1 vCPU and 0.5 GB memory per instance with CPU always allocated, as Google recommends. If you handle more than about 1 million requests per month, consider turning off request logging, as it can add significant costs.

Conclusion

Setting up GTM server-side tagging involves several key steps, but it offers significant benefits in terms of data reliability and compliance with browser restrictions. Here’s a quick summary:

  1. Create a GTM server container
  2. Provision your tagging server (automatic for testing, manual or a hosting service for production)
  3. Connect your billing account and set up the server
  4. Configure your server and set up a custom domain
  5. Set up data forwarding from your website/app to the GTM server and to the vendor’s platform
  6. Test and publish your setup
  7. Scale your server to production settings

By following these steps, you can ensure a smooth transition to server-side tagging and maintain accurate and reliable data collection for your analytics and marketing needs.

If you have further questions about the process or want to learn more about advanced topics, feel free to contact us.

Continue learning

Freek KampenIvo Taris

Need an answer for your own setup?

Questions about your tracking, GA4 or which tool fits? Ask Freek and Ivo, the analytics specialists behind New Metrics. You get a reply from one of us, not a bot.

Contact us