PostHog JS

repository·main·Indexed 20 days ago

https://github.com/posthog/posthog-js

A 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.

Tokens
88.4K
Snippets
331
Records
481
Agent score
69%

What's inside posthog-js

  1. What is rrweb?

    main

    rrweb (record and replay the web) is a tool designed to record and replay user interactions on the web. It operates through two primary functions:

    1. Record: Captures all mutations occurring in the DOM.
    2. Replay: Replays those recorded mutations sequentially based on their corresponding timestamps to recreate the user's session.
  2. Overview of PostHog JS packages

    main
    PostHog 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.
  3. Available OpenAI + PostHog AI Example Scripts

    main

    The 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.
  4. Logging features for state management libraries

    main

    The 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 posthogReduxLogger middleware.
    • Kea: Implemented via posthogKeaLogger plugin, which provides a cleaner API for Kea users.
  5. PostHog Remix Integration Features

    main

    The 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.
  6. Features demonstrated in @posthog/convex

    main

    The @posthog/convex example 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 context and reactive methods.
      • Remote Evaluation: Using action context and per-call /flags requests.
      • Reactive Updates: The app demonstrates a live, reactive view of flag values for the current Distinct ID that updates when flags change in PostHog.
    • AI Generation Tracing: Capturing $ai_generation events using the @posthog/ai package. By default, it uses OpenTelemetry, but manual capture is also supported.