How it works
Once started, the SDK sends periodic TimeSpentEvents throughout the session. When the student finishes, the SDK sends one final TimeSpentEvent followed by an ActivityCompletedEvent. All events share the samerunId for correlation.
Starting an activity
Every single-session activity starts the same way:See the
reference for
full parameter documentation including time options, callbacks, and
runId.Ending an activity
How you end an activity depends on whether the student completed the activity. In both cases, accumulated time data is flushed. The difference is whether a completion result is also recorded.With completion
When a student finishes, callactivity.end() with completion metrics:
- Flushes a final heartbeat (
TimeSpentEvent) - Submits the completion (
ActivityCompletedEvent)
See the reference
for full completion data documentation.
Without completion
Callactivity.end() without arguments to flush time data only; in this case, no completion event is recorded.
- Flushes a final heartbeat (
TimeSpentEvent) - Stops the heartbeat timer
- Cleanup: the component unmounts before the student finishes (e.g. navigating away mid-activity). You want to flush accumulated time without recording a result.
- Stateful activities: the activity spans multiple sessions, so the client only tracks time per visit. Completion is recorded by the server when the student eventually finishes.
Framework integration
- React
- Vue
- Svelte
- Solid
Best practices
Start activities in useEffect/onMount
Start activities in useEffect/onMount
Always start activities in lifecycle hooks to ensure proper cleanup.
Flush time on cleanup, complete on user action
Flush time on cleanup, complete on user action
Use
activity.end() (no args) in cleanup functions. Only call activity.end(metrics) when
the student has actually finished the activity.Handle missing timeback client
Handle missing timeback client
Check if the timeback client exists before creating activities (it may be null if not
authenticated).
Use meaningful activity IDs
Use meaningful activity IDs
Use stable, unique IDs that identify the specific lesson or content piece.
Next steps
Stateful activities
Multi-session activities with server-side completion
Reference
Parameters, properties, methods, and callbacks