Overview
A session represents a single browsing session on your site. Sessions help you understand how users interact with your site during a specific visit, including how long they stay, how many pages they view, and when they leave.Session Lifecycle
Creation
A new session is automatically created when:- A user visits your site for the first time
- A user returns after 30+ minutes of inactivity
- A user closes their browser tab and opens a new one
- The
clear()function is called
Format
Session IDs use the format:sess_7c9e6679-7425-40de-944b-e07fc1f90ae7
Storage
Sessions are stored insessionStorage with the following keys:
The session ID itself.
Example:
"sess_7c9e6679-7425-40de-944b-e07fc1f90ae7"Last activity timestamp in milliseconds. Updated on every pageview.
Example:
"1709318400000"Session creation timestamp in milliseconds.
Example:
"1709318400000"Session Expiration
Inactivity Timeout
Sessions expire after 30 minutes of inactivity. The inactivity timer resets on every pageview. How it works:- User views a page -
did_session_timestampis updated - User views another page 10 minutes later - timestamp updated again
- User is inactive for 30 minutes - session expires
- User returns - new session is created
/home/daytona/workspace/source/packages/tracker/src/core/tracker.ts:188-189
Manual Expiration
You can manually expire a session by callingclear():
Tab Closure
Since sessions are stored insessionStorage, they automatically expire when the user:
- Closes the browser tab
- Closes the browser window
- Navigates to a different domain (unless using cross-domain tracking)
Opening the same site in a new tab creates a separate session. Each tab maintains its own
sessionStorage.Session Metrics
Databuddy automatically tracks session-level metrics:Page Count
The number of pages viewed in the current session:screen_view and page_exit events:
Session Start Time
The timestamp when the session was created:Time on Page
Calculated for eachpage_exit event:
Engaged Time
Databuddy tracks actual engagement time by monitoring visibility and focus:- Page is visible (not in background tab)
- Browser window has focus
- Automatically pauses when user switches tabs
Session Management Functions
getSessionId()
Retrieve the current session ID:With URL Parameters
Check URL parameters first (for cross-domain tracking):clear()
Reset the current session and create new identifiers:clear() does:
- Removes all tracking data from storage
- Generates new anonymous ID
- Generates new session ID
- Resets page count to 0
- Resets engagement metrics
- Clears global properties
/home/daytona/workspace/source/packages/tracker/src/index.ts:329-346
flush()
Force immediate delivery of all queued events:flush():
- Before navigation to external sites
- Before closing a modal or overlay
- After critical events that must be captured
/home/daytona/workspace/source/packages/sdk/src/core/tracker.ts:159-175
Event Batching
To optimize network usage, Databuddy batches events before sending them to the server.Default Configuration
Enable or disable event batching.
Number of events to batch before sending. Min: 1, Max: 50.
Batch timeout in milliseconds. Min: 100, Max: 30000.
- Batch reaches
batchSizeevents batchTimeoutmilliseconds have elapsed since first event in batch
Custom Batching Configuration
Disable Batching
Session-Based Analysis
Track Session Duration
Session Abandonment
Detect when users abandon a flow:Multi-Tab Detection
Back/Forward Cache (bfcache)
Modern browsers use back/forward cache to instantly restore pages when users click the back button. Databuddy handles this automatically:- Detects when a page is restored from bfcache
- Checks if session has expired (30 minutes)
- Generates new session ID if needed
- Tracks a
screen_viewevent with special property
/home/daytona/workspace/source/packages/tracker/src/index.ts:233-251
Best Practices
Session Boundaries
Use
clear() to mark logical session boundaries like user logout. Don’t call it unnecessarily.Flush Critical Events
Call
flush() before navigation to external sites or critical user actions to ensure events are captured.Monitor Session Length
Track session duration metrics to understand user engagement patterns.
Cross-Domain Continuity
Use tracking parameters when linking between your domains to maintain session continuity.
Related Documentation
- User Identification - Anonymous and session IDs
- Pageview Tracking - Automatic pageview detection
- Custom Events - Track user interactions