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

chore: forces the SDK to be initialized on the main thread #11

Merged
merged 3 commits into from
Oct 25, 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
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
Loading