How inbestigator/analytics works
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.