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 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.
I will show two examples of configuring the same origin custom domain for sGTM using Cloudflare or nginx.
a. Create a worker in Cloudflare.
b. Add worker name.
c. 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:
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);
},
};
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.
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;
}
We've got you covered! Click Get help, fill-up the form, and we will send you a quote.
With the latest update of Safari, Google Tag Manager, Google Analytics, and Google Ads scripts will be blocked in Incognito mode and when Advanced tracking protection is on, but server-side tracking is a solution.
Nov 30, 2023In this guide find out how to set up WhatsApp conversion API and what is needed for the successful implementation.