Skip to main content
This guide is for Next.js projects that use the /app directory (Next.js 13+). If your project has a /pages folder instead, use this guide
Before starting: Get your API key from Settings

Step 1 — Install the SDK


Step 2 — Create components/analytics.tsx (One-time setup)

Create this file anywhere in your project. This is the only place Analytiq is configured.
components/analytics.tsx
Add your API key to .env.local:
The prefix must be NEXT_PUBLIC_. Any other prefix (like VITE_ or just ANALYTIQ_KEY) will make the key undefined on the client.

Step 3 — Wrap your app in app/layout.tsx

Open your existing app/layout.tsx and wrap the children with the provider. Make sure it goes inside your Auth provider.
app/layout.tsx
Setup is complete! You never need to touch these two files again for analytics purposes.

What the setup handles automatically


Step 4 — Track Custom Events

For specific actions (page views, button clicks, purchases), import track() from your analytics.tsx file and call it where the action happens. Always add 'use client' at the top since track() is browser-only.
app/page.tsx

Step 5 — Verify it’s working

  1. Run: npm run dev
  2. Log into your app (so your useAuth returns a user)
  3. Open http://localhost:3000, press F12Network tab, filter by track
  4. You should see a POST /api/events/track request for any event you fire manually

Common Mistakes