/Documentation

How to use same origin through Vercel

Updated Aug 12, 2026

Create a Serverless Function

// api/metrics.js // The external URL you want to proxy to: const TARGET_URL = "https://sgtm.example.com"; module.exports = async (req, res) => { // Vercel populates x-forwarded-for with the real client IP (among others). // The first IP is typically the visitor’s IP. const forwardedFor = req.headers["x-forwarded-for"] || ""; const clientIp = forwardedFor.split(",")[0].trim() || "unknown"; // Build new headers for forwarding const newHeaders = { // Spread original request headers if you want ...req.headers, // Override or add your custom ones "X-Forwarded-For": clientIp, "X-From-Cdn": "cf-stape", "Host": "sgtm.example.com", "CF-Connecting-Ip": clientIp }; // Forward the request to the target // NOTE: If you need the original body, you can pass it along. // But in Node serverless on Vercel, `req` is a stream, so you’d typically // parse it first (e.g. using `req.body` if using Next.js API routes). const response = await fetch(TARGET_URL, { method: req.method, headers: newHeaders, // If you need the original POST body, see note below body: req }); // Relay the response back to the client const text = await response.text(); // Copy headers from the upstream response for (const [key, value] of response.headers.entries()) { res.setHeader(key, value); } res.status(response.status).send(text); };

Testing your Same Origin setup

Test GA4 tag
Adding the same-origin path to container settings
Preview opened at the new path
Incoming page_view request from your real domain

Was this article helpful?

Comments

Can’t find what you are looking for?