Skip to content

Commit

Permalink
chore: sdk info in recordings (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Sep 12, 2024
1 parent 725172f commit e797bd0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ internal class PostHogAndroidContext(
private val context: Context,
private val config: PostHogAndroidConfig,
) : PostHogContext {
private val cacheSdkInfo by lazy {
val sdkInfo = mutableMapOf<String, Any>()

sdkInfo["\$lib"] = config.sdkName
sdkInfo["\$lib_version"] = config.sdkVersion

sdkInfo
}

private val cacheStaticContext by lazy {
val staticContext = mutableMapOf<String, Any>()

Expand Down Expand Up @@ -57,9 +66,6 @@ internal class PostHogAndroidContext(
staticContext["\$os_name"] = "Android"
staticContext["\$os_version"] = Build.VERSION.RELEASE

staticContext["\$lib"] = config.sdkName
staticContext["\$lib_version"] = config.sdkVersion

staticContext["\$is_emulator"] = isEmulator

staticContext
Expand Down Expand Up @@ -191,6 +197,10 @@ internal class PostHogAndroidContext(
return dynamicContext
}

override fun getSdkInfo(): Map<String, Any> {
return cacheSdkInfo
}

// Inspired from https://github.com/fluttercommunity/plus_plugins/blob/a71a27c5fbdbbfc56a30359a1aff0a3d3da8dc73/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/MethodCallHandlerImpl.kt#L105-L123
private val isEmulator: Boolean
get() = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,17 @@ internal class PostHogAndroidContextTest {
// its dynamic
assertNotNull(staticContext["\$os_version"])

assertEquals(config.sdkName, staticContext["\$lib"])
assertEquals(config.sdkVersion, staticContext["\$lib_version"])

assertNotNull(staticContext["\$is_emulator"])
}

fun `returns sdk info`() {
val sut = getSut()
val sdkInfo = sut.getSdkInfo()

assertEquals(config.sdkName, sdkInfo["\$lib"])
assertEquals(config.sdkVersion, sdkInfo["\$lib_version"])
}

@Test
fun `returns dynamic context`() {
val sut = getSut()
Expand Down
1 change: 1 addition & 0 deletions posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public final class com/posthog/internal/PersonProfiles : java/lang/Enum {

public abstract interface class com/posthog/internal/PostHogContext {
public abstract fun getDynamicContext ()Ljava/util/Map;
public abstract fun getSdkInfo ()Ljava/util/Map;
public abstract fun getStaticContext ()Ljava/util/Map;
}

Expand Down
42 changes: 24 additions & 18 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,36 @@ public class PostHog private constructor(
}
}

userProperties?.let {
props["\$set"] = it
}

userPropertiesSetOnce?.let {
props["\$set_once"] = it
}

if (appendGroups) {
// merge groups
mergeGroups(groups)?.let {
props["\$groups"] = it
}
}

props["\$is_identified"] = isIdentified

props["\$process_person_profile"] = hasPersonProcessing()
}

// Session replay should have the SDK info as well
config?.context?.getSdkInfo()?.let {
props.putAll(it)
}

PostHogSessionManager.getActiveSessionId()?.let { sessionId ->
val tempSessionId = sessionId.toString()
props["\$session_id"] = tempSessionId
if (config?.sessionReplay == true) {
// only Session replay needs $window_id
if (!appendSharedProps && config?.sessionReplay == true) {
// Session replay requires $window_id, so we set as the same as $session_id.
// the backend might fallback to $session_id if $window_id is not present next.
props["\$window_id"] = tempSessionId
Expand All @@ -337,22 +358,7 @@ public class PostHog private constructor(
props.putAll(it)
}

userProperties?.let {
props["\$set"] = it
}

userPropertiesSetOnce?.let {
props["\$set_once"] = it
}

if (appendGroups) {
// merge groups
mergeGroups(groups)?.let {
props["\$groups"] = it
}
}

// Replay needs distinct_id also in the props
// only Session replay needs distinct_id also in the props
// remove after https://github.com/PostHog/posthog/pull/18954 gets merged
val propDistinctId = props["distinct_id"] as? String
if (!appendSharedProps && config?.sessionReplay == true && propDistinctId.isNullOrBlank()) {
Expand Down Expand Up @@ -430,7 +436,7 @@ public class PostHog private constructor(
groups = groups,
// only append shared props if not a snapshot event
appendSharedProps = !snapshotEvent,
// only append groups if not a group identify event
// only append groups if not a group identify event and not a snapshot
appendGroups = !groupIdentify,
)

Expand Down
2 changes: 2 additions & 0 deletions posthog/src/main/java/com/posthog/internal/PostHogContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface PostHogContext {
public fun getStaticContext(): Map<String, Any>

public fun getDynamicContext(): Map<String, Any>

public fun getSdkInfo(): Map<String, Any>
}

0 comments on commit e797bd0

Please sign in to comment.