Stape

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

Updated
Apr 18, 2024
Published
Oct 6, 2020
Also available in

In this article, I 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 sGTMCopy link to this section

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 start phasing out of third-party cookies in 2024, now is the time to begin using first-party cookies instead of third parties. 

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. 

CharacteristicsSame originSubdomainDefault domain
Example URLexample.com/sgtmsgtm.example.comwjzsdfvom.us.stape.io
Stability of cookiesIt gives the whole benefit of prolonging cookies lifetime without additional configuration. It gives the ability to set first-party cookies, though to increase cookie lifetime in Safari and other browsers with ITP, you need to configure CDN or use Cookie Keeper. It can only set Javascript cookies.
ComplexityComplicated. Configure a CDN or load balancer to forward requests. May need to update DNS entries.Easy. Update DNS entries.It is pre-configured for you when you set up the stape.io account.

How to add a custom domain inside the server Google Tag Manager containerCopy link to this section

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 subdomainCopy link to this section

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

using subdomain in stape.io

2. Select if you want to use CDN and what type. 

  • Disable CDN - choose not to use any type of CDN.
  • Stape Global CDN - loads scripts from a server closer to the user visiting your website.
  • Own CDN - helps to tag server URLs from the same IP as your website. It gives the ability to set cookies that Safari can not shorten.
enable cdn in stape.io

Adding custom subdomain with A and AAAA records:

You must set A and AAAA records if you do not use Global CDN.

You must set A, AAAA and CNAME records, if you use Global CDN.

You must set A and AAAA records if you do not use Global CDN.
You must set A, AAAA and CNAME records, if you use Global CDN.

Adding custom subdomain with a CNAME record:

This can be useful when DNS providers restrict adding duplicate records. A common situation is with domains hosted on Shopify or Wix.

Let's say your tagging server URL is sgtm.example.com. You should create A and AAAA DNS records to verify the domain on Stape. If Shopify manages your domain, creating both A and AAAA records for the same subdomain is impossible. Thus, you can't verify a custom domain on Stape.

To handle this issue, enable the checkbox Use CNAME record instead of A records, and instead of two A records, you will be asked to add one CNAME record to verify the domain.

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

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

You must set CNAME record if you do not use Global CDN.
You must set two CNAME records, if you use Global CDN.

3. Navigate to your DNS provider; for instance:

  • GoDaddy
  • Google Domains
  • Hostgator
  • Hover
  • Namecheap
  • Squarespace
  • AWS
  • Cloudflare

4. The below example will show how to add DNS setting in Cloudflare. Login to your account -> click on your site’s domain -> Click DNS -> Add record

set DNS record custom subdomain server google tag manager

5. Add these settings (FOR EXAMPLE):

Type: A

Name: ss (or any other subdomain you prefer)

IPv4 address: IP address depends on the location of the servers. You can find the custom domain IP address in your stape.io account. 

TTL: Auto

Proxy status: disable

Your settings should look like this:

set DNS record custom subdomain sGTM
!

CDN

Remember to add a CNAME record if you enabled CDN for your container. You will find the required CNAME records in the stape.io container settings.

6. Once you have done, click Save.

7. Once DNS records are created, it might take up to 72 hours to validate the custom domain inside the stape admin. Once a domain is validated, it will have status ready. If it’s been more than 72 hours and the domain is still not verified, follow this guide on troubleshooting custom domain verification.

validate the custom domain inside the stape admin

 8. Go back to the Google Tag Manager Server container -> Choose your server container -> Admin -> container settings -> paste tagging server URL.

set tagging server url in gtm

Updating the Web GTM script on your website is highly recommended if you use a custom domain. This tweak will make gtm.js load from your domain. To do so, replace the default domain googletagmanager.com with the custom domain you set up in the previous step. 

update google tag manager script stape

9. Inside the Web container, create a new tag of the tag Type Google Tag. Add your Google Tag ID.

In the section Configuration settings add the following parameters:

  • Name: server_container_url
  • Value: Enter the URL

Add trigger to GA4 tag. Normally it should trigger on all page views. 

set up Google Analytics 4 server-side 

You can also create a Google tag: Configuration settings variable that will predefine Google Tag settings if you need to use multiple Google Tags on your website and do not want to add settings for each of the tags manually.

These parameters can be, for example, a setting that defines whether you want to send a page view event every time a Google Tag triggers, set UTM parameters, client ID, etc. There is a list of standard Google Tags configuration parameters.    

10. To set up the GA4 event, go to the tags section and create a new tag with the tag type Google Analytics: GA4 Event. Add your GA4 ID and the event name; there is a list of standard event names

You can create a Google Tag: Event Settings variable that will help to share event parameters across several GA4 event tags or Google Tags. It can be, for example, user or product properties, and there is also a list of standard parameters for event settings. 

Google analytics 4 events settings 
google tag event settings variable

11. Open Google Tag Manager Server container -> click Client -> create GA4 client and save it -> go to Tags and set up GA4 tag. Remember that GA4 automatically sends not only pageview but some other events. Inside Event Name choose variable {{Event Name}}. -> add trigger Client Name equals GA4.

set up ga4 tag in the server container 
set up ga4 client in the server container 

12. Open the Server container preview mode and check that you see GA4 requests. Publish updated inside server and web Google Tag Manager containers. 

test ga4 trigger in the server container 

That’s it! Now your GTM server container runs from your subdomain.

Using the same originCopy link to this section

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 Copy link to this section

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. nginxCopy link to this section

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; }

ConclusionCopy link to this section

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.

Got trouble with adding a custom domain?

All it takes is a few simple questions. Click Get help, fill up the form, and we will guide you.

Get help
Tagged with:gtm server

Relevant posts

Updated Oct 24, 2022

Third-party cookies are about to expire. Will server-side tracking help?

By the end of 2024, Chrome and Chrome-based browsers will be done with third-party cookies. Safari and Firefox already implemented Intelligent Tracking Algorithms that can block trackers. That leads to the next point: digital advertising methods that rely on third-party cookies to target consumers might become ineffective or even stop working altogether. This change in how advertisers track users will hurt many publishers and ad networks that rely on these third-party companies to display ads and collect data from site visitors to understand their audience. In this blog post, I will explain what a third-party cookie is, why it matters, and how server-side tracking can help businesses transit to the world without third-party cookies.

Updated Apr 11, 2024

How much does server GTM cost and why Stape is cheaper than Google Cloud’s App Engine

What cost can you expect for server Google Tag Manager hosted on Google Cloud? Is it a good idea to use a test environment on the productions site. Why stape’s hosting for server GTM is cheaper and how we secure our user’s data.

Updated Feb 23, 2024

Google tag manager server-side: How to set up server Tag Manager, Universal Analytics, GA4, and Facebook conversion API

Server-side tagging has been one of the main trends in web analytics for the last couple of years. Ad blockers, Intelligent Tracking Protection, 3rd party cookie restrictions, regulations like GDPR made analytics and advertising companies start worrying about how and what information they collect about site visitors. Server-side tagging allows moving third-party tags off your site and into a cloud server. In this case, third-party pixels are loaded directly from the could server rather than your site. In this article, I will explain and demonstrate the basics of setting up Google Tag Manager server container, server Universal Analytics, GA4, and Facebook Conversion API.

Host your GTM server at Stape