Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): use backend regions for network metric paths (#101440)

Paul D'Ambra278bee398b2b
diff --git a/products/desktop/docs/CONVENTIONS.md b/products/desktop/docs/CONVENTIONS.md
index 78956b14b517..c17663a06840 100644
--- a/products/desktop/docs/CONVENTIONS.md
+++ b/products/desktop/docs/CONVENTIONS.md
@@ -225,6 +225,14 @@ Main-process events use `trackAppEvent(eventName, properties)` from `apps/code/s
Both clients set `team: "posthog-code"` as a super-property.
+### Network metrics
+
+The network duration metric uses backend URLs from the shared region configuration, including a configured custom cloud.
+This works before login and after logout.
+The analytics ingestion host does not select the backend.
+External or invalid URLs use `path: "external"`.
+Skill names, skill file paths, and MCP tool names use `:id` placeholders; other backend paths use the SDK's default templates.
+
### Event Names
- Format: `Object verbed`.
diff --git a/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.test.ts b/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.test.ts
index 250fe003aacb..693c1217c2bc 100644
--- a/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.test.ts
+++ b/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.test.ts
@@ -222,32 +222,29 @@ describe("track", () => {
});
describe("networkMetricPath", () => {
- const apiHost = "https://internal-c.posthog.com";
-
- it("leaves the path undefined for the app's own API host", async () => {
+ it.each([
+ "https://us.posthog.com",
+ "https://eu.posthog.com",
+ "http://localhost:8010",
+ "https://app.dev.posthog.dev",
+ ])("keeps API paths for %s without authentication", async (origin) => {
const { networkMetricPath } = await loadAnalytics();
- const path = networkMetricPath(
- {
- url: "https://internal-c.posthog.com/api/projects/1/tasks/",
+ expect(
+ networkMetricPath({
+ url: `${origin}/api/projects/1/tasks/`,
method: "GET",
- },
- apiHost,
- );
-
- expect(path).toBeUndefined();
+ }),
+ ).toBeUndefined();
});
it("collapses the path for a presigned artifact URL on another host", async () => {
const { networkMetricPath } = await loadAnalytics();
- const path = networkMetricPath(
- {
- url: "https://s3.example.com/bucket/artifacts/ab12cd34_customer-roadmap.pdf?X-Amz-Signature=abc",
- method: "GET",
- },
- apiHost,
- );
+ const path = networkMetricPath({
+ url: "https://s3.example.com/bucket/artifacts/ab12cd34_customer-roadmap.pdf?X-Amz-Signature=abc",
+ method: "GET",
+ });
expect(path).toBe("external");
});
@@ -255,31 +252,96 @@ describe("networkMetricPath", () => {
it("collapses the path for an unparseable URL", async () => {
const { networkMetricPath } = await loadAnalytics();
- const path = networkMetricPath(
- { url: "not a url", method: "GET" },
- apiHost,
- );
+ const path = networkMetricPath({ url: "not a url", method: "GET" });
expect(path).toBe("external");
});
-});
-describe("metrics.network.attributes callback", () => {
- it("returns undefined for requests to the app's own API host", async () => {
- const { initializePostHog } = await loadAnalytics();
+ it.each([
+ "https://us.posthog.com.example.com/api/projects/1/tasks/",
+ "https://us.posthog.com:8443/api/projects/1/tasks/",
+ "https://internal-c.posthog.com/api/projects/1/tasks/",
+ "https://posthog.example.com/api/projects/1/tasks/",
+ ])("redacts an unknown backend: %s", async (url) => {
+ const { networkMetricPath } = await loadAnalytics();
- initializePostHog();
+ expect(networkMetricPath({ url, method: "GET" })).toBe("external");
+ });
- const attributesCallback =
- mockPosthog.init.mock.calls[0][1].metrics.network.attributes;
- const result = attributesCallback({
- url: "https://internal-c.posthog.com/api/projects/1/tasks/",
+ it("uses the existing custom cloud configuration", async () => {
+ const { networkMetricPath } = await loadAnalytics();
+ const { configureCustomCloud } = await import("@posthog/shared");
+ const request = {
+ url: "https://posthog.example.com/api/projects/1/tasks/",
method: "GET",
+ };
+
+ expect(networkMetricPath(request)).toBe("external");
+ configureCustomCloud({
+ url: "https://posthog.example.com",
+ oauthClientId: "test-client",
});
+ expect(networkMetricPath(request)).toBeUndefined();
+ configureCustomCloud(null);
+ expect(networkMetricPath(request)).toBe("external");
+ });
- expect(result).toBeUndefined();
+ it.each([
+ {
+ case: "a skill name",
+ url: "https://us.posthog.com/api/environments/1/llm_skills/name/incident-runbook",
+ expected: "/api/environments/:id/llm_skills/name/:id",
+ },
+ {
+ case: "a nested skill file path",
+ url: "https://us.posthog.com/api/environments/1/llm_skills/name/incident-runbook/files/docs/readme.md",
+ expected: "/api/environments/:id/llm_skills/name/:id/files/:id",
+ },
+ {
+ case: "an MCP tool name",
+ url: "https://us.posthog.com/api/environments/1/mcp_server_installations/0f8c2b1e-1111-4222-8333-444455556666/tools/lookup_customer_record/",
+ expected: "/api/environments/:id/mcp_server_installations/:id/tools/:id/",
+ },
+ ])("templates $case on the app's own backend", async ({ url, expected }) => {
+ const { networkMetricPath } = await loadAnalytics();
+
+ const path = networkMetricPath({ url, method: "GET" });
+
+ expect(path).toBe(expected);
});
+ it("leaves the fixed MCP tools refresh action untemplated", async () => {
+ const { networkMetricPath } = await loadAnalytics();
+
+ const path = networkMetricPath({
+ url: "https://us.posthog.com/api/environments/1/mcp_server_installations/0f8c2b1e-1111-4222-8333-444455556666/tools/refresh/",
+ method: "POST",
+ });
+
+ expect(path).toBeUndefined();
+ });
+});
+
+describe("metrics.network.attributes callback", () => {
+ it.each(["https://us.posthog.com", "https://eu.posthog.com"])(
+ "keeps API paths for %s with a separate analytics host",
+ async (origin) => {
+ vi.stubEnv("VITE_POSTHOG_API_HOST", "https://internal-c.posthog.com");
+ const { initializePostHog } = await loadAnalytics();
+
+ initializePostHog();
+ const attributesCallback =
+ mockPosthog.init.mock.calls[0][1].metrics.network.attributes;
+
+ expect(
+ attributesCallback({
+ url: `${origin}/api/projects/1/tasks/`,
+ method: "GET",
+ }),
+ ).toBeUndefined();
+ },
+ );
+
it("returns { path: 'external' } for requests to other hosts", async () => {
const { initializePostHog } = await loadAnalytics();
diff --git a/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.ts b/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.ts
index 9227936611a8..050287317f34 100644
--- a/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.ts
+++ b/products/desktop/packages/ui/src/shell/posthogAnalyticsImpl.ts
@@ -8,7 +8,12 @@ import type {
AnalyticsProperties,
IAnalytics,
} from "@posthog/platform/analytics";
-import type { Adapter, ModelAccess } from "@posthog/shared";
+import {
+ type Adapter,
+ CLOUD_REGIONS,
+ getCloudUrlFromRegion,
+ type ModelAccess,
+} from "@posthog/shared";
import {
type EventPropertyMap,
isInboxAnalyticsEvent,
@@ -111,6 +116,35 @@ let flagsUnavailable = false;
const SESSION_IDLE_TIMEOUT_SECONDS = 36_000;
+const OWN_BACKEND_FREE_TEXT_PATH_TEMPLATES = [
+ {
+ pattern:
+ /^(\/api\/environments\/)\d+(\/llm_skills\/name\/)[^/]+(\/files\/).+$/,
+ replacement: "$1:id$2:id$3:id",
+ },
+ {
+ pattern: /^(\/api\/environments\/)\d+(\/llm_skills\/name\/)[^/]+$/,
+ replacement: "$1:id$2:id",
+ },
+ {
+ // The tool name comes from the MCP server, so a custom server can put any
+ // text here. `tools/refresh/` is a fixed action rather than a tool name, so
+ // the lookahead keeps that route on its own path.
+ pattern:
+ /^(\/api\/environments\/)\d+(\/mcp_server_installations\/)[^/]+(\/tools\/)(?!refresh\/?$)[^/]+(\/?)$/,
+ replacement: "$1:id$2:id$3:id$4",
+ },
+] as const;
+
+function templateOwnApiPath(pathname: string): string | undefined {
+ for (const template of OWN_BACKEND_FREE_TEXT_PATH_TEMPLATES) {
+ if (template.pattern.test(pathname)) {
+ return pathname.replace(template.pattern, template.replacement);
+ }
+ }
+ return undefined;
+}
+
/**
* Path attribute for the automatic network-duration metric. posthog-js's default
* path templating only replaces numeric/uuid-like segments, so a presigned
@@ -118,17 +152,24 @@ const SESSION_IDLE_TIMEOUT_SECONDS = 36_000;
* user-controlled filename — see `_build_artifact_storage_path` in
* products/tasks/backend/facade/api.py) or any other non-API request would leak
* that filename into the shared Metrics project. Only requests to the app's own
- * API host get path-based attribution; everything else collapses to a fixed
+ * backend host get path-based attribution; everything else collapses to a fixed
* value.
+ *
+ * Backend URLs come from the region configuration. Analytics ingestion uses
+ * a separate host, so it cannot identify backend requests.
*/
export function networkMetricPath(
request: NetworkMetricsRequest,
- apiHost: string,
): string | undefined {
try {
- const requestHost = new URL(request.url).host;
- const appHost = new URL(apiHost).host;
- return requestHost === appHost ? undefined : "external";
+ const requestUrl = new URL(request.url);
+ const isBackend = CLOUD_REGIONS.some(
+ (region) => getCloudUrlFromRegion(region) === requestUrl.origin,
+ );
+ if (!isBackend) {
+ return "external";
+ }
+ return templateOwnApiPath(requestUrl.pathname);
} catch {
return "external";
}
@@ -165,10 +206,10 @@ export function initializePostHog(sessionId?: string) {
// keyed by method/host/path (posthog-js templates numeric and uuid-like
// path segments to `:id` before dimensioning). posthog-js's own capture/flags/session-recording
// requests are excluded automatically. `attributes` keeps path-based
- // attribution to this app's own API — see `networkMetricPath`.
+ // attribution to this app's own backend — see `networkMetricPath`.
network: {
attributes: (request) => {
- const path = networkMetricPath(request, apiHost);
+ const path = networkMetricPath(request);
return path === undefined ? undefined : { path };
},
},