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 locallyticsMigrate Database
Create the required tables in your database. Supports PostgreSQL, MySQL, and SQLite.
npx locallytics-cli migrateSet
DATABASE_URL in your environment first.Create API Route
import { locallytics } from "locallytics";
const analytics = await locallytics({
database: process.env.DATABASE_URL!,
});
export const { GET, POST } = analytics;Add Tracker
import { LocallyticsGrabber } from "locallytics";
export default function RootLayout({ children }) {
return (
<html>
<body>
{children}
<LocallyticsGrabber />
</body>
</html>
);
}View Data
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?
- API Route — Configuration options and authentication
- Locallytics Grabber — Client-side tracking options
- Locallytics Data — Query options and return types
- CLI — Database management commands