Locallytics

Introduction

Self-hosted, privacy-first analytics for Next.js.

Locallytics is a lightweight analytics SDK that stores data in your own database. Built for Next.js App Router.

  • Privacy-first — No cookies, respects Do Not Track
  • Self-hosted — Your data stays on your servers
  • Lightweight — Under 5KB client bundle

Quick Start

Install

npm install locallytics

Migrate Database

Create the required tables in your database. Supports PostgreSQL, MySQL, and SQLite.

npx locallytics-cli migrate
Set DATABASE_URL in your environment first.

Create API Route

app/api/analytics/route.ts
import { locallytics } from "locallytics";

const analytics = await locallytics({
  database: process.env.DATABASE_URL!,
});

export const { GET, POST } = analytics;

Add Tracker

app/layout.tsx
import { LocallyticsGrabber } from "locallytics";

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        {children}
        <LocallyticsGrabber />
      </body>
    </html>
  );
}

View Data

app/dashboard/page.tsx
import { LocallyticsData } from "locallytics";

export default async function Dashboard() {
  const data = await LocallyticsData();

  return (
    <div>
      <p>{data.pageviews} pageviews</p>
      <p>{data.uniqueVisitors} visitors</p>
    </div>
  );
}

What's Next?

On this page