Skip to content

Commit

Permalink
feat(replay): Add getSessonId() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Apr 12, 2023
1 parent 3efb556 commit 86d4a09
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/replay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ A session starts when the Session Replay SDK is first loaded and initialized. Th
[^1]: An 'interaction' refers to either a mouse click or a browser navigation event.
### Accessing the Replay Session ID
You can get the ID of the currently running session via `replay.getSessionId()`.
This will return `undefined` if no session is ongoing.
### Replay Captures Only on Errors
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.
Expand Down
11 changes: 11 additions & 0 deletions packages/replay/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
return this._replay.flushImmediate();
}

/**
* Get the current session ID.
*/
public getSessionId(): string | undefined {
if (!this._replay || !this._replay.isEnabled()) {
return;
}

return this._replay.getSessionId();
}

/** Setup the integration. */
private _setup(): void {
// Client is not available in constructor, so we need to wait until setupOnce
Expand Down
26 changes: 26 additions & 0 deletions packages/replay/test/integration/getSessionId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { mockSdk } from '../mocks/mockSdk';
import { useFakeTimers } from '../utils/use-fake-timers';

useFakeTimers();

describe('Integration | getSessionId', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('works', async () => {
const { integration, replay } = await mockSdk({
replayOptions: {
stickySession: true,
},
});

expect(integration.getSessionId()).toBeDefined();
expect(integration.getSessionId()).toEqual(replay.session?.id);

// When stopped, it is undefined
integration.stop();

expect(integration.getSessionId()).toBeUndefined();
});
});

0 comments on commit 86d4a09

Please sign in to comment.