Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.getReplayId()`.
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 getReplayId(): 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/getReplayId.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 | getReplayId', () => {
afterEach(() => {
jest.clearAllMocks();
});

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

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

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

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