# posthog/posthog-js architecture

> Architecture diagram of the GitHub repository posthog/posthog-js, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/posthog/posthog-js
- Repository: https://github.com/posthog/posthog-js

## Overview

##### Main Components

a. Core Library Components:
- PostHog Core Engine
- Feature Flags System
- Session Recording
- Event Capture System
- Survey System
- Storage Management
- Request Queue Handler

b. Integration Components:
- External Services Integration (Sentry, Segment)
- Web Vitals Tracking
- Exception Tracking
- Heatmaps
- Auto-capture System

c. Framework-specific Implementations:
- React Integration
- Next.js Support
- Nuxt.js Support

##### Component Relationships

**[Browser Environment]**

- [PostHog Core]
- > Feature Flags
- > Session Recording
- > Event Capture
- > Storage Management

- [Extensions]
- > Exception Tracking
- > Web Vitals
- > Surveys
- > Heatmaps

- [Integration Layer]
- > Sentry
- > Segment

- [Framework Bindings]
- > React
- > Next.js
- > Nuxt.js

##### Important Implementation Details to Highlight

- Event pipeline from capture to storage
- Persistence layer interactions
- Request queue system
- How feature flags interact with other components
- Demonstrate the session recording flow

##### Specific Technical Elements

- Rate limiting mechanisms
- Retry queue system
- Autocapture functionality
- Survey widget implementation
- Session ID management

##### Data Flow Patterns

- Event capture → Queue → Processing → Storage
- Feature Flag checks → Application logic
- Session Recording → Compression → Storage
- Survey triggers → UI rendering → Response capture

This library is primarily client-side, so emphasize browser interactions and client-side storage patterns, while clearly showing how it communicates with PostHog's backend services.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Browser["Browser Environment"]
        Core["PostHog Core Engine"]:::core
        click Core "https://github.com/PostHog/posthog-js/blob/main/src/posthog-core.ts"

        subgraph CoreComponents["Core Components"]
            FF["Feature Flags System"]:::core
            click FF "https://github.com/PostHog/posthog-js/blob/main/src/posthog-featureflags.ts"
            SR["Session Recording"]:::core
            click SR "https://github.com/PostHog/posthog-js/blob/main/src/extensions/replay/sessionrecording.ts"
            EC["Event Capture"]:::core
            click EC "https://github.com/PostHog/posthog-js/blob/main/src/autocapture.ts"
            SM["Storage Management"]:::core
            click SM "https://github.com/PostHog/posthog-js/blob/main/src/storage.ts"
            RQ["Request Queue"]:::core
            click RQ "https://github.com/PostHog/posthog-js/blob/main/src/request-queue.ts"
            SID["Session Management"]:::core
            click SID "https://github.com/PostHog/posthog-js/blob/main/src/session-props.ts"
        end

        subgraph Extensions["Extensions"]
            ET["Exception Tracking"]:::extension
            click ET "https://github.com/PostHog/posthog-js/tree/main/src/extensions/exception-autocapture/"
            WV["Web Vitals"]:::extension
            click WV "https://github.com/PostHog/posthog-js/tree/main/src/extensions/web-vitals/"
            SV["Surveys"]:::extension
            click SV "https://github.com/PostHog/posthog-js/tree/main/src/extensions/surveys/"
            HM["Heatmaps"]:::extension
            click HM "https://github.com/PostHog/posthog-js/blob/main/src/heatmaps.ts"
            RL["Rate Limiting"]:::extension
            click RL "https://github.com/PostHog/posthog-js/blob/main/src/rate-limiter.ts"
        end

        subgraph Integrations["Integration Layer"]
            Sentry["Sentry"]:::integration
            click Sentry "https://github.com/PostHog/posthog-js/blob/main/src/extensions/sentry-integration.ts"
            Segment["Segment"]:::integration
            click Segment "https://github.com/PostHog/posthog-js/blob/main/src/extensions/segment-integration.ts"
        end

        subgraph Frameworks["Framework Bindings"]
            React["React"]:::framework
            click React "https://github.com/PostHog/posthog-js/tree/main/react/"
            Next["Next.js"]:::framework
            click Next "https://github.com/PostHog/posthog-js/tree/main/playground/nextjs/"
            Nuxt["Nuxt.js"]:::framework
            click Nuxt "https://github.com/PostHog/posthog-js/tree/main/playground/nuxtjs/"
        end
    end

    Core --> CoreComponents
    Core --> Extensions
    Core --> Integrations
    Core --> Frameworks

    FF --> EC
    SR --> SM
    EC --> RQ
    RQ --> SM
    SID --> EC

    ET --> Core
    WV --> Core
    SV --> Core
    HM --> Core
    RL --> RQ

    Sentry --> Core
    Segment --> Core

    React --> Core
    Next --> Core
    Nuxt --> Core

    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef extension fill:#4CAF50,stroke:#2E7D32,color:white
    classDef integration fill:#FF9800,stroke:#EF6C00,color:white
    classDef framework fill:#9C27B0,stroke:#6A1B9A,color:white

    subgraph Legend
        L1["Core Components"]:::core
        L2["Extensions"]:::extension
        L3["Integrations"]:::integration
        L4["Framework Bindings"]:::framework
    end
```
