posthog-js

The official browser SDK for PostHog — autocapture, session replay, feature flags, and surveys in one drop-in script.

SDK
npm
v1.434.15
614 stars
Apache-2.0 AND MIT

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
93 /100 Excellent
Development Activity 100
Maintenance 100
Community 84
Maturity 60
Momentum 28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
86 /100 Excellent
Architecture 87
Code Quality 88
Innovation 84
Learning Curve 85

posthog-js is PostHog’s official JavaScript SDK for the browser, giving web apps event tracking, autocapture, session recording, feature flags, A/B testing, and in-app surveys through a single client. It ships as the flagship package in the PostHog JS monorepo, which also publishes posthog-node, posthog-react-native, posthog-js-lite, and framework-specific wrappers like @posthog/react and @posthog/next — but posthog-js is the canonical entry point most sites install first.

The SDK is built to work with zero backend code: point it at a PostHog project API key and it starts capturing pageviews, clicks, and form submissions automatically, while exposing a small imperative API (posthog.capture, posthog.identify, posthog.isFeatureEnabled) for custom events and flag checks. Its extension-bundle architecture lazy-loads heavier features like session replay and surveys only when enabled, keeping the base bundle lean for sites that just want analytics.

What You Get

  • Autocapture of pageviews, clicks, form submissions, and rageclicks with no manual instrumentation
  • Session replay recording via an rrweb-based extension, lazy-loaded only when enabled
  • Feature flags and A/B/multivariate experiments evaluated client-side with isFeatureEnabled and getFeatureFlag
  • In-app surveys and product tours rendered directly by the SDK
  • Exception autocapture that reports uncaught errors and unhandled promise rejections as PostHog events
  • Cookieless tracking mode and a consent manager for GDPR/CCPA-compliant deployments
  • Web vitals capture with attribution for Core Web Vitals monitoring
  • Pluggable integrations for Segment, Sentry, and Crisp/Intercom chat widgets

Common Use Cases

  • Instrumenting a SaaS web app for product analytics without hand-writing every tracking call
  • Running feature flags and experiments directly in the browser to gate UI variants
  • Capturing session replays to debug user-reported bugs or watch real usage sessions
  • Sending front-end exceptions and unhandled promise rejections to PostHog alongside product events
  • Collecting in-app survey responses and NPS scores tied to identified users

Under The Hood

Architecture The SDK centers on PostHog, the main class defined in posthog-core.ts, which wires together a ConsentManager (consent.ts), PostHogPersistence (posthog-persistence.ts/storage.ts) for storing distinct IDs and properties, a RequestQueue/RetryQueue pair (request-queue.ts, retry-queue.ts) that batches and retries event delivery over request.ts, and a RemoteConfigLoader (remote-config.ts) that fetches project-level flag/feature configuration on init. Heavier capabilities — session replay, surveys, product tours, exception autocapture, web vitals — live under src/extensions/ and are pulled in only when enabled, via a family of alternate entrypoints in src/entrypoints/ (module.es.ts, module.full.es.ts, array.full.ts, etc.) that Rollup compiles into differently-sized bundles depending on which extensions a consumer opts into.

Tech Stack Written in TypeScript (96% of the monorepo) with strict mode enabled, targeting ES5 for broad browser compatibility. The package is built with tsc + Rollup (rollup.config.mjs) inside a pnpm/Turborepo monorepo (turbo.json, pnpm-workspace.yaml) that also builds sibling packages like posthog-node and posthog-react-native from a shared @posthog/core. Session replay builds on rrweb, and the repo ships its own packages/rrweb fork/wrapper alongside the browser SDK.

