A new way to set up a custom domain in server GTM

Jun 18, 2024
Dec 26, 2023

When setting up server-side tagging, the best practice was configuring a custom domain for your server Google Tag Manager environment. This approach primarily allows setting up first-party cookies, which have a longer lifespan than third-party cookies, enhancing user tracking and data collection consistency.

With the new restrictions coming from Safari, more than just setting up a custom domain is required to prolong cookie lifetime. Even if you map a custom domain, cookies in Safari will be shortened. To make it last longer, you must ensure the custom domain has the same IP address as your website’s primary domain by configuring Own CDN, or use stape Cookie Keeper power-up to store and restore cookies

However, Google has recently changed the preferred way of mapping a custom domain for the server Google Tag Manager container. Instead of using a subdomain (like they encourage using the same origin (like 

With the same origin custom domain for sGTM, you will have the main benefit of server-side tagging - setting up first-party cookies. And with the tagging server using the same origin domain, you do not need to configure any additional settings to prolong cookies. However, mapping the same origin will be more complicated than configuring a subdomain. 

I will show two examples of configuring the same origin custom domain for sGTM using Cloudflare or nginx.

These are prerequisites to configure the same origin custom domain

  1. Server Google Tag Manager container set up. 
  2. A CDN or load balancer that can forward requests.
  3. If you use App Engine, the tagging server must run version 2.2.0 or later.
  4. Select the path for your sGTM custom domain, for example,,, etc. 

How to configure the same origin custom domain in sGTM

1. Cloudflare

For this option to work all your site traffic must be proxied through CloudFlare. Thus, the functionality of CF Workers will allow you to proxy sGTM requests as well.

a. Create a worker in Cloudflare.

worker in cloudflare - stape

b. Add worker name and click Deploy.

add worker name in cloudflare - stape

c. In the worker settings, click 'Edit code’ and create js similar to the screenshot below, where:

/metrics/ - is a path you choose for your server container. - is your tagging server URL. There are two scenarios:

  • [Not recommended] The default tagging server URL that was provided to you by stape. In this case, the tagging server URL will look like
  • [Recommended] The custom subdomain you’ve set up inside the admin. Using a custom subdomain when configuring a worker is recommended since it gives two benefits: loading gtm.js and gtag.js from a custom path using Custom Loader power-up, which makes tracking scripts unblockable and allows the setting of long-lived first-party cookies. If you use a custom subdomain for your same origin tagging server URL, ensure you've added a custom domain to your sGTM container on stape and created 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 = ''; let newRequest = new Request((`https://` + domain + pathname + search), request); newRequest.headers.set('Host', domain); return fetch(newRequest); }, };
same origin worker

Deploy and save changes.

d. Go to your sGTM worker -> Settings -> Triggers and create a new Route. Add the URL you use for server GTM ending with *. In my case, it’s* and select your domain in zone selection 

same origin worker

2. nginx

If you use Ngnix you can configure all the same things quite easily with it and do without using Cloudflare.

Add to your server config, where:

/metrics - path you selected for server GTM. - custom domain of your sGTM. 

location = /metrics { return 302 /metrics/; } location ~ ^/metrics(.*) { resolver valid=3600s; proxy_pass$1$is_args$args; proxy_set_header Host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

Host your GTM server at Stape