Google has changed the preferred way of mapping a custom domain for the server Google Tag Manager container. Instead of using a subdomain (like sgtm.example.com) they encourage using the same origin (like example.com/sgtm).
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.
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.
b. Add worker name and click Deploy.
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.
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);
},
};
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 stape.video/metrics* and select your domain in the zone selection
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.
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;
}
Comments
0 comments
Please sign in to leave a comment.