From 386580b0ee64304e142ab3b7d55b93268cdab35d Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Wed, 3 Sep 2025 13:13:35 +0530 Subject: [PATCH] fix: posthog use reverse proxy and reduce tracking information --- apps/webapp/app/hooks/usePostHog.ts | 3 +- apps/webapp/app/routes/.$.tsx | 38 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 apps/webapp/app/routes/.$.tsx diff --git a/apps/webapp/app/hooks/usePostHog.ts b/apps/webapp/app/hooks/usePostHog.ts index b09979d..c7e5cfb 100644 --- a/apps/webapp/app/hooks/usePostHog.ts +++ b/apps/webapp/app/hooks/usePostHog.ts @@ -19,7 +19,8 @@ export const usePostHog = ( if (postHogInitialized.current === true) return; if (logging) console.log("Initializing PostHog"); posthog.init(apiKey, { - api_host: "https://us.i.posthog.com", + api_host: "/relay-Xm1a", + ui_host: "https://us.posthog.com", opt_in_site_apps: true, debug, loaded: function (posthog) { diff --git a/apps/webapp/app/routes/.$.tsx b/apps/webapp/app/routes/.$.tsx new file mode 100644 index 0000000..25023f0 --- /dev/null +++ b/apps/webapp/app/routes/.$.tsx @@ -0,0 +1,38 @@ +import type { ActionFunction, LoaderFunction } from "@remix-run/node"; + +const API_HOST = "us.i.posthog.com"; +const ASSET_HOST = "us-assets.i.posthog.com"; + +const posthogProxy = async (request: Request) => { + const url = new URL(request.url); + const hostname = url.pathname.startsWith("/relay-Xm1a/static/") + ? ASSET_HOST + : API_HOST; + + const newUrl = new URL(url); + newUrl.protocol = "https"; + newUrl.hostname = hostname; + newUrl.port = "443"; + newUrl.pathname = newUrl.pathname.replace(/^\/relay-Xm1a/, ""); + + const headers = new Headers(request.headers); + headers.set("host", hostname); + + const response = await fetch(newUrl, { + method: request.method, + headers, + body: request.body, + }); + + return new Response(response.body, { + status: response.status, + statusText: response.statusText, + headers: response.headers, + }); +}; + +export const loader: LoaderFunction = async ({ request }) => + posthogProxy(request); + +export const action: ActionFunction = async ({ request }) => + posthogProxy(request);