mcpField
I
installing-posthog-in-a-codebase

installing-posthog-in-a-codebase

github.com/posthogDevelopment959 installsUpdated today
Repository

About installing-posthog-in-a-codebase

A skill for installing PostHog in a codebase the agent can inspect and edit. It uses framework-specific PostHog documentation to guide SDK selection, configuration, initialization, user identification, and verification.

Key features

  1. 01Detect the app framework from project files
  2. 02Consult framework-specific SDK documentation
  3. 03Configure tokens and hosts through environment variables
  4. 04Initialize SDKs and identify users
  5. 05Verify changes with project checks and event data

Use cases

  1. 01Add PostHog to a web application
  2. 02Set up client and server SDKs
  3. 03Check event delivery after installation

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/posthog/ai-plugin --skill installing-posthog-in-a-codebase

After installing, type/installing-posthog-in-a-codebasein Claude Code to confirm it loaded.

Details

From the project README

Installing PostHog in a codebase

Use this skill when you can read and edit the user's code. If you can't access their code, as is common in ChatGPT on the web or Claude on the web, use installing-posthog-from-chat.

PostHog's docs are the source of truth for each SDK's setup. Init options, file locations and recommended settings change between SDK releases, so read the docs page for the framework before you write code, and follow it rather than what you remember.

1. Find the framework and read its docs

Detect the framework from the manifest files (package.json, pyproject.toml, requirements.txt, Gemfile, composer.json, go.mod, and so on) and the app's entry points. A web app with its own backend usually needs a client SDK and a server SDK, so read both pages.

FrameworkDocs page
JavaScript web (any framework)https://posthog.com/docs/libraries/js.md
React, including Vite appshttps://posthog.com/docs/libraries/react.md
Next.jshttps://posthog.com/docs/libraries/next-js.md
Vue.jshttps://posthog.com/docs/libraries/vue-js.md
Nuxthttps://posthog.com/docs/libraries/nuxt-js.md
Svelte and SvelteKithttps://posthog.com/docs/libraries/svelte.md
Angularhttps://posthog.com/docs/libraries/angular.md
Astrohttps://posthog.com/docs/libraries/astro.md
Remixhttps://posthog.com/docs/libraries/remix.md
React Routerhttps://posthog.com/docs/libraries/react-router.md
TanStack Starthttps://posthog.com/docs/libraries/tanstack-start.md
Node.js, including Expresshttps://posthog.com/docs/libraries/node.md
Python, including FastAPIhttps://posthog.com/docs/libraries/python.md
Djangohttps://posthog.com/docs/libraries/django.md
Flaskhttps://posthog.com/docs/libraries/flask.md
Rubyhttps://posthog.com/docs/libraries/ruby.md
Ruby on Railshttps://posthog.com/docs/libraries/ruby-on-rails.md
PHPhttps://posthog.com/docs/libraries/php.md
Laravelhttps://posthog.com/docs/libraries/laravel.md
Gohttps://posthog.com/docs/libraries/go.md
Javahttps://posthog.com/docs/libraries/java.md
.NEThttps://posthog.com/docs/libraries/dotnet.md
Elixirhttps://posthog.com/docs/libraries/elixir.md
Rusthttps://posthog.com/docs/libraries/rust.md
React Nativehttps://posthog.com/docs/libraries/react-native.md
Flutterhttps://posthog.com/docs/libraries/flutter.md
iOShttps://posthog.com/docs/libraries/ios.md
Androidhttps://posthog.com/docs/libraries/android.md

For a framework that isn't listed, call docs-search with the framework name.

2. Install the latest SDK

Add each package with the project's own package manager and no version, for example npm install posthog-js, pnpm add posthog-node, uv add posthog or bundle add posthog-ruby. That resolves the newest release. Don't type a version into the manifest from memory, because that pins an old release.

In a monorepo, install into the package that runs the app, not the workspace root.

3. Get the token and host

Call project-get without an ID. Use api_token as the project token and api_host as the SDK's host option.

  • Read both from environment variables in code, using the names the docs page uses (for example NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN and NEXT_PUBLIC_POSTHOG_HOST).
  • Write the real values to the env file the framework loads, such as .env.local or .env. If the project has an example env file, add the variable names there with empty values.
  • Never hard-code the token or the host in source files, and never use them as fallback defaults in code.

4. Initialize as the docs show

  • Initialize the SDK once, in the place the docs page names. Don't add a second initialization elsewhere.
  • For posthog-js, set defaults to the date the docs page shows. It selects the newest set of recommended settings, so don't take a date from memory.
  • For a backend SDK in a long-running server, create one client for the process, reuse it, and shut it down when the process exits. Don't create a client per request.
  • In serverless or edge handlers (for example Next.js route handlers on Vercel, or AWS Lambda), the runtime can freeze as soon as the response is sent, so queued events are lost. Flush before the handler returns: await flush() on a reused client, or use the platform's waitUntil, as the docs page describes.
  • For a server-rendered app (Django, Rails, Laravel and similar), add the server SDK as well as the browser snippet.

5. Identify users

If the app has login, call identify after login with a stable user ID from the auth system, such as the database ID, not an email address or display name. Call reset() on logout. Send server-side events with the same distinct ID, so client and server events land on the same person.

6. Verify

Run the project's build, type check or tests, and fix anything your change broke. If you can run the app, load a page and check that events arrive with read-data-schema or execute-sql. Tell the user which environment variables to add in their hosting provider before they deploy.

Comments

Markdown supported