serkankaya.
Türkçe sürüm
Let’s work together ↗

Server-Side GTM Setup: A Step-by-Step Guide with Cloud Run

Server-side GTM setup step by step: server container, Cloud Run, custom domain, GA4, Google Ads and Meta tags, testing and keeping costs under control.

Server-side GTM setup means opening a server container in Google Tag Manager, running it on a cloud server, connecting it to your site’s own domain and pointing the browser tags at that server. The technical part of the setup is quick with Cloud Run. The real work happens in the custom domain, the preview server, the consent setup and the testing steps.

In this post I walk through the setup in the order I apply it: prerequisites, architecture, provisioning on Cloud Run, the custom domain, routing the web container and verification. If you want to know what server-side tracking is and what it solves first, I recommend reading what is server-side tracking before you start.

What do you need for a server-side GTM setup?

A server-side GTM setup requires a Google Tag Manager account, a Google Cloud account with billing enabled, access to your domain’s DNS settings and a clear event map. If any of these is missing, the setup either stays half-finished or carries the wrong data to the server.

Google’s Cloud Run guide lists the prerequisites clearly.[1] You need a Google Cloud account, a billing account, and the person doing the setup needs the Project Creator and Billing Account User roles. If you are opening a new billing account, you also need the Billing Account Creator role.

Alongside the technical prerequisites, I make two more preparations:

  • Event map: I write down in a table which event goes to which platform with which parameters. The value, currency and ID fields of conversion events such as purchases, form submissions and appointments are settled here.
  • Domain decision: I decide up front whether the tagging server will run on a subdomain such as metrics.yourdomain.com or on a same-origin path such as yourdomain.com/metrics. That decision determines the DNS, CDN and load balancer work.

How does the server-side GTM architecture work?

In the server-side GTM architecture, the web container in the browser sends the event to the tagging server on the site’s subdomain. The server container running on the tagging server claims the request with a client, turns it into an event and fires the platform tags. Debugging sessions run on a separate preview server.

Web container Tagging server sGTM client GA4, Ads, Meta
The browser sends a single measurement stream; the server container turns it into events and distributes them to platforms.

In this structure, three concepts need to be kept apart:

  1. Server container: The container where you manage tags, triggers and variables in the GTM interface. It resembles the web container but runs on a server, not in the browser.
  2. Tagging server: The server or cluster of servers that runs the server container on live traffic. Google recommends provisioning a cluster rather than a single server for better availability and scalability.[4]
  3. Preview server: It only runs Preview mode. According to Google’s manual setup guide, you should deploy exactly one preview server and not scale it beyond one instance.[4]

Every HTTP request arriving at the server container is claimed by an sGTM client. Google defines clients as adapters between the software on the user’s device and the server container; the GA4 client comes pre-installed in every server container by default.[3]

How do you set up server-side GTM step by step?

Short answer

How do you set up server-side GTM step by step?

To set up server-side GTM, you first create a Server container in GTM and provision the tagging server on infrastructure such as Cloud Run. Then you connect the server to a custom domain, add the address to the container settings and update the Google tag in the web container so it sends events to that address.

  1. Create a new container of type Server in GTM.
  2. Provision the tagging server on Cloud Run automatically or manually.
  3. Run the preview server as a single instance.
  4. Connect the tagging server to a custom domain.
  5. Add the server URL to the container settings.
  6. Point the Google tag in the web container to that address.
  7. Set up and test the platform tags in the server container.

The order below matches the flow in Google’s Cloud Run guide and the order I apply in practice.[1]

  1. Create the server container

    In GTM, open a new container from the Admin section and choose Server as the target platform. Once the container is created, the setup options screen opens.

  2. Provision the tagging server

    Automatic provisioning sets up the Google Cloud project and the Cloud Run service in a few clicks. If you will use your own infrastructure, choose manual provisioning and copy the Container config value.

  3. Configure scaling

    In Cloud Run, select the service and use Edit & Deploy New Revision to set the minimum and maximum number of instances. Leave the preview server on a single instance.

  4. Connect the custom domain

    Route the tagging server to a subdomain or to a same-origin path. Complete the DNS record and the SSL certificate.

  5. Add the server URL to GTM

    In the server container, go to Admin > Container Settings and save the server address with Add URL. If you have several domains, the path must be the same on all of them.

Docker commands for a manual setup

If you will run it on your own server or on another cloud, you use Google’s official Docker image. The manual setup guide defines two required environment variables: CONTAINER_CONFIG, plus RUN_AS_PREVIEW_SERVER for the preview server or PREVIEW_SERVER_URL for the tagging server.[4] The first command starts the preview server, and the second starts a tagging server instance.

