# inbestigator/analytics architecture

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

- Interactive diagram: https://gitdiagram.com/inbestigator/analytics
- Repository: https://github.com/inbestigator/analytics

## Overview

##### Project Overview and Purpose
- Is designed as an analytics platform with core functionalities such as capturing logs, re-capturing data, releasing (deleting) events, and performing user fingerprinting.
- The project is architected as a full-stack application with a clear separation between the client libraries (for capturing analytics), an API/backend built with Next.js (in the “www” directory), and core/utility modules written in Deno (distributed in the “lib” folder).
- There is also a database layer managed through Prisma (evident from the “www/prisma” migrations and schema), which implies storage for analytics logs and possibly user data.

##### Analyzing the File Structure
- lib/ Folder:
- Contains the core logic in modules like capture.ts, client.ts, fingerprint.ts, recap.ts, and release.ts.
- There is an “internal” module (incognito.ts), which may encapsulate additional privacy features or internal methods.
- This layer is used by the client library allowing third-party or in-app integrations to utilize the analytics functionalities.
- www/ Folder:
- Contains a Next.js web application with:
□ an API directory (www/src/app/api) housing endpoints for analytics capture (e.g., /analytics/capture and /analytics/recap) along with authentication and tRPC routes.
□ A “server” directory for backend logic (such as database interaction, authentication configurations, and router definitions).
□ Client-side components (UI elements like auth-button, dialogs, and project components) that likely interact with the analytics API.
□ Prisma folder with migrations indicating how database schema is managed.
- Tests:
- The tests (in the tests folder) validate the core functions such as capturing data and recurring logs.
- Configuration Files:
- Configuration files for Deno (deno.json) and Next.js (next.config.js, tsconfig.json) guide build/deployment, further emphasizing the segmented architecture.

a. Main Components:
- Analytics Client Library (in lib/): Representing modules like capture, recap, release, fingerprint.
- Web/API Server (Next.js part in www/) with specific API routes:
- /api/analytics endpoints (for capture and recap)
- /api/auth endpoints (using next-auth for authentication)
- tRPC endpoints
- Frontend Client/Website: User interface components that trigger analytics requests.
- Database: Prisma-managed datastore handling analytics events and user data.
- External Services: Optional external integrations if needed (e.g., authentication providers, third-party analytics viewers) explicitly indicated by artifacts like the NextAuth routes.

b. Relationships and Interactions:
- Within the API Server, illustrate calls to the underlying Analytics Client Library functions (capture, recap, etc.) which encapsulate business logic.
- If applicable, interactions between authentication components and the analytics tracking (for user fingerprinting).

c. Architectural Patterns & Design Principles:
- Separation of Concerns: Client library (analytics functionality) is separated from the Next.js API and UI.
- Modular Design: The “lib” modules are small, focused modules (each ~3kb gzipped) that handle distinct aspects of analytics.
- API Layer: Next.js API routes wrap and expose the functionality of the core library to client apps.
- Use of ORM (Prisma) abstracts the underlying database, making database operations modular and scalable.
- Security Best Practices: Clear delineation of private vs. public keys (as noted in the README for client initialization) underscores the need for authentication and secure API design.

d. Technologies and Frameworks:
- Deno (as indicated by deno.json and the lib folder) for the core analytics functions.
- Next.js for the frontend and API endpoints.
- Prisma as the ORM for database management.
- tRPC for typed API calls in the API layer.
- NextAuth for authentication (based on files in www/src/app/api/auth).
- Tailwind CSS (referenced via www/tailwind.config.ts) for frontend styling.

##### What
- Labels for each core component: “Analytics Client Library,” “API Server (Next.js)” with sub-sections for “Analytics Routes,” “Authentication,” and “tRPC Endpoints,” “Frontend UI,” and “Database (Prisma).”
- Frontend → API Server (HTTP/tRPC calls)
- API Server → Client Library (internal function calls for analytics capture, release, recap, fingerprint)
- Client Library → Database (CRUD operations)
- Annotations noting key technologies (Deno, Next.js, Prisma, etc.) beside the respective components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend UI"
        Frontend_App["App (layout & pages)"]
        UI_Components["UI Components (auth-button, create-event, etc.)"]
    end

    %% API Server Layer
    subgraph "API Server (Next.js)"
        Analytics_API["Analytics API"]
        Auth_API["Auth API"]
        TRPC_App["tRPC Endpoints (App)"]
        Server_Logic["Server Logic"]
        TRPC_Server["tRPC Endpoints (Server)"]
    end

    %% Analytics Client Library Layer
    subgraph "Analytics Client Library"
        Client_Lib["Analytics Core Library"]
        Incognito["Internal Incognito Module"]
    end

    %% Database Layer
    Database["Database (Prisma)"]

    %% Data Flow
    Frontend_App -->|"HTTP_call"| Analytics_API
    Frontend_App -->|"Auth_call"| Auth_API
    Frontend_App -->|"tRPC_call"| TRPC_App

    Analytics_API -->|"forwards_request"| Server_Logic
    Auth_API -->|"forwards_request"| Server_Logic
    TRPC_App -->|"delegates"| TRPC_Server

    Server_Logic -->|"invokes"| Client_Lib
    TRPC_Server -->|"may_invoke"| Client_Lib

    Client_Lib -->|"uses_internal"| Incognito
    Client_Lib -->|"writes/reads"| Database
    Database -->|"response"| Server_Logic
    Server_Logic -->|"response"| Frontend_App

    %% Click Events
    click Frontend_App "https://github.com/inbestigator/analytics/tree/main/www/src/app"
    click UI_Components "https://github.com/inbestigator/analytics/tree/main/www/src/components"
    click Analytics_API "https://github.com/inbestigator/analytics/tree/main/www/src/app/api/analytics"
    click Auth_API "https://github.com/inbestigator/analytics/tree/main/www/src/app/api/auth"
    click TRPC_App "https://github.com/inbestigator/analytics/tree/main/www/src/app/api/trpc"
    click Server_Logic "https://github.com/inbestigator/analytics/tree/main/www/src/server"
    click TRPC_Server "https://github.com/inbestigator/analytics/blob/main/www/src/server/api/trpc.ts"
    click Client_Lib "https://github.com/inbestigator/analytics/tree/main/lib/core"
    click Incognito "https://github.com/inbestigator/analytics/blob/main/lib/internal/incognito.ts"
    click Database "https://github.com/inbestigator/analytics/tree/main/www/prisma"

    %% Styles
    classDef frontend fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef apiserver fill:#FFCC80,stroke:#E65100,stroke-width:2px;
    classDef clientlib fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef database fill:#FFECB3,stroke:#F57F17,stroke-width:2px;

    class Frontend_App,UI_Components frontend;
    class Analytics_API,Auth_API,TRPC_App,Server_Logic,TRPC_Server apiserver;
    class Client_Lib,Incognito clientlib;
    class Database database;
```
