Before starting: Get your API key from the Settings page
What you’ll need
- React app (Vite or Create React App)
- Your Analytiq API key
- An existing auth state (like
useAuth(), auseState, or a Context)
Step 1 — Install the SDK
Step 2 — Create src/analytics.jsx (One-time setup)
Create a new file in your src/ folder called analytics.jsx. This is the only place Analytiq is configured. You never touch it again after this.
- JavaScript (analytics.jsx)
- TypeScript (analytics.tsx)
src/analytics.jsx
.env:
Step 3 — Wrap your app with AnalyticsProvider
Opensrc/App.jsx (or App.tsx) and wrap your existing app with the provider you just created. Make sure it goes inside your Auth provider.
src/App.jsx
That’s the entire setup! You never touch these files 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 (your auth clears) | user becomes null → reset() called automatically |
| User goes offline | Events saved to localStorage, retried on reconnect |
Step 4 — Track Custom Events
For any specific action you want to measure (page views, clicks, purchases), importtrack() from your analytics.jsx file and call it where the action happens.
- Track a page view
- After an API call succeeds
src/pages/Home.jsx
Rule of thumb: Always import
track from your local analytics.jsx file, not directly from analytiq. This ensures your tracking is always correctly initialized before it fires.Step 5 — Verify it’s working
- Run your app:
npm run dev - Log into your app (so your
useAuthreturns a user) - Open browser DevTools (
F12) → Network tab, filter bytrack - You should see a
POST /api/events/trackrequest for every event you fire
Common Mistakes
| Mistake | Symptom | Fix |
|---|---|---|
Using ANALYTIQ_KEY without VITE_ prefix | undefined key error in console | Rename to VITE_ANALYTIQ_KEY in .env |
Importing track from analytiq instead of analytics.jsx | Events fire before SDK initializes | Always import from your local analytics.jsx hub |
AnalyticsProvider placed outside AuthProvider | useAuth() crashes with context error | Ensure Auth wraps Analytics in App.jsx |
Not calling identify() after login | Active Users shows 0 on dashboard | The analytics.jsx useEffect must receive a truthy user object |