The two-minute setup
Six entries, in order. You'll need a Supabase project and your site's production URL. Or skip to 2.7 and hand the whole recipe to your AI tool.
Six entries, in order. You'll need a Supabase project and your site's production URL. Or skip to 2.7 and hand the whole recipe to your AI tool.
npm install peeplessZero dependencies. The browser part is under 2 KB gzipped, held under a 2 KB budget by the build.
In Supabase, open the SQL editor and run starters/supabase/schema.sql once (on Lovable or Bolt, as a new migration). It creates peepless.counts in a schema the public API can't reach. Only your server writes to it, through peepless_add.
create table if not exists peepless.counts (
day date not null,
project text not null check (project ~ '^[a-z0-9][a-z0-9-]{0,63}$'),
counter text not null check (counter ~ '^[A-Za-z0-9_:/*.\[\]-]{1,120}$'),
count bigint not null default 0 check (count >= 0),
primary key (day, project, counter)
);
Don't expose the peepless schema in the API settings. One row per day, project and counter, holding a number and nothing else.
Not on Supabase? Run starters/postgres/0001_peepless.sql on any Postgres and use postgresStore, or the D1 migration in the Cloudflare starter with d1Store. Something else? Write a store and check it with testStore from peepless/testing.
Pick the one that fits. In each, list your site's origin, the events it may count, and its page patterns.
// peepless's counter as a Next.js route handler: app/api/peepless/route.ts. // The client's default endpoint is /api/peepless, so start() needs no endpoint option. // Set SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY (server-only; never NEXT_PUBLIC_). // On Vercel, visits are counted by country from the x-vercel-ip-country header. import { createHandler, supabaseStore } from 'peepless/server' const handler = createHandler({ projects: { 'my-app': { origin: 'https://example.com', events: ['signed_up', 'exported'], routes: ['/', '/blog/*', '/app/*'] }, }, store: supabaseStore({ url: process.env.SUPABASE_URL!, key: process.env.SUPABASE_SERVICE_ROLE_KEY! }), }) export const POST = handler export const OPTIONS = handler
// peepless's counter as a Supabase Edge Function. // // supabase functions deploy peepless --no-verify-jwt // // --no-verify-jwt is needed because beacons carry no Supabase key; the handler checks // the Origin instead. Then point the client at it: // start({ project: 'my-app', host: 'example.com', // endpoint: 'https://<project-ref>.supabase.co/functions/v1/peepless' }) // and add that URL to connect-src if the site has a Content-Security-Policy. // // Visits are counted by country only when the request carries a country header // (Vercel, Cloudflare, CloudFront). Where it doesn't, they show as country:xx. import { createHandler, supabaseStore } from 'npm:peepless@0/server' const handler = createHandler({ projects: { // Your site's name, the origin it's served from, and the events it may count. 'my-app': { origin: 'https://example.com', events: ['signed_up', 'exported'], routes: ['/', '/blog/*', '/app/*'] }, }, store: supabaseStore({ url: Deno.env.get('SUPABASE_URL')!, key: Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!, }), }) Deno.serve(handler)
// peepless on Cloudflare Workers, counting into D1. Serves: // POST /api/peepless the beacon (the client's default endpoint) // GET /api/peepless/stats the numbers, with Authorization: Bearer <PEEPLESS_STATS_TOKEN> // and hands every other request to your site's static assets. // // Setup (see wrangler.jsonc next to this file): // npx wrangler d1 create peepless # put the id in wrangler.jsonc // npx wrangler d1 migrations apply peepless --remote // npx wrangler secret put PEEPLESS_STATS_TOKEN # optional: a long random value // npx wrangler deploy // // Countries come from request.cf.country, which Workers always has. The rate limit is per Worker instance. import { createHandler, createStatsHandler, d1Store } from 'peepless/server' interface Env { DB: D1Database ASSETS: Fetcher PEEPLESS_STATS_TOKEN?: string } const projects = { // Your site's name, the origin it's served from, and what it may count. 'my-app': { origin: 'https://example.com', events: ['signed_up', 'exported'], routes: ['/', '/blog/*', '/app/*'] }, } /** request.cf.country as a lowercase code; T1 (Tor) and XX (unknown) count as no country. */ const country = (r: Request) => { const c = (r as { cf?: { country?: string } }).cf?.country?.toLowerCase() ?? '' return /^[a-z]{2}$/.test(c) && c !== 'xx' && c !== 't1' ? c : null } let counter: ((req: Request) => Promise<Response>) | undefined export default { async fetch(req: Request, env: Env): Promise<Response> { const { pathname } = new URL(req.url) const store = d1Store(env.DB) // Made once per instance, so its rate limit sees more than one request. Cloudflare sets // cf-connecting-ip itself; x-forwarded-for can come from the client. if (pathname === '/api/peepless') return (counter ??= createHandler({ projects, store, address: (r) => r.headers.get('cf-connecting-ip'), country }))(req) if (pathname === '/api/peepless/stats') return createStatsHandler({ store, token: env.PEEPLESS_STATS_TOKEN })(req) return env.ASSETS.fetch(req) }, } satisfies ExportedHandler<Env>
With D1 and its migration, starters/cloudflare/migrations/0001_peepless.sql, in place of the Supabase table.
// peepless's counter as a SvelteKit endpoint: src/routes/api/peepless/+server.ts. // The client's default endpoint is /api/peepless, so start() needs no endpoint option. // Set SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY as private env vars (never PUBLIC_). // Using Postgres directly instead? Swap the store for postgresStore (see the README). import { env } from '$env/dynamic/private' import { createHandler, supabaseStore } from 'peepless/server' const handler = createHandler({ projects: { 'my-app': { origin: 'https://example.com', events: ['signed_up', 'exported'], routes: ['/', '/blog/*', '/app/*'] }, }, store: supabaseStore({ url: env.SUPABASE_URL!, key: env.SUPABASE_SERVICE_ROLE_KEY! }), }) export const POST = ({ request }: { request: Request }) => handler(request) export const OPTIONS = POST
// Remix, Astro, Hono, Deno, Bun: anything with Request and Response. // Serve POST and OPTIONS at /api/peepless. import { createHandler, memoryStore } from 'peepless/server' const handler = createHandler({ projects: { 'my-app': { origin: 'https://example.com', events: ['exported'], routes: ['/', '/blog/*'] } }, store: memoryStore(), // or supabaseStore, postgresStore(query), d1Store(env.DB), or your own perMinute: 60, }) export const POST = ({ request }) => handler(request) export const OPTIONS = ({ request }) => handler(request)
A wrong setting throws when the handler is created, so a typo fails at deploy time instead of silently counting nothing.
Call start once, in the browser, as early as you can: src/main.tsx in Vite, or a 'use client' component in your root layout on Next.js. It counts the first page and every client-side navigation after it.
import { start } from 'peepless'
start({
project: 'my-app',
host: 'example.com', // counts only here: not on localhost or preview URLs
routes: ['/', '/blog/*', '/app/*'], // page types, never the real path
// endpoint: 'https://<ref>.supabase.co/functions/v1/peepless', // with the Edge Function
})
No bundler? Copy node_modules/peepless/dist/peepless.js into public/ and load it from your own site:
<script src="/peepless.js" data-project="my-app" data-host="example.com" data-routes="/ /blog/* /app/*" defer></script>
Where an action finishes successfully (not when a button is clicked), call track. Every event name must be listed in events on the server, or it's refused.
import { track } from 'peepless'
track('exported')
track('signed_up', user.id) // with a key: at most once per key per page load. The key is never sent.
Nothing is counted on localhost, on preview URLs, or in Playwright and Puppeteer. Open the live site and look in the network tab for a POST to the endpoint answering 204. Or ask the book directly:
select counter, sum(count) from peepless.counts where day > current_date - 30 group by counter order by 2 desc;
Visit any page with ?peepless-ignore once to stop counting your own device (?peepless-ignore=off undoes it).
llms.txt is the whole recipe, written for coding assistants: which starter fits your stack, where each file goes, and what not to change. Paste this into Lovable, Bolt, Cursor, Claude Code or similar:
peepless npm package. Follow https://unpkg.com/peepless/llms.txt exactly. My site is https://example.com. Count these actions: signed_up, exported.