Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

1.2 KB
Raw Blame
// Browser PostHog traffic goes through our own domain so blanket ad-block
// lists for *.posthog.com don't drop consented visitors' events. Consent is
// unaffected: posthog-js sends nothing until analytics is accepted.
// Misspelt on purpose: blocklist rules match the literal word "posthog".
export const POSTHOG_PROXY_PATH = "/relay/pnosthog";

const POSTHOG_CLOUD_HOST = /^https:\/\/(us|eu)(?:\.i)?\.posthog\.com\/?$/i;

interface PostHogConfig {
  key?: string;
  host?: string;
}

// Only PostHog Cloud is proxied, in whichever region NEXT_PUBLIC_POSTHOG_HOST
// names. A self-hosted PostHog, or none at all, keeps talking to its own host.
export function getPostHogProxyTarget({ key, host }: PostHogConfig) {
  if (!key) return null;
  const region = host?.trim().match(POSTHOG_CLOUD_HOST)?.[1]?.toLowerCase();
  if (!region) return null;
  return {
    apiHost: `https://${region}.i.posthog.com`,
    assetsHost: `https://${region}-assets.i.posthog.com`,
    uiHost: `https://${region}.posthog.com`,
  };
}

export function getPostHogClientHosts({ key, host }: PostHogConfig) {
  const target = getPostHogProxyTarget({ key, host });
  if (!target) return { api_host: host };
  return { api_host: POSTHOG_PROXY_PATH, ui_host: target.uiHost };
}