This guide is for Next.js projects with a
/pages directory. If you have an /app directory, use the App Router guideStep 1 — Install the SDK
Step 2 — Create components/analytics.jsx (One-time setup)
Create this file anywhere in your project. This is the only place Analytiq is configured.
- TypeScript (analytics.tsx)
- JavaScript (analytics.js)
components/analytics.tsx
.env.local:
Step 3 — Wrap your app in pages/_app
_app is a special Next.js file that wraps every page. Add your AnalyticsProvider there.
- TypeScript (pages/_app.tsx)
- JavaScript (pages/_app.js)
pages/_app.tsx
Setup is complete! You never need to touch
_app again for analytics purposes.What the setup handles automatically
| Action | What happens |
|---|---|
| App loads (user logged out) | init() runs once, anonymous tracking begins |
| App loads (user already logged in) | userId restored from localStorage, user identified immediately |
| User logs in (your auth changes) | useEffect detects new user → identify(user) called automatically |
| User logs out | user becomes null → reset() called automatically |
Step 4 — Track Custom Events
For any specific action, importtrack() from your analytics.jsx file and call it where the action happens.
- Track a page view
- After a login / API call
pages/dashboard.jsx
Step 5 — Verify it’s working
- Run:
npm run dev - Log into your app
- Open
http://localhost:3000, press F12 → Network tab - You should see
POST /api/events/trackrequests for everytrack()call you make
Common Mistakes
| Mistake | Symptom | Fix |
|---|---|---|
Using ANALYTIQ_KEY without NEXT_PUBLIC_ | undefined key warning | Rename to NEXT_PUBLIC_ANALYTIQ_KEY |
AnalyticsProvider outside AuthProvider | useAuth() context error | Auth must always wrap Analytics |
Importing track from analytiq directly | Events may fire before SDK initializes | Always import from your local analytics.jsx hub |