Skip to content

Commit

Permalink
chore: forces the SDK to be initialized on the main thread (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Oct 25, 2024
1 parent 70f54a1 commit 166b7af
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Next

## 0.1.6 - 2024-10-25

- chore: forces the SDK to be initialized on the main thread

## 0.1.5 - 2024-10-18

- chore: target min iOS 13 in podspec
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.posthog:posthog-android:3.8.2"
implementation "com.posthog:posthog-android:3.8.3"
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.UiThreadUtil

import com.posthog.PostHog
import com.posthog.PostHogConfig
Expand All @@ -25,51 +26,60 @@ class PosthogReactNativeSessionReplayModule(reactContext: ReactApplicationContex

@ReactMethod
fun start(sessionId: String, sdkOptions: ReadableMap, sdkReplayConfig: ReadableMap, decideReplayConfig: ReadableMap, promise: Promise) {
val uuid = UUID.fromString(sessionId)
PostHogSessionManager.setSessionId(uuid)

val context = this.reactApplicationContext
val apiKey = sdkOptions.getString("apiKey") ?: ""
val host = sdkOptions.getString("host") ?: PostHogConfig.DEFAULT_HOST
val debugValue = sdkOptions.getBoolean("debug")

val maskAllTextInputs = sdkReplayConfig.getBoolean("maskAllTextInputs")
val maskAllImages = sdkReplayConfig.getBoolean("maskAllImages")
val captureLog = sdkReplayConfig.getBoolean("captureLog")
val debouncerDelayMs = sdkReplayConfig.getInt("androidDebouncerDelayMs")

val endpoint = decideReplayConfig.getString("endpoint")

val distinctId = sdkOptions.getString("distinctId") ?: ""
val anonymousId = sdkOptions.getString("anonymousId") ?: ""
val theSdkVersion = sdkOptions.getString("sdkVersion")

val config = PostHogAndroidConfig(apiKey, host).apply {
debug = debugValue
captureDeepLinks = false
captureApplicationLifecycleEvents = false
captureScreenViews = false
sessionReplay = true
sessionReplayConfig.screenshot = true
sessionReplayConfig.captureLogcat = captureLog
sessionReplayConfig.debouncerDelayMs = debouncerDelayMs.toLong()
sessionReplayConfig.maskAllImages = maskAllImages
sessionReplayConfig.maskAllTextInputs = maskAllTextInputs

if (!endpoint.isNullOrEmpty()) {
snapshotEndpoint = endpoint
val initRunnable = Runnable {
val uuid = UUID.fromString(sessionId)
PostHogSessionManager.setSessionId(uuid)

val context = this.reactApplicationContext
val apiKey = sdkOptions.getString("apiKey") ?: ""
val host = sdkOptions.getString("host") ?: PostHogConfig.DEFAULT_HOST
val debugValue = sdkOptions.getBoolean("debug")

val maskAllTextInputs = sdkReplayConfig.getBoolean("maskAllTextInputs")
val maskAllImages = sdkReplayConfig.getBoolean("maskAllImages")
val captureLog = sdkReplayConfig.getBoolean("captureLog")
val debouncerDelayMs = sdkReplayConfig.getInt("androidDebouncerDelayMs")

val endpoint = decideReplayConfig.getString("endpoint")

val distinctId = sdkOptions.getString("distinctId") ?: ""
val anonymousId = sdkOptions.getString("anonymousId") ?: ""
val theSdkVersion = sdkOptions.getString("sdkVersion")

val config = PostHogAndroidConfig(apiKey, host).apply {
debug = debugValue
captureDeepLinks = false
captureApplicationLifecycleEvents = false
captureScreenViews = false
sessionReplay = true
sessionReplayConfig.screenshot = true
sessionReplayConfig.captureLogcat = captureLog
sessionReplayConfig.debouncerDelayMs = debouncerDelayMs.toLong()
sessionReplayConfig.maskAllImages = maskAllImages
sessionReplayConfig.maskAllTextInputs = maskAllTextInputs

if (!endpoint.isNullOrEmpty()) {
snapshotEndpoint = endpoint
}

if (!theSdkVersion.isNullOrEmpty()) {
sdkName = "posthog-react-native"
sdkVersion = theSdkVersion
}
}
PostHogAndroid.setup(context, config)

if (!theSdkVersion.isNullOrEmpty()) {
sdkName = "posthog-react-native"
sdkVersion = theSdkVersion
}
}
PostHogAndroid.setup(context, config)
setIdentify(config.cachePreferences, distinctId, anonymousId)

setIdentify(config.cachePreferences, distinctId, anonymousId)
promise.resolve(null)
}

promise.resolve(null)
// forces the SDK to be initialized on the main thread
if (UiThreadUtil.isOnUiThread()) {
initRunnable.run()
} else {
UiThreadUtil.runOnUiThread(initRunnable)
}
}

@ReactMethod
Expand Down

0 comments on commit 166b7af

Please sign in to comment.