Setting up locally

This chapter provides a comprehensive guide for developers to set up and run diy-analytics locally. By following these steps, you will be able to customize, contribute, and gain a deeper understanding of the project.

Prerequisites

Ensure the following tools are installed on your system before proceeding:

  • Node.js & npm: Version 18 or later is recommended.
  • Git: For cloning the repository and version control.
  • PostgreSQL Database: PostgreSQL 14 or later. You can use a local PostgreSQL server or a cloud database (Vercel Postgres, Neon, Supabase, etc.).

Local Setup Steps

1. Clone the Repository

Open your terminal and execute:

bash
git clone https://github.com/heysagnik/diy-analytics.git && cd diy-analytics

2. Install the dependencies

bash
npm install

3. Configure your environment

Copy the example environment file and fill in your own values:

bash
cp .env.local.example .env.local

Then edit .env.local:

.env.local
1# PostgreSQL connection URI 2# Example: postgres://user:password@localhost:5432/diy-analytics?sslmode=disable 3DATABASE_URL= 4 5# Public URL of your deployment (no trailing slash) 6NEXT_PUBLIC_SITE_URL=http://localhost:3000 7 8# Optional storage cap in MB (defaults to 512) 9DATABASE_STORAGE_CAP_MB=512

See Environment Variables for what each of these does. Note that authentication is database-backed — there's no global admin password to configure, you'll sign up for a workspace account the first time you open the app.

4. Apply Database Migrations

Your database is empty until you run this — it creates the tables diy-analytics needs (projects, pageviews, events, and so on) using Drizzle ORM, the library that manages the database schema:

bash
npm run db:migrate

If migrating from an existing MongoDB deployment, run the one-time data migration script:

bash
npm run db:migrate-from-mongo

See Migrating from MongoDB for detailed migration instructions.

5. Start the Development Server

With everything in place, you can now start the application.

bash
npm run dev

This command fires up a local, hot-reloading server at http://localhost:3000.

6. Run the test suite (optional)

The project uses Jest for tests:

bash
npm test

7. Build for production (optional)

To verify a production build compiles cleanly:

bash
1npm run build 2npm run start