Skip to content

Commit

Permalink
fix: allow passing sample rate and remote config fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
izaaz committed Oct 31, 2024
1 parent 54a097f commit 5f31002
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class PluginSessionReplayReactNativeModule(private val reactContext: ReactApplic
}

@ReactMethod
fun setup(apiKey: String, deviceId: String?, sessionId: Double) {
fun setup(apiKey: String, deviceId: String?, sessionId: Double, sampleRate: Double, enableRemoteConfig: Boolean) {
LogcatLogger.logger.logMode = Logger.LogMode.DEBUG
sessionReplay = SessionReplay(
apiKey,
reactContext.applicationContext,
deviceId ?: "",
sessionId.toLong(),
logger = LogcatLogger.logger,
sampleRate = 1.0,
enableRemoteConfig = false,
sampleRate = sampleRate,
enableRemoteConfig = enableRemoteConfig,
)
}

Expand Down
11 changes: 7 additions & 4 deletions packages/plugin-session-replay-react-native/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ import {
import { LogLevel } from '@amplitude/analytics-types';
import { NavigationContainer } from '@react-navigation/native';

import { SessionReplayPlugin } from '@amplitude/plugin-session-replay-react-native';
import { AmpMaskView } from '@amplitude/plugin-session-replay-react-native';
import { SessionReplayPlugin, AmpMaskView, SessionReplayConfig } from '@amplitude/plugin-session-replay-react-native';
import {
createNativeStackNavigator,
type NativeStackScreenProps,
Expand Down Expand Up @@ -239,11 +238,15 @@ const Stack = createNativeStackNavigator<RootStackParamList>();

function App(): React.JSX.Element {
useEffect(() => {
const config: SessionReplayConfig = {
enableRemoteConfig: false,
sampleRate: 1
};
(async () => {
await init('YOUR-API-KEY', 'example_user_id', {
await init('6151e18fcbce1f94010e8791964d2a71', 'example_user_id', {
logLevel: LogLevel.Verbose,
}).promise;
await add(new SessionReplayPlugin()).promise;
await add(new SessionReplayPlugin(config)).promise;
track('test');
await identify(new Identify().set('react-native-test', 'yes')).promise;
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@interface RCT_EXTERN_MODULE(PluginSessionReplayReactNative, NSObject)

RCT_EXTERN_METHOD(setup:(NSString)apiKey deviceId:(NSString)deviceId sessionId:(nonnull NSNumber)sessionId resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(setup:(NSString)apiKey deviceId:(NSString)deviceId sessionId:(nonnull NSNumber)sessionId sampleRate:(float)sampleRate enableRemoteConfig:(BOOL)enableRemoteConfig resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(setSessionId:(nonnull NSNumber) resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class PluginSessionReplayReactNative: NSObject {

var sessionReplay: SessionReplay!

@objc(setup:deviceId:sessionId:resolve:reject:)
func setup(_ apiKey: String, deviceId: String, sessionId: NSNumber, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
print("setup: \(apiKey) \(deviceId) \(sessionId)")
@objc(setup:deviceId:sessionId:sampleRate:enableRemoteConfig:resolve:reject:)
func setup(_ apiKey: String, deviceId: String, sessionId: NSNumber, sampleRate: Float, enableRemoteConfig: Bool, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
print("setup: API Key: \(apiKey) Device Id: \(deviceId) Session Id: \(sessionId) Sample Rate: \(sampleRate) Enable Remote Config: \(enableRemoteConfig)")
sessionReplay = SessionReplay(apiKey:apiKey,
deviceId: deviceId,
sessionId: sessionId.int64Value,
sampleRate: 1.0,
sampleRate: sampleRate,
logger:ConsoleLogger(logLevel: LogLevelEnum.DEBUG.rawValue),
enableRemoteConfig: false)
enableRemoteConfig: enableRemoteConfig)
sessionReplay.start()
resolve(nil)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-session-replay-react-native/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { SessionReplayPlugin } from './session-replay';

export { AmpMaskView } from './AmpMaskView';
export { AmpMaskView } from './app-mask-view';

export { SessionReplayConfig } from './session-replay-config';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface SessionReplayConfig {
sampleRate?: number;
enableRemoteConfig?: boolean;
}

export const getDefaultConfig = () => {
return {
sampleRate: 1,
enableRemoteConfig: true,
};
}
19 changes: 16 additions & 3 deletions packages/plugin-session-replay-react-native/src/session-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { EnrichmentPlugin, Event, ReactNativeClient, ReactNativeConfig } fr

import { PluginSessionReplayReactNative } from './native-module';
import { VERSION } from './version';
import { SessionReplayConfig, getDefaultConfig } from './session-replay-config';

export class SessionReplayPlugin implements EnrichmentPlugin<ReactNativeClient, ReactNativeConfig> {
name = '@amplitude/plugin-session-replay-react-native';
Expand All @@ -16,14 +17,26 @@ export class SessionReplayPlugin implements EnrichmentPlugin<ReactNativeClient,
// @ts-ignore
config: ReactNativeConfig;

constructor() {
// empty default constructor
sessionReplayConfig: SessionReplayConfig;

constructor(config: SessionReplayConfig = {}) {
this.sessionReplayConfig = {
...getDefaultConfig(),
...config,
};
console.log('Initializing SessionReplayPlugin with config: ', this.sessionReplayConfig);
}

async setup(config: ReactNativeConfig, _: ReactNativeClient): Promise<void> {
this.config = config;
console.log(`Installing @amplitude/plugin-session-replay-react-native, version ${VERSION}.`);
await PluginSessionReplayReactNative.setup(config.apiKey, config.deviceId, config.sessionId);
await PluginSessionReplayReactNative.setup(
config.apiKey,
config.deviceId,
config.sessionId,
this.sessionReplayConfig.sampleRate ?? 1,
this.sessionReplayConfig.enableRemoteConfig ?? true,
);
}

async execute(event: Event): Promise<Event | null> {
Expand Down

0 comments on commit 5f31002

Please sign in to comment.