Stape
Search
Try for free

How to add a custom domain to the Google Tag Manager server container

Updated
Dec 31, 2024
Published
Oct 6, 2020
Also available in

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. 

Benefits of mapping a custom domain inside sGTM

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 server-side GTM.

Since Chrome is the last browser that supports third-party cookies and will phase out cookies in early 2025, now is the time to begin using first-party cookies instead of third-party. 

⚠️UPDATE: Google announced that it will no longer pursue its plans to phase out third-party cookies. Instead, the company will introduce a new solution: a one-time prompt that allows users to set their preferences, which will apply across all Google browsing experiences.

The stability and longevity of cookies set by sGTM depend on how a custom domain is implemented. There are three types of custom domain configurations within sGTM. 

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.

  • Same origin
  • Subdomain
  • Default domain
types of custom domain configuration

How to add a custom domain inside the server Google Tag Manager container

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:

  • Using a subdomain.
  • Using the same origin.

Using a subdomain

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

Using a subdomain

2. There are two ways of adding custom domain:

  • Automatically (via Entri)

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.

Automatically (via Entri)
  • Manually

After you add a custom domain 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 CDN option you choose.

  • Disable CDN - choose not to use any type of CDN.
  • Stape Global CDN - loads scripts from a server closer to a user visiting your website.
  • Own CDN - helps route sGTM custom domain and proxy sGTM traffic through the DNS provider of your website. In this case, the IP addresses of your website and the custom domain of the sGTM will match, and server-side cookies will be considered as first-party. As a result, it gives the ability to increase cookie duration.

Adding a custom domain with CNAME record.

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

CNAME record

You must create two CNAME records, if you use Global CDN.

two CNAME records

Below there’s a step-by-step guidance on how to create records using Cloudflare.

1. Log in to your domain name service and create a new DNS record for the subdomain you want to use. 

create a new DNS record

If you use Global CDN, set additional CNAME record

If you use Global CDN, set additional CNAME record

2. Once your custom domain is verified, you should see the status ready below the tagging server URL in the Stape admin. If there are any problems with verifying a custom domain, please follow this guide on troubleshooting a custom domain.

status ready below the tagging server URL in the stape admin

3. Go to your Google Tag Manager Server container -> Admin -> Container Settings -> Change Tagging server URL to your subdomain.

Change Tagging server URL to your subdomain.

Adding a custom domain 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.

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.

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.

DNS setup for CloudFlare. 

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

a record
aaaa record

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.

tagging server url

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.

update the script on the website

An even better solution would be to use a Custom Loader power-up.

Using the same origin

The prerequisites to configure the same origin custom domain:

  1. A CDN or load balancer that can forward requests.
  2. If you use App Engine, the tagging server must run version 2.2.0 or later.
  3. Select the path for your sGTM custom domain, for example, example.com/sgtm, example.com/data, example.com/metrics, etc. 

We will show you 2 examples of configuring the same origin custom domain for sGTM using Cloudflare or nginx. 

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.

add worker name in cloudflare - stape

c. Create js similar to the one below, where:

/metrics/ - is a path you choose for your server container.

https://sgtm.stape.video/ - 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 https://wapdsrl.ca.stape.io.
  • [Recommended] The custom subdomain you’ve set up inside the stape.io 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 = 'sgtm.stape.video'; let newRequest = new Request((`https://` + domain + pathname + search), request); newRequest.headers.set('Host', domain); return fetch(newRequest); }, };
same origin worker

d. Go to the Workers Route and create a new Route. Add the URL you use for server GTM ending with *. In my case, it’s stape.video/metrics* and select the worker you’ve created on the previous steps. 

manage worker

2. nginx

Add to your server config, where:

/metrics - path you selected for server GTM.

https://gtm.mysimple.name - custom domain of your sGTM. 

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

Add the static header:

    add_header X-From-Cdn “cf-stape”;

Here’s an example:

server {    listen 80;    server_name example.com;    # Add the static header    add_header X-From-Cdn "cf-stape";    location / {        root /var/www/html;        index index.html;    } }

Conclusion

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.

Try Stape for all things server-sideright now!