Skip to content

Commit 86d4a09

Browse files
committed
feat(replay): Add getSessonId() method
1 parent 3efb556 commit 86d4a09

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

packages/replay/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ A session starts when the Session Replay SDK is first loaded and initialized. Th
138138
139139
[^1]: An 'interaction' refers to either a mouse click or a browser navigation event.
140140
141+
### Accessing the Replay Session ID
142+
143+
You can get the ID of the currently running session via `replay.getSessionId()`.
144+
This will return `undefined` if no session is ongoing.
145+
141146
### Replay Captures Only on Errors
142147
143148
Alternatively, rather than recording an entire session, you can capture a replay only when an error occurs. In this case, the integration will buffer up to one minute worth of events prior to the error being thrown. It will continue to record the session following the rules above regarding session life and activity. Read the [sampling](#Sampling) section for configuration options.

packages/replay/src/integration.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
226226
return this._replay.flushImmediate();
227227
}
228228

229+
/**
230+
* Get the current session ID.
231+
*/
232+
public getSessionId(): string | undefined {
233+
if (!this._replay || !this._replay.isEnabled()) {
234+
return;
235+
}
236+
237+
return this._replay.getSessionId();
238+
}
239+
229240
/** Setup the integration. */
230241
private _setup(): void {
231242
// Client is not available in constructor, so we need to wait until setupOnce
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { mockSdk } from '../mocks/mockSdk';
2+
import { useFakeTimers } from '../utils/use-fake-timers';
3+
4+
useFakeTimers();
5+
6+
describe('Integration | getSessionId', () => {
7+
afterEach(() => {
8+
jest.clearAllMocks();
9+
});
10+
11+
it('works', async () => {
12+
const { integration, replay } = await mockSdk({
13+
replayOptions: {
14+
stickySession: true,
15+
},
16+
});
17+
18+
expect(integration.getSessionId()).toBeDefined();
19+
expect(integration.getSessionId()).toEqual(replay.session?.id);
20+
21+
// When stopped, it is undefined
22+
integration.stop();
23+
24+
expect(integration.getSessionId()).toBeUndefined();
25+
});
26+
});

0 commit comments

Comments
 (0)