Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Sep 24, 2024
2 parents 594f88e + e5173f7 commit a2b0c13
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 23 deletions.
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.7.3"
implementation "com.posthog:posthog-android:3.7.4"
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import com.posthog.PostHog
import com.posthog.PostHogConfig
import com.posthog.android.PostHogAndroid
import com.posthog.android.PostHogAndroidConfig
import com.posthog.internal.PostHogPreferences
import com.posthog.internal.PostHogPreferences.Companion.ANONYMOUS_ID
import com.posthog.internal.PostHogPreferences.Companion.DISTINCT_ID
import com.posthog.internal.PostHogSessionManager
import java.util.UUID

Expand All @@ -20,13 +23,6 @@ class PosthogReactNativeSessionReplayModule(reactContext: ReactApplicationContex
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
fun multiply(a: Double, b: Double, promise: Promise) {
promise.resolve(a * b)
}

@ReactMethod
fun start(sessionId: String, sdkOptions: ReadableMap, sdkReplayConfig: ReadableMap, decideReplayConfig: ReadableMap, promise: Promise) {
val uuid = UUID.fromString(sessionId)
Expand All @@ -44,6 +40,9 @@ class PosthogReactNativeSessionReplayModule(reactContext: ReactApplicationContex

val endpoint = decideReplayConfig.getString("endpoint")

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

val config = PostHogAndroidConfig(apiKey, host).apply {
debug = debugValue
captureDeepLinks = false
Expand All @@ -62,6 +61,8 @@ class PosthogReactNativeSessionReplayModule(reactContext: ReactApplicationContex
}
PostHogAndroid.setup(context, config)

setIdentify(config.cachePreferences, distinctId, anonymousId)

promise.resolve(null)
}

Expand All @@ -84,6 +85,24 @@ class PosthogReactNativeSessionReplayModule(reactContext: ReactApplicationContex
promise.resolve(null)
}

@ReactMethod
fun identify(distinctId: String, anonymousId: String, promise: Promise) {
setIdentify(PostHog.getConfig<PostHogConfig>()?.cachePreferences, distinctId, anonymousId)

promise.resolve(null)
}

private fun setIdentify(cachePreferences: PostHogPreferences?, distinctId: String, anonymousId: String) {
cachePreferences?.let { preferences ->
if (anonymousId.isNotEmpty()) {
preferences.setValue(ANONYMOUS_ID, anonymousId)
}
if (distinctId.isNotEmpty()) {
preferences.setValue(DISTINCT_ID, distinctId)
}
}
}

companion object {
const val NAME = "PosthogReactNativeSessionReplay"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,10 @@
"-DFOLLY_CFG_NO_COROUTINES=1",
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
Expand Down Expand Up @@ -683,7 +686,10 @@
"-DFOLLY_CFG_NO_COROUTINES=1",
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down
9 changes: 5 additions & 4 deletions ios/PosthogReactNativeSessionReplay.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

@interface RCT_EXTERN_MODULE(PosthogReactNativeSessionReplay, NSObject)

RCT_EXTERN_METHOD(multiply:(float)a withB:(float)b
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(start:(NSString)sessionId
withSdkOptions:(NSDictionary)sdkOptions
withSdkReplayConfig:(NSDictionary)sdkReplayConfig
Expand All @@ -23,6 +19,11 @@ @interface RCT_EXTERN_MODULE(PosthogReactNativeSessionReplay, NSObject)
RCT_EXTERN_METHOD(endSession:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(identify:(NSString)distinctId
withAnonymousId:(NSString)anonymousId
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

+ (BOOL)requiresMainQueueSetup
{
return NO;
Expand Down
33 changes: 28 additions & 5 deletions ios/PosthogReactNativeSessionReplay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import PostHog
@objc(PosthogReactNativeSessionReplay)
class PosthogReactNativeSessionReplay: NSObject {

@objc(multiply:withB:withResolver:withRejecter:)
func multiply(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(a*b)
}

private var config: PostHogConfig?

@objc(start:withSdkOptions:withSdkReplayConfig:withDecideReplayConfig:withResolver:withRejecter:)
func start(sessionId: String, sdkOptions: [String: Any], sdkReplayConfig: [String: Any], decideReplayConfig: [String: Any], resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) {
let apiKey = sdkOptions["apiKey"] as? String ?? ""
Expand Down Expand Up @@ -41,8 +38,15 @@ class PosthogReactNativeSessionReplay: NSObject {
config.snapshotEndpoint = endpoint
}

let distinctId = sdkOptions["distinctId"] as? String ?? ""
let anonymousId = sdkOptions["anonymousId"] as? String ?? ""

PostHogSDK.shared.setup(config)

self.config = config

setIdentify(self.config?.storageManager, distinctId: distinctId, anonymousId: anonymousId)

resolve(nil)
}

Expand All @@ -64,4 +68,23 @@ class PosthogReactNativeSessionReplay: NSObject {
PostHogSDK.shared.endSession()
resolve(nil)
}

@objc(identify:withAnonymousId:withResolver:withRejecter:)
func identify(distinctId: String, anonymousId: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
setIdentify(self.config?.storageManager, distinctId: distinctId, anonymousId: anonymousId)

resolve(nil)
}

private func setIdentify(_ storageManager: PostHogStorageManager?, distinctId: String, anonymousId: String) {
guard let storageManager = storageManager else {
return
}
if !anonymousId.isEmpty {
storageManager.setAnonymousId(anonymousId)
}
if !distinctId.isEmpty {
storageManager.setDistinctId(distinctId)
}
}
}
14 changes: 10 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ const PosthogReactNativeSessionReplay =
}
);

export function multiply(a: number, b: number): Promise<number> {
return PosthogReactNativeSessionReplay.multiply(a, b);
}

export function start(
sessionId: string,
sdkOptions: { [key: string]: any },
Expand All @@ -48,6 +44,13 @@ export function isEnabled(): Promise<boolean> {
return PosthogReactNativeSessionReplay.isEnabled();
}

export function identify(
distinctId: string,
anonymousId: string
): Promise<void> {
return PosthogReactNativeSessionReplay.identify(distinctId, anonymousId);
}

export interface PostHogReactNativeSessionReplayModule {
start: (
sessionId: string,
Expand All @@ -61,13 +64,16 @@ export interface PostHogReactNativeSessionReplayModule {
endSession: () => Promise<void>;

isEnabled: () => Promise<boolean>;

identify: (distinctId: string, anonymousId: string) => Promise<void>;
}

const PostHogReactNativeSessionReplay: PostHogReactNativeSessionReplayModule = {
start,
startSession,
endSession,
isEnabled,
identify,
};

export default PostHogReactNativeSessionReplay;

0 comments on commit a2b0c13

Please sign in to comment.