Skip to content

Commit

Permalink
feat(analytics-remote-config): add string type to sessionId for custo…
Browse files Browse the repository at this point in the history
…m session id
  • Loading branch information
junjie-amplitude committed Dec 6, 2024
1 parent ec1f760 commit 45e131c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
10 changes: 5 additions & 5 deletions packages/analytics-remote-config/src/remote-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>
localConfig: Config;
retryTimeout = 1000;
attempts = 0;
lastFetchedSessionId: number | undefined;
lastFetchedSessionId: number | string | undefined;
sessionTargetingMatch = false;
configKeys: string[];
metrics: RemoteConfigMetric = {};
Expand All @@ -36,7 +36,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>
getRemoteConfig = async <K extends keyof RemoteConfig>(
configNamespace: string,
key: K,
sessionId?: number,
sessionId?: number | string,
): Promise<RemoteConfig[K] | undefined> => {
const fetchStartTime = Date.now();
// Finally fetch via API
Expand Down Expand Up @@ -64,7 +64,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>
return REMOTE_CONFIG_SERVER_URL;
}

fetchWithTimeout = async (sessionId?: number): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
fetchWithTimeout = async (sessionId?: number | string): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const remoteConfig = await this.fetchRemoteConfig(controller.signal, sessionId);
Expand All @@ -74,7 +74,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>

fetchRemoteConfig = async (
signal: AbortController['signal'],
sessionId?: number,
sessionId?: number | string,
): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
if (sessionId === this.lastFetchedSessionId && this.attempts >= this.localConfig.flushMaxRetries) {
return this.completeRequest({ err: MAX_RETRIES_EXCEEDED_MESSAGE });
Expand Down Expand Up @@ -128,7 +128,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>

retryFetch = async (
signal: AbortController['signal'],
sessionId?: number,
sessionId?: number | string,
): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
await new Promise((resolve) => setTimeout(resolve, this.attempts * this.retryTimeout));
return this.fetchRemoteConfig(signal, sessionId);
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-remote-config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface BaseRemoteConfigFetch<T> {
getRemoteConfig: <K extends keyof T>(
configNamespace: string,
key: K,
sessionId?: number,
sessionId?: number | string,
) => Promise<T[K] | undefined>;
}

Expand Down
20 changes: 16 additions & 4 deletions packages/session-replay-browser/src/config/joined-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class SessionReplayJoinedConfigGenerator {
this.remoteConfigFetch = remoteConfigFetch;
}

async generateJoinedConfig(_sessionId?: string | number): Promise<SessionReplayJoinedConfig> {
async generateJoinedConfig(sessionId?: string | number): Promise<SessionReplayJoinedConfig> {
const config: SessionReplayJoinedConfig = { ...this.localConfig };
// Special case here as optOut is implemented via getter/setter
config.optOut = this.localConfig.optOut;
Expand All @@ -55,12 +55,24 @@ export class SessionReplayJoinedConfigGenerator {
config.captureEnabled = true;
let remoteConfig: SessionReplayRemoteConfig | undefined;
try {
const samplingConfig = await this.remoteConfigFetch.getRemoteConfig('sessionReplay', 'sr_sampling_config');
const samplingConfig = await this.remoteConfigFetch.getRemoteConfig(
'sessionReplay',
'sr_sampling_config',
sessionId,
);

const privacyConfig = await this.remoteConfigFetch.getRemoteConfig('sessionReplay', 'sr_privacy_config');
const privacyConfig = await this.remoteConfigFetch.getRemoteConfig(
'sessionReplay',
'sr_privacy_config',
sessionId,
);

// This is intentionally forced to only be set through the remote config.
config.interactionConfig = await this.remoteConfigFetch.getRemoteConfig('sessionReplay', 'sr_interaction_config');
config.interactionConfig = await this.remoteConfigFetch.getRemoteConfig(
'sessionReplay',
'sr_interaction_config',
sessionId,
);

if (samplingConfig || privacyConfig) {
remoteConfig = {};
Expand Down
2 changes: 0 additions & 2 deletions packages/session-replay-browser/src/events/events-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export const createEventsManager = async <Type extends EventType>({
events: string[];
sessionId: string | number;
deviceId: string;
customSessionId?: string;
sequenceId?: number;
}) => {
if (config.debugMode) {
Expand Down Expand Up @@ -138,7 +137,6 @@ export const createEventsManager = async <Type extends EventType>({
event: { type: Type; data: string };
sessionId: number;
deviceId: string;
customSessionId: string | null;
}) => {
store
.addEventToCurrentSequence(sessionId, event.data)
Expand Down

0 comments on commit 45e131c

Please sign in to comment.