Overview
TheTrackTime component monitors how long users spend on a page and tracks when they reach specific time intervals. It also captures the total time spent when the component unmounts.
Props
The content to render and track time for.
The event name to track for time interval milestones.
Array of time intervals in seconds. Tracks an event when each interval is reached.
Usage
Basic Time Tracking
Custom Intervals
Product Page Engagement
Documentation Page
Tracked Events
time_on_page (or custom event)
Tracked when a user reaches a time interval:page_time_final
Automatically tracked when the component unmounts:Behavior
- Starts tracking time immediately on mount
- Each interval is tracked only once
- Checks intervals every second using
setInterval - Tracks final time on component unmount
- Automatically cleans up timers on unmount
- Renders children as a React Fragment (no wrapper element)
- Time is measured in seconds
Use Cases
- Measure content engagement duration
- Track reading time for articles
- Monitor video or media consumption time
- Identify high-value content by time spent
- A/B test content length effectiveness
- Trigger actions based on time thresholds
- Analyze user attention spans
- Measure product page interest
Implementation Details
The component uses twouseEffect hooks:
- Interval tracking: Runs a timer every second to check if any time intervals have been reached
- Final time tracking: Captures the total time spent when the component unmounts
Performance Notes
- Uses a 1-second interval timer (low overhead)
- Intervals are tracked in a Set for efficiency
- No re-renders triggered by time updates
- Cleanup is automatic and thorough