# microsoft/clarity architecture

> Architecture diagram of the GitHub repository microsoft/clarity, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/microsoft/clarity
- Repository: https://github.com/microsoft/clarity

## Overview

##### Main Components
- Clarity-JS: This is the client-side instrumentation library that runs on user websites. It captures user interactions, layout changes, and performance metrics.
- Clarity-Decode: This backend (or server-side) module receives the raw telemetry data and decodes it into its original, interpretable format.
- Clarity-DevTools: A Chromium-based devtools extension that helps in generating live captures and debugging sessions on the fly.

##### Out the Relationships and Interactions
- Clarity-JS component as a key part of the client-side instrumentation. Note that it is responsible for capturing various types of user interactions (clicks, scrolling, input events, layout changes, performance metrics, etc.).
- Clarity-Decode as a backend service that processes incoming data, decodes various formats (interaction, layout, performance, etc.) and prepares it for visualization.

##### Architectural Patterns and Design Principles
- Specify that the system uses event-driven data capture and pipeline processing, where the telemetry flows from client detection to server decoding and finally to session replay visualization.

##### Relevant Technologies and Frameworks
- Languages/Frameworks: All packages are written in TypeScript.
- Build Tools: Use of Rollup (as seen in rollup.config.ts files) for bundling.
- Package Management: Managed via Lerna, highlighting a monorepo structure.
- Additionally, note any relevant configurations like ESLint/TSLint configurations and TypeScript definitions (d.ts files) that enforce strong typing across the packages.

##### Essential Elements
- Annotations: Optionally annotate with key design decisions such as “modular design with Lerna” or “privacy-first data handling”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Side Components
    subgraph "Client Side"
        Browser["Browser/Client (Website Integration)"]:::client
        ClarityJS["Clarity-JS – Client Instrumentation"]:::client
        Telemetry["Telemetry Data Pipeline"]:::client
    end

    %% Backend Services
    subgraph "Backend Services"
        ClarityDecode["Clarity-Decode – Telemetry Data Decoding"]:::backend
        ClarityVisualize["Clarity-Visualize – Session Replay Visualization"]:::backend
    end

    %% Developer Tools
    subgraph "Developer Tools"
        ClarityDevTools["Clarity-DevTools – Chromium DevTools Extension"]:::devtools
    end

    %% Data Flow Connections
    Browser -->|"sendsTelemetry"| ClarityJS
    ClarityJS -->|"capturesAndForwards"| Telemetry
    Telemetry -->|"forwardsRawData"| ClarityDecode
    ClarityDecode -->|"producesDecodedData"| ClarityVisualize
    Browser -->|"injectsDevTools"| ClarityDevTools
    ClarityJS -->|"optionalLiveCapture"| ClarityDevTools

    %% Click Events for Component Mapping
    click ClarityJS "https://github.com/microsoft/clarity/tree/master/packages/clarity-js"
    click ClarityDecode "https://github.com/microsoft/clarity/tree/master/packages/clarity-decode"
    click ClarityVisualize "https://github.com/microsoft/clarity/tree/master/packages/clarity-visualize"
    click ClarityDevTools "https://github.com/microsoft/clarity/tree/master/packages/clarity-devtools"

    %% Styles
    classDef client fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef backend fill:#c3e6cb,stroke:#000,stroke-width:2px;
    classDef devtools fill:#ffcc80,stroke:#000,stroke-width:2px;
```
