PostHog JS
repository·main·Indexed 20 days ago
https://github.com/posthog/posthog-jsA monorepo containing a suite of JavaScript SDKs and integration packages for connecting web, mobile, and server-side environments to PostHog for analytics and feature management. Includes compliance adapters for browser and Node SDKs, as well as AI integration examples for providers such as Anthropic, AWS Bedrock, Azure OpenAI, Cerebras, Cloudflare AI Gateway, Cohere, Convex, Dedalus Labs, DeepSeek, Fireworks AI, and Google Gemini.
What's inside posthog-js
- @rrweb/types is a package containing the shared TypeScript definitions used across all rrweb packages. It ensures type safety and consistency for data structures used in recording and replaying web sessions.
Overview of the PostHog Browser SDK Compliance Adapter
mainThe Compliance Adapter is a specialized tool designed for theposthog-jsbrowser SDK. It integrates with the PostHog SDK Test Harness to ensure the SDK meets compliance standards. It utilizesjsdomto simulate a browser environment within Node.js, allowing the browser SDK to be tested in a server-side context.What is @rrweb/replay?
mainThe@rrweb/replaypackage contains the necessary code to replay recorded events. Currently, it serves as a wrapper around theReplayerclass from the mainrrwebpackage, with the intention of housing allReplayer-related code directly in this package in the future.Overview of rrdom-nodejs
mainrrdom-nodejsis a Node.js implementation of therrdomlibrary. It is designed to allow developers to replay and inspect recorded user interactions fromrrwebwithin a Node.js environment, rather than being restricted to a browser. For general information on howrrwebworks, refer to the mainrrwebguide.What is rrweb?
mainrrweb (record and replay the web) is a tool designed to record and replay user interactions on the web. It operates through two primary functions:
- Record: Captures all mutations occurring in the DOM.
- Replay: Replays those recorded mutations sequentially based on their corresponding timestamps to recreate the user's session.
Overview of PostHog JS packages
mainPostHog JS is a monorepo providing multiple specialized packages for integrating PostHog into various JavaScript environments. Depending on your runtime (Browser, Node.js, React, etc.), you should select the appropriate package from the repository to ensure compatibility and optimal performance.What is @rrweb/all?
mainThe
@rrweb/allpackage is a convenience bundle that includes the following core rrweb packages:rrweb: The main library@rrweb/record: For recording user sessions@rrweb/replay: For replaying recorded sessions@rrweb/packer: For compressing/unpacking data
Overview of @rrweb/utils
main@rrweb/utils is a package containing shared utility functions used across various
rrwebpackages. It serves as a foundational library for the rrweb ecosystem.For detailed information on how to use rrweb, refer to the main rrweb guide.
Available OpenAI + PostHog AI Example Scripts
mainThe following example scripts demonstrate how to track various OpenAI API calls using PostHog AI:
chat-completions.ts: Chat Completions API with tool calling.chat-completions-streaming.ts: Chat Completions with streaming support.responses.ts: Responses API with tool calling.responses-streaming.ts: Responses API with streaming support.embeddings.ts: Generating text embeddings.transcription.ts: Audio transcription using Whisper.image-generation.ts: Image generation via the Responses API.
Logging features for state management libraries
mainThe PostHog logging integrations for state management libraries support several key features to ensure efficient and secure telemetry:
- Action and state logging: Captures transitions and current state in PostHog.
- Rate limiting: Prevents log flooding by limiting the frequency of events.
- State diffing: Optimizes logs by only recording values that have changed.
- Action/state masking: Protects sensitive data by masking specific fields before they are sent to PostHog.
- Performance monitoring: Detects and logs slow actions to help identify bottlenecks.
The examples demonstrate two different implementation styles:
- Redux: Implemented via
posthogReduxLoggermiddleware. - Kea: Implemented via
posthogKeaLoggerplugin, which provides a cleaner API for Kea users.
PostHog Remix Integration Features
mainThe Remix playground demonstrates several key PostHog integration patterns:
- Automatic pageview tracking: Configured using
capture_pageview: 'history_change'to ensure pageviews are tracked during client-side navigation. - Custom event capture: Demonstrates how to use PostHog React hooks to capture specific user actions.
- PostHog React Provider: Shows how to wrap the application with the necessary provider for React-based tracking.
- SSR Support: Includes Vite configuration optimized for Server-Side Rendering.
- Session Replay Testing: Includes a media page with base64 images to test session replay capabilities.
- Automatic pageview tracking: Configured using
Features demonstrated in @posthog/convex
mainThe
@posthog/convexexample serves as a reference for integrating PostHog with Convex, covering the following capabilities:- Analytics: Capturing events (Sections 01–03) that appear in the PostHog activity feed.
- Feature Flag Evaluation:
- Local Evaluation: Using
query contextandreactivemethods. - Remote Evaluation: Using
action contextand per-call/flagsrequests. - Reactive Updates: The app demonstrates a live, reactive view of flag values for the current Distinct ID that updates when flags change in PostHog.
- Local Evaluation: Using
- AI Generation Tracing: Capturing
$ai_generationevents using the@posthog/aipackage. By default, it uses OpenTelemetry, but manual capture is also supported.