From 3ec8975a5ad81844816a63116562ac8daeab4b3d Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 21 Sep 2023 15:31:06 +0200 Subject: [PATCH] fixes --- .../posthog/android/sample/MainActivity.kt | 3 +- .../src/main/java/com/posthog/PostHog.kt | 52 ++++++++++++------- .../java/com/posthog/internal/PostHogQueue.kt | 9 ++-- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MainActivity.kt b/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MainActivity.kt index fcf5a10f..eb112ea7 100644 --- a/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MainActivity.kt +++ b/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MainActivity.kt @@ -11,7 +11,6 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.tooling.preview.Preview -import com.posthog.PostHog import com.posthog.android.sample.ui.theme.PostHogAndroidSampleTheme class MainActivity : ComponentActivity() { @@ -41,7 +40,7 @@ fun Greeting(name: String, modifier: Modifier = Modifier) { // PostHog.optOut() // PostHog.optIn() // PostHog.identify("my_distinct_id", properties = mapOf("my_property" to 1), userProperties = mapOf("name" to "hello")) - PostHog.capture("testEvent", mapOf("testProperty" to "testValue")) +// PostHog.capture("testEvent", mapOf("testProperty" to "testValue")) // PostHog.reloadFeatureFlagsRequest() // PostHog.isFeatureEnabled("sessionRecording") // val props = mutableMapOf() diff --git a/posthog-v3/posthog/src/main/java/com/posthog/PostHog.kt b/posthog-v3/posthog/src/main/java/com/posthog/PostHog.kt index d54e5a63..47e893bb 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/PostHog.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/PostHog.kt @@ -137,32 +137,39 @@ public class PostHog private constructor() { config?.cachePreferences?.setValue("distinctId", value) } - private fun buildProperties(distinctId: String, properties: Map?, userProperties: Map?, groupProperties: Map?): Map { + private fun buildProperties(distinctId: String, properties: Map?, userProperties: Map?, groupProperties: Map?, appendSharedProps: Boolean = true): Map { val props = mutableMapOf() - config?.context?.getStaticContext()?.let { + properties?.let { props.putAll(it) } - config?.context?.getDynamicContext()?.let { - props.putAll(it) - } + if (appendSharedProps) { + config?.context?.getStaticContext()?.let { + props.putAll(it) + } - if (config?.sendFeatureFlagEvent == true) { - featureFlags?.getFeatureFlags()?.let { - if (it.isNotEmpty()) { - val keys = mutableListOf() - for (entry in it.entries) { - props["\$feature/${entry.key}"] = entry.value - keys.add(entry.key) + config?.context?.getDynamicContext()?.let { + props.putAll(it) + } + + if (config?.sendFeatureFlagEvent == true) { + featureFlags?.getFeatureFlags()?.let { + if (it.isNotEmpty()) { + val keys = mutableListOf() + for (entry in it.entries) { + props["\$feature/${entry.key}"] = entry.value + keys.add(entry.key) + } + props["\$active_feature_flags"] = keys } - props["\$active_feature_flags"] = keys } } } // TODO: $set_once userProperties?.let { + // TODO: should this be person_properties to decide API? props["\$set"] = it } @@ -170,10 +177,6 @@ public class PostHog private constructor() { props["\$groups"] = it } - properties?.let { - props.putAll(it) - } - // only set if not there. props["distinct_id"]?.let { props["distinct_id"] = distinctId @@ -249,19 +252,25 @@ public class PostHog private constructor() { return } - val oldDistinctId = this.distinctId + val previousDistinctId = this.distinctId val props = mutableMapOf() props["\$anon_distinct_id"] = anonymousId props["distinct_id"] = distinctId + // TODO: do we append groups? + properties?.let { props.putAll(it) } capture("\$identify", properties = props, userProperties = userProperties) - if (oldDistinctId != distinctId) { + if (previousDistinctId != distinctId) { + // We keep the AnonymousId to be used by decide calls and identify to link the previousId + this.anonymousId = previousDistinctId + this.distinctId = distinctId + reloadFeatureFlagsRequest() } } @@ -286,6 +295,8 @@ public class PostHog private constructor() { } newGroups[type] = key preferences.setValue("\$groups", newGroups) + + // TODO: if the group does not exist yet, should we reload the Feature flags? } capture("\$groupidentify", properties = props) @@ -302,10 +313,11 @@ public class PostHog private constructor() { val props = mutableMapOf() props["\$anon_distinct_id"] = anonymousId props["distinct_id"] = distinctId + // TODO: person_properties, group_properties val groups = config?.memoryPreferences?.getValue("\$groups") as? Map - featureFlags?.loadFeatureFlags(buildProperties(distinctId, props, null, groups)) + featureFlags?.loadFeatureFlags(buildProperties(distinctId, props, null, groups, appendSharedProps = false)) } public fun isFeatureEnabled(key: String, defaultValue: Boolean = false): Boolean { diff --git a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt index a782f754..87b73b4f 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt @@ -268,13 +268,14 @@ internal class PostHogQueue(private val config: PostHogConfig, private val api: fun clear() { executor.execute { + val tempFiles: List synchronized(dequeLock) { - // TODO: probably have to sync due to timers - deque.forEach { - it.delete() - } + tempFiles = deque.toList() deque.clear() } + tempFiles.forEach { + it.delete() + } } } }