Analytics
Tracking Custom Events
Learn how to track custom user interactions using hook
Beyond automatic page views and Web Vitals, you'll want to understand how user interact with specific parts of your app.
How to Track Events
Once you've set up the AnalyticsProvider, you can call the track
function from any Client Component to record an event.
That's it! When the button is clicked, an event named "get_started_button_clicked"
is sent.
Adding Context with Data
Often you'll want to include more details about the event. Pass a simple object as the second argument to track
.
The track
function signature:
eventName
: A clear name for the interaction (e.g., "signup", "video_play").eventData
(Optional): A plain object with any relevant details. Keep it simple and avoid sensitive information.
Where to Use track
Remember to call track
from Client Components, because user interactions
happen in the browser.
Use track
inside:
- Event handlers (
onClick
,onSubmit
etc.) useEffect
hooks (for events triggered by component lifecycle or state changes)
Tracking custom events helps you understand user behavior deeply.