0
# Session Management
1
2
User session tracking and management for monitoring user behavior and application health across user sessions.
3
4
## Capabilities
5
6
### Session Control
7
8
Start, end, and capture session information.
9
10
```typescript { .api }
11
/**
12
* Start a new user session
13
* @param context - Optional session context
14
* @returns Session instance
15
*/
16
function startSession(context?: SessionContext): Session;
17
18
/**
19
* End the current user session
20
*/
21
function endSession(): void;
22
23
/**
24
* Capture current session information
25
* @param end - Whether to end the session after capturing
26
*/
27
function captureSession(end?: boolean): void;
28
```
29
30
**Usage Examples:**
31
32
```typescript
33
import { startSession, endSession, captureSession } from "@sentry/browser";
34
35
// Start session with context
36
startSession({
37
user: { id: "12345", email: "user@example.com" },
38
environment: "production",
39
release: "1.2.3",
40
});
41
42
// Capture session health
43
captureSession(); // Reports current session status
44
45
// End session on logout
46
endSession();
47
```
48
49
## Types
50
51
### Session Structure
52
53
```typescript { .api }
54
interface Session {
55
/** Session ID */
56
sid: string;
57
58
/** Device ID */
59
did?: string;
60
61
/** Initial session flag */
62
init: boolean;
63
64
/** Session timestamp */
65
timestamp: number;
66
67
/** Session start time */
68
started: number;
69
70
/** Session duration in seconds */
71
duration?: number;
72
73
/** Session status */
74
status: SessionStatus;
75
76
/** Release version */
77
release?: string;
78
79
/** Environment */
80
environment?: string;
81
82
/** User agent */
83
userAgent?: string;
84
85
/** IP address */
86
ipAddress?: string;
87
88
/** User information */
89
user?: User;
90
91
/** Number of errors in session */
92
errors?: number;
93
94
/** Session attributes */
95
attrs?: {
96
release?: string;
97
environment?: string;
98
user_agent?: string;
99
ip_address?: string;
100
};
101
}
102
103
type SessionStatus = "ok" | "exited" | "crashed" | "abnormal";
104
105
interface SessionContext {
106
user?: User;
107
tags?: { [key: string]: Primitive };
108
environment?: string;
109
release?: string;
110
}
111
```
112
113
### Browser Session Integration
114
115
The `browserSessionIntegration` automatically tracks sessions:
116
117
```typescript { .api }
118
/**
119
* Browser session integration for automatic session tracking
120
* @param options - Session integration options
121
* @returns Browser session integration
122
*/
123
function browserSessionIntegration(options?: BrowserSessionOptions): Integration;
124
125
interface BrowserSessionOptions {
126
/** Maximum session duration in seconds */
127
maxSessionDuration?: number;
128
129
/** Session timeout in seconds */
130
sessionTimeout?: number;
131
}
132
```
133
134
**Usage Example:**
135
136
```typescript
137
import { browserSessionIntegration } from "@sentry/browser";
138
139
Sentry.init({
140
dsn: "YOUR_DSN",
141
integrations: [
142
browserSessionIntegration({
143
maxSessionDuration: 30 * 60, // 30 minutes
144
sessionTimeout: 5 * 60, // 5 minutes
145
}),
146
],
147
});
148
```
149
150
## Session Health Monitoring
151
152
Sessions automatically track:
153
- Session duration
154
- Number of errors during session
155
- Crashes and abnormal exits
156
- User engagement metrics
157
- Application health over time