Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow passing sample rate and remote config fetching #911

Merged
merged 3 commits into from
Oct 31, 2024
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
7 changes: 6 additions & 1 deletion packages/plugin-session-replay-react-native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ import { SessionReplayPlugin } from '@amplitude/plugin-session-replay-react-nati

// ...

const config: SessionReplayConfig = {
enableRemoteConfig: true, // default true
sampleRate: 1, // default 0
};
await init('YOUR_API_KEY').promise;
await add(new SessionReplayPlugin()).promise;
await add(new SessionReplayPlugin(config)).promise;

```


## Masking views
To maks certain views, add the `AmpMaskView` tag with the mask property `amp-mask` around the section to be masked

Expand Down
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
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', {
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: 0,
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
Loading