docker run -p 8080:8080 \
  -e CONTAINER_CONFIG='<container-config>' \
  -e RUN_AS_PREVIEW_SERVER=true \
  gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable

docker run -p 8080:8080 \
  -e CONTAINER_CONFIG='<container-config>' \
  -e PREVIEW_SERVER_URL='https://preview.yourdomain.com' \
  gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable

According to Google, every server instance in the cluster must be configured with the same CONTAINER_CONFIG and PREVIEW_SERVER_URL values.[4] The load balancer or CDN in front of the preview server must also have a timeout longer than 20 seconds.[4]

Cloud Run, Stape or your own server?

Cloud Run is the option Google describes in its own guide and the fastest one to set up with automatic provisioning. Managed services such as Stape take over cloud management for you. Running Docker on your own server gives you the most control, but it also leaves the maintenance load entirely to you.

Cloud Run (Google Cloud)

  • Set up from inside GTM with automatic provisioning
  • Scaling and region settings are under your control
  • Billing and logging costs land on your account
  • Version updates are done by deploying a new revision

Managed service (e.g. Stape)

  • Set up without opening a cloud account
  • The service handles scaling and updates
  • Priced by plan
  • Infrastructure settings are limited to what the service offers
The server container is the same in both options; the difference is who manages the infrastructure.

Google states the Cloud Run cost clearly in its own guide. Each server is a Cloud Run instance with 1 vCPU and 0.5 GB of memory on the CPU-always-allocated pricing model, and it costs roughly 45 US dollars per month.[1] Google recommends running at least 2 instances to reduce the risk of data loss in case of a server outage.[1]

Approximate monthly cost per server on Cloud Run
~45 USD
Recommended minimum number of server instances
2
Expected requests per second with 2-10 servers
35-350
Source: Google Tag Manager, Set up server-side tagging with Cloud Run

Google expects autoscaling between 2 and 10 servers to handle 35-350 requests per second. It also notes that this performance varies with the number of tags and what those tags do.[1] For teams working with a budget in Turkish lira, this cost depends on the exchange rate. That is why I recommend setting up a billing alert in Google Cloud from day one.

How do you connect a custom domain?

A custom domain is set up so the tagging server runs on the site’s own domain instead of the cloud provider’s default address. Google describes this setup as a best practice. A server running on the default address receives data but runs in a third-party context.[2]

Google’s custom domain guide compares three options for a site running on www.example.com:[2]

OptionExample URLServer-set cookie accessSetup
Same origin (best practice)www.example.com/metricsFullCDN or load balancer forwarding
Subdomainmetrics.example.comFullDNS record update
Default domainmetrics.run.appNone, JavaScript cookies onlyPre-configured

In practice, I start most setups with a subdomain, because it only requires adding a DNS record. A same-origin setup, on the other hand, needs path-based forwarding in a CDN such as Cloudflare or in a load balancer. If you point the subdomain to a third-party service with a CNAME, I explained how Safari’s ITP rules affect cookie lifetime in that case in the server-side tracking guide.

How does the web container send data to the server?

The web container sends data to the server once the server_container_url parameter is added to the configuration settings of the Google tag. The value of this parameter is the tagging server’s custom domain. After this setting, GA4 events go to your server first instead of straight to Google.

In the GTM interface, you add this setting as a parameter in the Configuration settings of the Google tag. On a site using gtag.js, the same setting looks like this:

// Let the Google tag send events to your own tagging server
gtag('config', 'G-1A2B3C4D5E', {
  server_container_url: 'https://metrics.yourdomain.com'
});

Google recommends using the Google Analytics tag on the web page to send data to the server container. This tag can use different transport methods, such as an image pixel, the Fetch API, XHR and a service worker inside an iframe loaded from the server domain.[3] If your site has a content security policy (CSP), you need to add the server address to the img-src, connect-src and frame-src directives.[3]

GA4, Google Ads and Meta tags

Once the GA4 client in the server container claims the incoming request, you set up the platform tags. My typical setup order is:

  • GA4: In the server container, the Google Analytics: GA4 tag forwards the events from the client to the GA4 property.
  • Google Ads: First the Conversion Linker tag, then Google Ads conversion tracking and, if needed, remarketing tags are set up.
  • Meta Conversions API: The Meta tag template from the Community Template Gallery is added. The Pixel ID and access token are defined. If the Pixel is also running in the browser, deduplication must use the same event name and event_id.

If you send additional parameters, Google notes that some server tags may add them to outgoing requests on their own. If you do not want a parameter to reach a platform, you can remove it with a Transformation rule in the server container.[3]

