How to use same origin through Netlify
Updated Jan 16, 2026
This instruction uses /metrics as an example of the same origin path. You can use any of your own.
You should also use the URL of your sGTM container instead of https://sgtm.example.com.
Create an Edge Function file
e.g. netlify/edge-functions/metrics.js
// netlify/functions/metrics-proxy.js
const TARGET_URL = "https://sgtm.example.com";
exports.handler = async (event, context) => {
// Netlify often includes the client’s IP in x-nf-client-connection-ip
const clientIp = event.headers["x-nf-client-connection-ip"] || "unknown";
// Build new headers object
const newHeaders = {
...event.headers,
"X-Forwarded-For": clientIp,
"X-From-Cdn": "cf-stape",
"CF-Connecting-Ip": clientIp,
"Host": "sgtm.example.com",
};
// Proxy the request to your target
const response = await fetch(TARGET_URL, {
method: event.httpMethod,
headers: newHeaders,
body: event.body, // pass the original request body
});
// Build the response for Netlify
const responseBody = await response.text();
return {
statusCode: response.status,
headers: Object.fromEntries(response.headers.entries()),
body: responseBody,
};
};If you are not using your own subdomain and are proxying requests to the standard Stape subdomain, you also need to add the X-Stape-Host header with the value of the host where the events are taking place (for instance, X-Stape-Host: www.example.com).
Deploy these changes to your Netlify site.
After following these steps, add your /path for the same origin to the Custom Loader's settings. Check the article on Same Origin Path to do this.
Testing your Same Origin setup
You can check that everything is configured correctly by sending a single test request using a GA4 tag.
1. Create a GA4 tag in your web GTM container.
Add your Measurement ID (any ID works for testing) and set the server_container_url to the same-origin path you configured (we'll use https://stape.work/metrics as an example).

2. (Optional) To streamline testing, add your same-origin path (in this example, it's https://stape.work/metrics) to your server GTM settings. Click Admin → Container Settings → enter the path under Server container URLs.

3. Open Preview mode in both your web GTM container and your server-side GTM container. If everything is set up correctly, the server container should open in preview at the new path you created.

4. Trigger a page view on your site. In the server container preview, you should now see an incoming page_view request from your website’s real domain.

Comments