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
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