Skip to content

Commit aa0771e

Browse files
committed
feat(webapp): make the PostHog host configurable for self-hosters
Add POSTHOG_HOST (default https://eu.posthog.com), used for the browser toolbar ui_host (threaded through the root loader) and the posthog-node server client. Self-hosters on a US project can now point analytics at us.posthog.com; the EU default keeps existing behaviour unchanged.
1 parent 52e768b commit aa0771e

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

apps/webapp/app/env.server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ const EnvironmentSchema = z
201201
// fans out to the ingest vs assets host by path.
202202
POSTHOG_INGEST_HOST: z.string().default("eu.i.posthog.com"),
203203
POSTHOG_ASSETS_HOST: z.string().default("eu-assets.i.posthog.com"),
204+
// PostHog app host, used for the browser toolbar (ui_host) and the server
205+
// client. Set to https://us.posthog.com for a US project (also switch the
206+
// ingest/assets hosts to their us.i / us-assets equivalents).
207+
POSTHOG_HOST: z.string().default("https://eu.posthog.com"),
204208
TRIGGER_TELEMETRY_DISABLED: z.string().optional(),
205209
AUTH_GITHUB_CLIENT_ID: z.string().optional(),
206210
AUTH_GITHUB_CLIENT_SECRET: z.string().optional(),

apps/webapp/app/hooks/usePostHog.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { useOrganizationChanged } from "./useOrganizations";
55
import { useOptionalUser, useUserChanged } from "./useUser";
66
import { useProjectChanged } from "./useProject";
77

8-
export const usePostHog = (apiKey?: string, logging = false, debug = false): void => {
8+
export const usePostHog = (
9+
apiKey?: string,
10+
uiHost?: string,
11+
logging = false,
12+
debug = false
13+
): void => {
914
const postHogInitialized = useRef(false);
1015
const location = useLocation();
1116
const user = useOptionalUser();
@@ -20,7 +25,7 @@ export const usePostHog = (apiKey?: string, logging = false, debug = false): voi
2025
// PostHog Cloud EU server-side.
2126
api_host: "/ph",
2227
// Point the toolbar at the real PostHog UI; without it, it falls back to /ph.
23-
ui_host: "https://eu.posthog.com",
28+
ui_host: uiHost,
2429
cross_subdomain_cookie: true,
2530
opt_in_site_apps: true,
2631
debug,
@@ -33,7 +38,7 @@ export const usePostHog = (apiKey?: string, logging = false, debug = false): voi
3338
},
3439
});
3540
postHogInitialized.current = true;
36-
}, [apiKey, logging, user]);
41+
}, [apiKey, uiHost, logging, user]);
3742

3843
useUserChanged((user) => {
3944
if (postHogInitialized.current === false) return;

apps/webapp/app/root.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
5151
const session = await getSession(request.headers.get("cookie"));
5252
const toastMessage = session.get("toastMessage") as ToastMessage;
5353
const posthogProjectKey = env.POSTHOG_PROJECT_KEY;
54+
const posthogUiHost = env.POSTHOG_HOST;
5455
const features = featuresForRequest(request);
5556
const timezone = await getTimezonePreference(request);
5657

@@ -68,6 +69,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
6869
user,
6970
toastMessage,
7071
posthogProjectKey,
72+
posthogUiHost,
7173
features,
7274
appEnv: env.APP_ENV,
7375
appOrigin: env.APP_ORIGIN,
@@ -116,8 +118,8 @@ export function ErrorBoundary() {
116118
}
117119

118120
export default function App() {
119-
const { posthogProjectKey, kapa: _kapa } = useTypedLoaderData<typeof loader>();
120-
usePostHog(posthogProjectKey);
121+
const { posthogProjectKey, posthogUiHost, kapa: _kapa } = useTypedLoaderData<typeof loader>();
122+
usePostHog(posthogProjectKey, posthogUiHost);
121123

122124
return (
123125
<>

apps/webapp/app/services/telemetry.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Telemetry {
2222
}
2323

2424
if (postHogApiKey) {
25-
this.#posthogClient = new PostHog(postHogApiKey, { host: "https://eu.posthog.com" });
25+
this.#posthogClient = new PostHog(postHogApiKey, { host: env.POSTHOG_HOST });
2626
} else {
2727
console.log("No PostHog API key, so analytics won't track");
2828
}

0 commit comments

Comments
 (0)