Skip to content

Commit

Permalink
chore(sr): amp-89623 Add staging endpoint capabilities for SDK (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
bravecod authored Dec 14, 2023
2 parents f8cf344 + 58d8cc0 commit bca3ddc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/analytics-types/src/server-zone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum ServerZone {
US = 'US',
EU = 'EU',
STAGING = 'STAGING',
}
3 changes: 2 additions & 1 deletion packages/session-replay-browser/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SessionReplayOptions,
SessionReplayPrivacyConfig,
} from './typings/session-replay';
import { DEFAULT_SAMPLE_RATE } from './constants';
import { DEFAULT_SAMPLE_RATE, DEFAULT_SERVER_ZONE } from './constants';
import { generateSessionReplayId } from './helpers';

export const getDefaultConfig = () => ({
Expand Down Expand Up @@ -40,6 +40,7 @@ export class SessionReplayConfig extends Config implements ISessionReplayConfig
this.sampleRate = options.sampleRate || DEFAULT_SAMPLE_RATE;
this.deviceId = options.deviceId;
this.sessionId = options.sessionId;
this.serverZone = options.serverZone || DEFAULT_SERVER_ZONE;

if (options.sessionId && options.deviceId) {
this.sessionReplayId = generateSessionReplayId(options.sessionId, options.deviceId);
Expand Down
3 changes: 3 additions & 0 deletions packages/session-replay-browser/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { AMPLITUDE_PREFIX } from '@amplitude/analytics-core';
import { IDBStoreSession } from './typings/session-replay';
import { ServerZone } from '@amplitude/analytics-types';

export const DEFAULT_EVENT_PROPERTY_PREFIX = '[Amplitude]';

export const DEFAULT_SESSION_REPLAY_PROPERTY = `${DEFAULT_EVENT_PROPERTY_PREFIX} Session Replay ID`;
export const DEFAULT_SESSION_START_EVENT = 'session_start';
export const DEFAULT_SESSION_END_EVENT = 'session_end';
export const DEFAULT_SAMPLE_RATE = 0;
export const DEFAULT_SERVER_ZONE = ServerZone.US;

export const BLOCK_CLASS = 'amp-block';
export const MASK_TEXT_CLASS = 'amp-mask';
export const UNMASK_TEXT_CLASS = 'amp-unmask';
export const SESSION_REPLAY_SERVER_URL = 'https://api-sr.amplitude.com/sessions/v2/track';
export const SESSION_REPLAY_EU_URL = 'https://api-sr.eu.amplitude.com/sessions/v2/track';
export const SESSION_REPLAY_STAGING_URL = 'https://api-sr.stag2.amplitude.com//sessions/v2/track';
export const STORAGE_PREFIX = `${AMPLITUDE_PREFIX}_replay_unsent`;
export const MAX_EVENT_LIST_SIZE_IN_BYTES = 1 * 1000000; // 1 MB
export const MIN_INTERVAL = 500; // 500 ms
Expand Down
6 changes: 6 additions & 0 deletions packages/session-replay-browser/src/session-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MAX_INTERVAL,
MIN_INTERVAL,
SESSION_REPLAY_EU_URL as SESSION_REPLAY_EU_SERVER_URL,
SESSION_REPLAY_STAGING_URL as SESSION_REPLAY_STAGING_SERVER_URL,
SESSION_REPLAY_SERVER_URL,
STORAGE_PREFIX,
defaultSessionStore,
Expand Down Expand Up @@ -374,9 +375,14 @@ export class SessionReplay implements AmplitudeSessionReplay {
}

getServerUrl() {
if (this.config?.serverZone === ServerZone.STAGING) {
return SESSION_REPLAY_STAGING_SERVER_URL;
}

if (this.config?.serverZone === ServerZone.EU) {
return SESSION_REPLAY_EU_SERVER_URL;
}

return SESSION_REPLAY_SERVER_URL;
}

Expand Down
22 changes: 21 additions & 1 deletion packages/session-replay-browser/test/session-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import * as AnalyticsClientCommon from '@amplitude/analytics-client-common';
import { LogLevel, Logger, ServerZone } from '@amplitude/analytics-types';
import * as RRWeb from '@amplitude/rrweb';
import * as IDBKeyVal from 'idb-keyval';
import { DEFAULT_SAMPLE_RATE, DEFAULT_SESSION_REPLAY_PROPERTY, SESSION_REPLAY_SERVER_URL } from '../src/constants';
import {
DEFAULT_SAMPLE_RATE,
DEFAULT_SESSION_REPLAY_PROPERTY,
SESSION_REPLAY_SERVER_URL,
SESSION_REPLAY_EU_URL,
SESSION_REPLAY_STAGING_URL,
} from '../src/constants';
import * as Helpers from '../src/helpers';
import { UNEXPECTED_ERROR_MESSAGE, getSuccessMessage } from '../src/messages';
import { SessionReplay } from '../src/session-replay';
Expand Down Expand Up @@ -997,6 +1003,20 @@ describe('SessionReplayPlugin', () => {
const sessionReplay = new SessionReplay();
expect(sessionReplay.getServerUrl()).toEqual(SESSION_REPLAY_SERVER_URL);
});

test('should return staging server url if staging config set', async () => {
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, { ...mockOptions, serverZone: ServerZone.STAGING }).promise;

expect(sessionReplay.getServerUrl()).toEqual(SESSION_REPLAY_STAGING_URL);
});

test('should return eu server url if eu config set', async () => {
const sessionReplay = new SessionReplay();
await sessionReplay.init(apiKey, { ...mockOptions, serverZone: ServerZone.EU }).promise;

expect(sessionReplay.getServerUrl()).toEqual(SESSION_REPLAY_EU_URL);
});
});

describe('getDeviceId', () => {
Expand Down

0 comments on commit bca3ddc

Please sign in to comment.