Code Quality The browser package alone carries 122 test files (Jest for unit tests under src/__tests__, Playwright for browser/e2e coverage), and tsconfig.json enforces strict, noImplicitReturns, and noUnusedParameters. The codebase is cleanly partitioned by concern (core state in top-level src/*.ts, feature extensions under src/extensions/, bundle variants under src/entrypoints/), and CI-aligned checks (build, lint, typecheck, test) are documented in CONTRIBUTING.md as required before opening a PR, backed by Husky pre-commit hooks.

API Design Onboarding is a single posthog.init(apiKey, options) call, after which autocapture starts with zero further code; the imperative surface for custom needs is intentionally small and consistent (capture, identify, isFeatureEnabled, getFeatureFlag, onFeatureFlags). API reference docs are generated straight from the TypeScript source via @microsoft/api-extractor (see the generate-references script), keeping documented signatures in sync with the shipped types, and framework-specific ergonomics are layered on top through separate packages (@posthog/react, @posthog/next, @posthog/nuxt) rather than bloating the core SDK.

Used by 199 apps in this directory

TypeScript
100%
Other

Activepieces

AI Assistants · Automation · Mcp

24,756

Open-source AI automation platform that converts 280+ workflow integrations into MCP servers for LLMs, with no-code builders and TypeScript extensibility.

View details
92
Repo Health
85
Technical
64
Dependency
Built with
TypeScript 100%
Updated 4 days ago
TypeScript
100%
Other

Activepieces

AI Assistants · Automation · Mcp

24,756

Open-source AI automation platform that converts 280+ workflow integrations into MCP servers for LLMs, with no-code builders and TypeScript extensibility.

View details
92
Repo Health
85
Technical
64
Dependency
Built with
TypeScript 100%
Updated 4 days ago
Go
59%
Apache 2.0

agent-orchestrator

AI Agents · AI Code Assistants · Developer Tools

12,440

A local desktop workspace that gives every coding task its own agent, Git branch, and worktree, then tracks tasks, pull requests, CI, and reviews for 27 coding agents on one live Kanban board.

View details
86
Repo Health
83
Technical
68
Dependency
Built with
Go 59%
TypeScript 38%
Updated 4 days ago
Go
59%
Apache 2.0

agent-orchestrator

AI Agents · AI Code Assistants · Developer Tools

12,440

A local desktop workspace that gives every coding task its own agent, Git branch, and worktree, then tracks tasks, pull requests, CI, and reviews for 27 coding agents on one live Kanban board.

View details
86
Repo Health
83
Technical
68
Dependency
Built with
Go 59%
TypeScript 38%
Updated 4 days ago
TypeScript
56%
Other

agenta

AI Development · Developer Tools · Devops

4,788

The open-source LLMOps platform unifying prompt engineering, evaluation, and observability for teams building reliable LLM applications.

View details
91
Repo Health
77
Technical
67
Dependency
Built with
TypeScript 56%
Python 39%
Updated 5 days ago
TypeScript
72%
AGPL 3.0

APITable

Databases · Low Code Platforms

15,623

API-first collaborative spreadsheet-database platform that auto-generates REST APIs and lets teams build internal tools, CRMs, and dashboards without code.

View details
71
Repo Health
77
Technical
60
Dependency
Built with
TypeScript 72%
Java 22%
Updated 3 weeks ago
TypeScript
89%
MIT

Amical

AI Assistants · Note Taking · Voice AI

1,540

Local-first AI dictation that understands your active app — private, offline, and built for speed.

View details
82
Repo Health
82
Technical
68
Dependency
Built with
TypeScript 89%
Updated 1 weeks ago
TypeScript
89%
MIT

Amical

AI Assistants · Note Taking · Voice AI

1,540

Local-first AI dictation that understands your active app — private, offline, and built for speed.

View details
82
Repo Health
82
Technical
68
Dependency
Built with
TypeScript 89%
Updated 1 weeks ago
TypeScript
54%
MIT

Anarlog

AI Assistants · Note Taking · Productivity

9,404

Anarlog is an open-source, local-first AI meeting notetaker that records, transcribes, and summarizes meetings entirely on your device — no cloud lock-in, no mandatory account, and every note saved as a plain markdown file you own forever.

View details
86
Repo Health
77
Technical
68
Dependency
Built with
TypeScript 54%
Rust 34%
Updated 4 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers