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 server GTM container instead of https://sgtm.example.com.
Step 1. Deploy proxy code
Start by creating a Deno Deploy account if you don't have one.
There are a few ways to get code deployed on Deno Deploy. We'll cover two in this section:
- Using Deno Deploy's web interface.
- Using Deno's runtime CLI.
In both cases, the script that you'll need to deploy will look something like this:
const ORIGIN_PATH = "/metrics";
const GTM_URL = "https://sgtm.example.com";
Deno.serve(async (request, connInfo) => {
const url = new URL(request.url);
if (url.pathname.startsWith(ORIGIN_PATH)) {
const newHeaders = new Headers(request.headers);
newHeaders.set("X-Forwarded-For", connInfo.remoteAddr.hostname);
newHeaders.set("X-From-Cdn", "cf-stape");
newHeaders.set("CF-Connecting-Ip", connInfo.remoteAddr.hostname);
newHeaders.set("HOST", "sgtm.example.com");
return fetch(GTM_URL, {
method: request.method,
headers: newHeaders,
body: request.body,
});
}
return new Response(null, { status: 404 });
});
Both ORIGIN_PATH and GTM_URL can and should be customized. They should match the paths you'll end up using in the script tag you'll add to the site you want analytics on.
Using Deno Deploy's web interface
Log into Deno Deploy, navigate to the Projects Overview page, and click on the "New Playground" button:
This will deploy a new project and present an online text editor with the contents of your project's main.ts file. Paste the contents of the proxy script provided above and click on "Save & Deploy":
Using Deno's runtime CLI
If you have Deno runtime installed locally, initialize a new project with the init command:
deno init proxy-project-folder
Inside that folder, create a file named main.ts with the contents of the proxy script provided above.
To deploy your proxy script, run the following from the project folder:
deno deploy
Step 2. Test deployment script
To confirm the proxy is working, open its path in your browser. Go to https://sgtm.example.com/metrics. If you see error 400 there - then everything is working correctly. You can also run a preview of the server container on your /metrics path.
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.
Comments
0 comments
Please sign in to leave a comment.