In this article, we will describe how to set up a custom domain within the server Google Tag Manager container. The main advantage of using a custom domain for sGTM is that it helps increase cookie lifetime by setting first-party cookies instead of third-party cookies.
The essential benefit of server-side tagging lies in setting first-party cookies, though this isn't enabled by default. To leverage this capability, it's necessary to implement a custom domain in your GTM server-side.
You can use a custom tagging server URL to set up first-party cookies. Custom tagging server URL or custom domain should share the same domain as your website. For example, if your website domain is example.com, then the tagging server URL should look like ss.example.com.
There are three ways to set up a custom domain:

| If you haven’t worked with custom domains in server-side tracking before, we have a blog article that explains different ways of setting up a custom domain: Server-Side GTM Domain Guide. |
First of all, you need to create and set up the Google Tag Manager server container.
If you don’t have a server container follow this instruction.
We will show two ways of mapping a custom domain for the server Google Tag Manager container:
Сhoose your container on Stape, scroll down, and click Add custom domain.

Add Subdomain name - it can be any name that works for you, just be sure to avoid using phrases specific to advertising or tracking: ad, gtm, stgm, tracking, analytics, metrics, stape, gtag, etc.
A Content Delivery Network (CDN) distributes your website's assets across a global network of servers to reduce latency and improve load times for users in different locations. It is useful if your audience is geographically distributed or if you want to improve cookie tracking and data accuracy.
By default, the CDN is disabled, which means JavaScript files (like gtm.js, gtag.js, and analytics.js) are served directly from the server location where your sGTM container is hosted. Effectively, it bypasses both Stape Global CDN and Own CDN options and gives you a direct, unproxied connection between your site and the sGTM container.
| Important: please consider your country's privacy regulations before setting up a CDN. |
To enable the CDN, click the Add CDN toggle and select one of the following options:


Next, in the How to connect section, there are two ways of connecting a custom domain to your container:

The easiest way to connect domains. Log in with your DNS provider details via Entri, and give us one-time permission to connect your domain. That's it, you can proceed directly to Step 5 to verify your domain; no manual configuration is required.

With this option, you have to add DNS records manually to your hosting provider to connect your subdomain to Stape.

After you add a custom subdomain to your container, you will see DNS records that should be created to verify the custom domain. The DNS record depends on the server location and the CDN option you choose. In the next step, we show how to add DNS records.
By default, subdomain verification is performed with CNAME records, but you can verify your subdomain with A and AAAA records if you need to (usually in cases when you need to avoid adding duplicated records).
We will cover both options below with a step-by-step guide on how to create records using Cloudflare. But you can use any other provider.
| Please note that A/AAAA records are incompatible with enabled CDN. |
Once you've entered the DNS records to your DNS provider, return to the Stape container page where you've been configuring the custom domain and click Verify.

Wait for Stape to finish verifying your custom domain (when the container status bar changes from Verifying to Ready). 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 you face any problems with domain verification, please follow our guide on troubleshooting a custom domain.

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

Update the script
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.
Alternatively, you can automate this process with the Custom Loader power-up. Instead of manually editing the GTM script, Custom Loader generates a ready-to-use code snippet that loads GTM and GA4 scripts directly from your custom domain. This saves your time, makes your tracking scripts more resilient to ad blockers, and assures all cookies are treated as first-party.

Prerequisites
Before you start, make sure you have the following in place:
Note:
Configure same origin domain step-by-step
Depending on your infrastructure, you can configure the same origin custom domain using different platforms and services. Here are the guides for each:
Note:
Below, we'll walk through the setup using Cloudflare as an example.
For this option to work, all your site traffic must be proxied through Cloudflare – the Cloudflare Workers functionality will then allow you to proxy sGTM requests as well. You should also use SSL/TLS in Full mode; otherwise, same-origin proxying may work via a 301 redirect, which is incorrect.
In the left sidebar, click Compute → Workers & Pages → click Create application → select the Start with Hello World! option.

Add a worker name → click Deploy.

Decide on the path for your sGTM custom domain – for example, example.com/sgtm, example.com/data, or example.com/metrics.
In the worker settings, click Edit code and paste in JS similar to the example below, where:
For the tagging server URL, you have two options:
https://wapdsrl.ca.stape.io).gtm.js and gtag.js from a custom path via the Custom Loader power-up, making tracking scripts unblockable, and it enables long-lived first-party cookies. If you use a custom subdomain as your same-origin tagging server URL, make sure you've added a custom domain to your sGTM container on Stape and created the required DNS records as described here. Do not use Own CDN with the same origin domain.
export default {
async fetch(request, env, ctx) {
let { pathname, search, host } = new URL(request.url);
pathname = pathname.replace('/metrics/', '/');
const domain = 'sst.stapedesk.com';
let newRequest = new Request((`https://` + domain + pathname + search), request);
newRequest.headers.set('Host', domain);
return fetch(newRequest);
},
};
Deploy and save your changes.

Go to your sGTM worker → click Settings → Domains & Routes → create a new Route. Add the URL you use for server GTM ending with * (in our example, it's stapedesk.com/metrics*) and select your domain in the zone selection.

Next to your domain name, click the three dots → Configure Rules.

Click Create rule → Configuration Rules:
/metrics (update this if you're using a different path).Click Deploy.

Go to Rules → Overview → Create Rule → Request Header Transform Rule:
/metrics (update this if you're using a different path).X-From-Cdn with the value cf-stape.Save the rule.

Once the same origin setup is done, we also recommend updating the Custom Loader power-up for ad blocker protection and implementing it on your site. Don’t forget to specify your Same Origin path in it.

For more information, see our guide on Custom Loader.
1. In your web GTM container, add Google Tag and specify your Measurement ID (any ID works for testing). Set the server_container_url to the same origin path you configured (in our example, it is https://stapedesk.com/metrics).

2. To streamline testing, click Admin → Container Settings → enter the path under Server container URLs.

3. For the testing, the same origin domain (as we show in the step below), you will also need to configure Client (that will transmit data from the web to the server container) and Google Analytics: GA4 tag in the server GTM.
As Client, we will use GA4.
To add GA4 Client in the server GTM, open the clients’ section → Create New client → Select client type Google Analytics: GA4 (Web) → Add client name and click Save.

To configure the GA4 tag, add a new tag with tag configuration “Google Analytics: GA4”, and just specify your Measurement ID in the tag.

And add the trigger for a tag. It should trigger when the client name receives the requests (in our case, the Client is GA4):

Click Preview in both web and server GTM containers.
If everything is set up correctly, the server container should open in preview at the new path you created. If, for some reason, the preview doesn't open at the new path, you can manually enter it in the preview window's URL.

Visit your website. In the server container preview, you should now see an incoming page_view request from your website's real domain.

Adding a custom domain to a Google Tag Manager (GTM) server container can significantly impact the effectiveness and flexibility of your server-side tracking. In this article, we have provided two methods of custom domain implementation.
A same-origin approach offers increased security and simpler cookie management, ideal for singular, focused domains. A subdomain setup provides flexibility and performance optimization, suitable for complex or multiple domain structures. Each has its trade-offs in complexity, performance, and management. Carefully consider your technical requirements, security needs, and tracking objectives to select the most appropriate configuration for your server-side tracking.
If you have any questions, visit our helpdesk or write a ticket. Our team of experts will be happy to help you with any questions regarding Stape’s hosting and server-side tracking.
Comments