How do you test the setup?

A server-side GTM setup is tested on three layers: that the server is up, that requests are processed correctly in the server container and that events reach the platforms correctly. If one of these layers is skipped, the setup may look like it works while data goes missing or is sent twice.

Google defines two basic checks.[1] The first is clicking the Preview button in the GTM workspace; if the preview page loads, the setup is configured correctly. The second is appending /healthy to the server address; if the service is working, the page shows the text ok.[1]

Google also recommends checking whether an event is actually being triggered when requests for a specific product are missing. The config command initializes the product, but data is usually only sent when an event is called; GA4 is the exception and sends page_view automatically.[1]

Server-side GTM pre-launch checklist0/9

What are the most common server-side GTM setup mistakes?

The most common mistakes in a server-side GTM setup are staying on the default address, scaling the preview server, leaving request logging on and letting costs grow, and not updating the tagging server version. Most of these mistakes are noticed not on day one but a few weeks later.

  • Staying on the default run.app address: The server receives data, but you cannot benefit from server-set cookies.[2]
  • Scaling the preview server: Google recommends exactly one preview server.[4] So fix the maximum number of instances at 1 in autoscaling.
  • Forgetting request logs: By default, the path and query parameters of every request are logged. Google notes that on servers handling more than 1 million requests a month these logs may incur significant charges, and recommends disabling request logging.[1]
  • Not updating the version: New tagging server versions contain security fixes. Google recommends updating at least for each major version when Tag Manager notifies you.[1]
  • Sending the event twice: If a platform tag sends the same event from both the web container and the server container, the conversion is counted twice. Do not forget to turn off the tag you moved on the web side.

Where should you start?

A server-side GTM setup is infrastructure made up of a server container, a tagging server on something like Cloud Run, a single preview server and the site’s own domain. The setup itself is quick. A reliable result, however, depends on the custom domain, the consent setup, moving tags correctly and testing on three layers.

My recommendation is to settle the event map and the domain decision first, then do the setup on Cloud Run or a managed service. In the first phase, move only GA4, then add the Google Ads and Meta tags. I describe how I build measurement infrastructure together with ad management on the performance marketing page. The same infrastructure is what connects ad clicks to the patient journey in health tourism marketing. You can find my background on the software side on the about page.

Key terms

Server container
Server container is the Google Tag Manager container type that runs on a server instead of in the browser and processes incoming measurement requests with clients, tags and triggers. It is hosted on a tagging server and is managed in the familiar GTM interface.
Preview server
Preview server is the separate server in a server-side Google Tag Manager setup that only runs Preview and debugging sessions. Google recommends deploying exactly one preview server and not scaling it beyond a single instance.
sGTM client
sGTM client is the adapter that claims an HTTP request arriving at the server container, turns it into one or more events and packages the result back to the device. The GA4 client comes pre-installed in every server container.
Container config
Container config is the configuration string that identifies a Google Tag Manager server container and is passed to the tagging server and preview server as an environment variable. In a manual setup, the same CONTAINER_CONFIG value is set on every server instance.

Frequently asked questions

Is a server-side GTM setup paid?

Google Tag Manager itself is free, but the infrastructure that runs the server container is not. According to Google's Cloud Run guide, each server in the recommended configuration costs roughly 45 US dollars per month. Managed services such as Stape are priced by their own plans, and cost grows with traffic.

Is a custom domain required for server-side GTM?

Technically, no; the server container still receives data on the default address provided by the cloud provider. However, that address runs in a third-party context and cannot benefit from server-set cookies. Google recommends serving it from the same origin as your site or from a subdomain.

Is the web container removed after setting up server-side GTM?

No. The web container remains the layer that starts the event in the browser. What changes is that the Google tag sends events to your tagging server address instead of straight to Google. Most platform tags are later moved into the server container.

Should you choose Cloud Run or Stape?

If you have a technical team that can manage its own Google Cloud account, Cloud Run gives you full control. If nobody on the team manages cloud infrastructure, a managed service such as Stape reduces the setup and maintenance load. With either option, the custom domain, preview and consent setup need the same care.

How do you know a server-side GTM setup is working?

The first check is that the page shows the text ok when you append /healthy to the server address. Next, you open Preview mode in the server container and inspect incoming requests and fired tags one by one. Finally, you compare the events in the GA4, Google Ads and Meta tools.

Sources

  1. Google Tag Manager: Set up server-side tagging with Cloud Run
  2. Google Tag Manager: Custom domain configuration
  3. Google Tag Manager: Send data to server-side Tag Manager
  4. Google Tag Manager: Manual setup guide