Skip to content

PostHog Plugin

@theholocron/holocron-plugin-posthog analytics

npm · GitHub

Install

pnpm add -D @theholocron/holocron-plugin-posthog

Capabilities

CapabilityToken
analyticsHOLOCRON_POSTHOG_TOKEN (posthog)
providers: {
analytics: "posthog",
}

Or with options:

providers: {
analytics: ["posthog", {
// EU cloud or self-hosted instance
host: "https://eu.posthog.com",
}],
}
Option Required Description
host No PostHog instance URL. Defaults to https://app.posthog.com (US cloud). Use https://eu.posthog.com for EU cloud.

The plugin uses a personal API key — not the project API key. The personal key (phx_*) is org-scoped and can create projects; the project key (phc_*) is the runtime tracking token your app embeds.

  1. Go to app.posthog.com/settings/user/api-keys
  2. Click Create personal API key, give it a label (e.g. “Holocron”), and copy it
Terminal window
holocron auth set posthog phx_xxxxxxxxxxxxxxxxxxxx

Or via env var:

Terminal window
export HOLOCRON_POSTHOG_TOKEN=phx_...
# Also recognized:
export POSTHOG_PERSONAL_API_KEY=phx_...
  • describe() — returns the provider name and required env var keys (NEXT_PUBLIC_POSTHOG_KEY, NEXT_PUBLIC_POSTHOG_HOST)
  • whoami() — verifies the personal API key by fetching the current user and org
  • ensureProject(name) — find or create a PostHog project by name, returning its tracking token (phc_*) and whether it already existed
import { createPlugin } from "@theholocron/holocron-plugin-posthog";
const plugin = createPlugin({ cliToken: process.env.HOLOCRON_POSTHOG_TOKEN });
const an = plugin.capabilities.analytics();
const { token, alreadyExists } = await an.ensureProject("my-app");
// Push NEXT_PUBLIC_POSTHOG_KEY=token and NEXT_PUBLIC_POSTHOG_HOST=https://app.posthog.com
// to GitHub Secrets via the secrets capability.
console.log(`PostHog key: ${token} (${alreadyExists ? "existing" : "created"})`);