From 8dc67a745f7d60031f7629baa8d84353dc841b6e Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 19 Sep 2023 14:51:05 +0200 Subject: [PATCH] fix issues --- .../com/posthog/android/PostHogAndroid.kt | 2 + .../internal/PostHogAdvertisingIdReader.kt | 19 ---- .../internal/PostHogSharedPreferences.kt | 34 ++++++- .../posthog/android/sample/MainActivity.kt | 7 +- .../java/com/posthog/android/sample/MyApp.kt | 2 +- posthog-v3/posthog/api/posthog.api | 45 +++++++--- .../src/main/java/com/posthog/PostHog.kt | 89 ++++++++++++++----- .../main/java/com/posthog/PostHogConfig.kt | 9 ++ .../src/main/java/com/posthog/PostHogEvent.kt | 7 ++ .../java/com/posthog/PostHogPreferences.kt | 8 +- .../com/posthog/internal/PostHogBatchEvent.kt | 1 - .../internal/PostHogMemoryPreferences.kt | 19 ++++ .../java/com/posthog/internal/PostHogQueue.kt | 16 ++++ .../test/resources/json/batch-request.json | 1 + 14 files changed, 199 insertions(+), 60 deletions(-) delete mode 100644 posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogAdvertisingIdReader.kt create mode 100644 posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogMemoryPreferences.kt diff --git a/posthog-v3/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt b/posthog-v3/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt index 2f771778..c72a311b 100644 --- a/posthog-v3/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt +++ b/posthog-v3/posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt @@ -8,6 +8,7 @@ import com.posthog.PostHogPrintLogger import com.posthog.android.internal.PostHogActivityLifecycleCallback import com.posthog.android.internal.PostHogAndroidContext import com.posthog.android.internal.PostHogAndroidLogger +import com.posthog.android.internal.PostHogSharedPreferences import java.io.File public class PostHogAndroid private constructor() { @@ -35,6 +36,7 @@ public class PostHogAndroid private constructor() { val path = File(context.cacheDir, "posthog-disk-queue") config.legacyStoragePrefix = config.legacyStoragePrefix ?: legacyPath.absolutePath config.storagePrefix = config.storagePrefix ?: path.absolutePath + config.preferences = config.preferences ?: PostHogSharedPreferences(context, config) if (context is Application) { config.integrations.add(PostHogActivityLifecycleCallback(context)) diff --git a/posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogAdvertisingIdReader.kt b/posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogAdvertisingIdReader.kt deleted file mode 100644 index b7f9dabd..00000000 --- a/posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogAdvertisingIdReader.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.posthog.android.internal - -import android.content.Context - -// GetAdvertisingIdTask, Utils -internal class PostHogAdvertisingIdReader(context: Context) { - fun getGooglePlayServicesAdvertisingID(): Pair? { - return null - } - - fun getAmazonFireAdvertisingID(): Pair? { - return null - } - - fun getDeviceId(): String? { - // TODO depends on DEFAULT_COLLECT_DEVICE_ID - return null - } -} diff --git a/posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogSharedPreferences.kt b/posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogSharedPreferences.kt index 01b5cb26..f5d94a2d 100644 --- a/posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogSharedPreferences.kt +++ b/posthog-v3/posthog-android/src/main/java/com/posthog/android/internal/PostHogSharedPreferences.kt @@ -3,19 +3,45 @@ package com.posthog.android.internal import android.content.Context import android.content.Context.MODE_PRIVATE import com.posthog.PostHogConfig +import com.posthog.PostHogPreferences -internal class PostHogSharedPreferences(context: Context, config: PostHogConfig) { +internal class PostHogSharedPreferences(context: Context, config: PostHogConfig) : + PostHogPreferences { private val sharedPreferences = context.getSharedPreferences("posthog-android-${config.apiKey}", MODE_PRIVATE) private val packageInfo = getPackageInfo(context, config) - fun getValue(key: String, defaultValue: Any? = null): Any? { + override fun getValue(key: String, defaultValue: Any?): Any? { return sharedPreferences.all[key] ?: defaultValue } - fun setValue(key: String, value: String) { + override fun setValue(key: String, value: Any) { val edit = sharedPreferences.edit() - edit.putString(key, value) + + when (value) { + is Boolean -> { + edit.putBoolean(key, value) + } + is String -> { + edit.putString(key, value) + } + is Float -> { + edit.putFloat(key, value) + } + is Long -> { + edit.putLong(key, value) + } + is Int -> { + edit.putInt(key, value) + } + } + + edit.apply() + } + + override fun clear() { + val edit = sharedPreferences.edit() + edit.clear() edit.apply() } 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 8d9f7efb..6a46d965 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 @@ -38,8 +38,11 @@ fun Greeting(name: String, modifier: Modifier = Modifier) { text = AnnotatedString("Hello $name!"), modifier = modifier, onClick = { -// PostHog.identify("my_distinct_id", properties = mapOf("my_property" to 1), userProperties = mapOf("name" to "hello")) - PostHog.capture("testEvent", mapOf("testProperty" to "testValue")) +// PostHog.optOut() + PostHog.identify("my_distinct_id", properties = mapOf("my_property" to 1), userProperties = mapOf("name" to "hello")) +// PostHog.capture("testEvent", mapOf("testProperty" to "testValue")) +// PostHog.optIn() +// PostHog.capture("testEvent", mapOf("testProperty" to "testValue")) // PostHog.reloadFeatureFlagsRequest() // PostHog.isFeatureEnabled("sessionRecording") // PostHog.flush() diff --git a/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MyApp.kt b/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MyApp.kt index b0ea92c2..efb6f096 100644 --- a/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MyApp.kt +++ b/posthog-v3/posthog-samples/posthog-android-sample/src/main/java/com/posthog/android/sample/MyApp.kt @@ -10,7 +10,7 @@ class MyApp : Application() { val config = PostHogConfig("_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI").apply { debug = true - flushAt = 5 +// flushAt = 5 // flushIntervalSeconds = 5 // flushAt = 1 } diff --git a/posthog-v3/posthog/api/posthog.api b/posthog-v3/posthog/api/posthog.api index 50f5ffee..33a81bbd 100644 --- a/posthog-v3/posthog/api/posthog.api +++ b/posthog-v3/posthog/api/posthog.api @@ -2,8 +2,8 @@ public final class com/posthog/PostHog { public static final field Companion Lcom/posthog/PostHog$Companion; public final fun alias (Ljava/lang/String;Ljava/util/Map;)V public static synthetic fun alias$default (Lcom/posthog/PostHog;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V - public final fun capture (Ljava/lang/String;Ljava/util/Map;)V - public static synthetic fun capture$default (Lcom/posthog/PostHog;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V + public final fun capture (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V + public static synthetic fun capture$default (Lcom/posthog/PostHog;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V public final fun close ()V public final fun flush ()V public final fun getAnonymousId ()Ljava/lang/String; @@ -17,16 +17,20 @@ public final class com/posthog/PostHog { public static synthetic fun identify$default (Lcom/posthog/PostHog;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V public final fun isFeatureEnabled (Ljava/lang/String;Z)Z public static synthetic fun isFeatureEnabled$default (Lcom/posthog/PostHog;Ljava/lang/String;ZILjava/lang/Object;)Z + public final fun optIn ()V + public final fun optOut ()V + public final fun register (Ljava/lang/String;Ljava/lang/Object;)V public final fun reloadFeatureFlagsRequest ()V public final fun reset ()V public final fun screen (Ljava/lang/String;Ljava/util/Map;)V public static synthetic fun screen$default (Lcom/posthog/PostHog;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V public final fun setup (Lcom/posthog/PostHogConfig;)V + public final fun unregister (Ljava/lang/String;)V } public final class com/posthog/PostHog$Companion { - public final fun capture (Ljava/lang/String;Ljava/util/Map;)V - public static synthetic fun capture$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)V + public final fun capture (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V + public static synthetic fun capture$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V public final fun close ()V public final fun flush ()V public final fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; @@ -37,6 +41,8 @@ public final class com/posthog/PostHog$Companion { public static synthetic fun identify$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V public final fun isFeatureEnabled (Ljava/lang/String;Z)Z public static synthetic fun isFeatureEnabled$default (Lcom/posthog/PostHog$Companion;Ljava/lang/String;ZILjava/lang/Object;)Z + public final fun optIn ()V + public final fun optOut ()V public final fun reloadFeatureFlagsRequest ()V public final fun reset ()V public final fun setup (Lcom/posthog/PostHogConfig;)V @@ -44,12 +50,13 @@ public final class com/posthog/PostHog$Companion { } public final class com/posthog/PostHogConfig { - public fun (Ljava/lang/String;Ljava/lang/String;ZIIIILcom/posthog/PostHogDataMode;Lcom/posthog/PostHogEncryption;Ljava/util/List;)V - public synthetic fun (Ljava/lang/String;Ljava/lang/String;ZIIIILcom/posthog/PostHogDataMode;Lcom/posthog/PostHogEncryption;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogDataMode;Lcom/posthog/PostHogEncryption;Ljava/util/List;)V + public synthetic fun (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogDataMode;Lcom/posthog/PostHogEncryption;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getApiKey ()Ljava/lang/String; public final fun getContext ()Lcom/posthog/PostHogContext; public final fun getDataMode ()Lcom/posthog/PostHogDataMode; public final fun getDebug ()Z + public final fun getEnable ()Z public final fun getEncryption ()Lcom/posthog/PostHogEncryption; public final fun getFlushAt ()I public final fun getFlushIntervalSeconds ()I @@ -59,10 +66,14 @@ public final class com/posthog/PostHogConfig { public final fun getLogger ()Lcom/posthog/PostHogLogger; public final fun getMaxBatchSize ()I public final fun getMaxQueueSize ()I + public final fun getPreferences ()Lcom/posthog/PostHogPreferences; + public final fun getPreloadFeatureFlags ()Z + public final fun getSendFeatureFlagEvent ()Z public final fun getStoragePrefix ()Ljava/lang/String; public final fun setContext (Lcom/posthog/PostHogContext;)V public final fun setDataMode (Lcom/posthog/PostHogDataMode;)V public final fun setDebug (Z)V + public final fun setEnable (Z)V public final fun setEncryption (Lcom/posthog/PostHogEncryption;)V public final fun setFlushAt (I)V public final fun setFlushIntervalSeconds (I)V @@ -70,6 +81,9 @@ public final class com/posthog/PostHogConfig { public final fun setLogger (Lcom/posthog/PostHogLogger;)V public final fun setMaxBatchSize (I)V public final fun setMaxQueueSize (I)V + public final fun setPreferences (Lcom/posthog/PostHogPreferences;)V + public final fun setPreloadFeatureFlags (Z)V + public final fun setSendFeatureFlagEvent (Z)V public final fun setStoragePrefix (Ljava/lang/String;)V } @@ -92,17 +106,21 @@ public abstract class com/posthog/PostHogEncryption { } public final class com/posthog/PostHogEvent { - public fun (Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;)V - public synthetic fun (Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;Ljava/util/UUID;Ljava/util/Map;)V + public synthetic fun (Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;Ljava/util/UUID;Ljava/util/Map;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun component1 ()Ljava/lang/String; public final fun component2 ()Ljava/util/Map; public final fun component3 ()Ljava/util/Date; - public final fun copy (Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;)Lcom/posthog/PostHogEvent; - public static synthetic fun copy$default (Lcom/posthog/PostHogEvent;Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;ILjava/lang/Object;)Lcom/posthog/PostHogEvent; + public final fun component4 ()Ljava/util/UUID; + public final fun component5 ()Ljava/util/Map; + public final fun copy (Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;Ljava/util/UUID;Ljava/util/Map;)Lcom/posthog/PostHogEvent; + public static synthetic fun copy$default (Lcom/posthog/PostHogEvent;Ljava/lang/String;Ljava/util/Map;Ljava/util/Date;Ljava/util/UUID;Ljava/util/Map;ILjava/lang/Object;)Lcom/posthog/PostHogEvent; public fun equals (Ljava/lang/Object;)Z public final fun getEvent ()Ljava/lang/String; public final fun getProperties ()Ljava/util/Map; public final fun getTimestamp ()Ljava/util/Date; + public final fun getUserProperties ()Ljava/util/Map; + public final fun getUuid ()Ljava/util/UUID; public fun hashCode ()I public fun toString ()Ljava/lang/String; } @@ -124,6 +142,13 @@ public abstract interface class com/posthog/PostHogLogger { } public abstract interface class com/posthog/PostHogPreferences { + public abstract fun clear ()V + public abstract fun getValue (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; + public abstract fun setValue (Ljava/lang/String;Ljava/lang/Object;)V +} + +public final class com/posthog/PostHogPreferences$DefaultImpls { + public static synthetic fun getValue$default (Lcom/posthog/PostHogPreferences;Ljava/lang/String;Ljava/lang/Object;ILjava/lang/Object;)Ljava/lang/Object; } public final class com/posthog/PostHogPrintLogger : com/posthog/PostHogLogger { 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 f2b4dc37..f7fd5526 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/PostHog.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/PostHog.kt @@ -2,14 +2,13 @@ package com.posthog import com.posthog.internal.PostHogApi import com.posthog.internal.PostHogFeatureFlags +import com.posthog.internal.PostHogMemoryPreferences import com.posthog.internal.PostHogQueue import com.posthog.internal.PostHogSerializer import com.posthog.internal.PostHogSessionManager import com.posthog.internal.PostHogStorage import com.posthog.internal.SendCachedEventsIntegration -// TODO: register/unregister? (Super Property) https://posthog.com/docs/libraries/react-native - public class PostHog private constructor() { @Volatile private var enabled = false @@ -23,14 +22,13 @@ public class PostHog private constructor() { private var api: PostHogApi? = null private var queue: PostHogQueue? = null private var context: PostHogContext? = null - private var serializer: PostHogSerializer? = null // TODO: flushTimer, reachability, flagCallReported public fun setup(config: PostHogConfig) { synchronized(lock) { if (enabled) { - println("Setup called despite already being setup!") + config.logger.log("Setup called despite already being setup!") return } @@ -40,6 +38,12 @@ public class PostHog private constructor() { val api = PostHogApi(config, serializer) val queue = PostHogQueue(config, storage, api, serializer) val featureFlags = PostHogFeatureFlags(config, api) + config.preferences = config.preferences ?: PostHogMemoryPreferences() + + val enable = config.preferences?.getValue("opt-out", defaultValue = config.enable) as? Boolean + enable?.let { + config.enable = enable + } val sendCachedEventsIntegration = SendCachedEventsIntegration(config, api, serializer) @@ -59,7 +63,7 @@ public class PostHog private constructor() { queue.start() // TODO: guarded by preloadFeatureFlags -// loadFeatureFlagsRequest() + loadFeatureFlagsRequest() } } @@ -97,7 +101,6 @@ public class PostHog private constructor() { properties?.let { props.putAll(it) } - // TODO: message_id=messageId context?.getStaticContext()?.let { props.putAll(it) @@ -115,18 +118,44 @@ public class PostHog private constructor() { return props } - // TODO: optIn/enable? - // test: $merge_dangerously - public fun capture(event: String, properties: Map? = null) { + public fun capture(event: String, properties: Map? = null, userProperties: Map? = null) { if (!isEnabled()) { return } + if (config?.enable == false) { + config?.logger?.log("PostHog is in OptOut state.") + return + } - val postHogEvent = PostHogEvent(event, buildProperties(properties)) + val postHogEvent = PostHogEvent(event, buildProperties(properties), userProperties = userProperties) queue?.add(postHogEvent) } + public fun optIn() { + if (!isEnabled()) { + return + } + + config?.enable = true + config?.preferences?.setValue("opt-out", true) + } + + public fun optOut() { + if (!isEnabled()) { + return + } + + config?.enable = false + config?.preferences?.setValue("opt-out", false) + } + + public fun register(key: String, value: Any) { + } + + public fun unregister(key: String) { + } + public fun screen(screenTitle: String, properties: Map? = null) { if (!isEnabled()) { return @@ -174,14 +203,8 @@ public class PostHog private constructor() { properties?.let { props.putAll(it) } - userProperties?.let { - // Should $set be its own data class? - props["\$set"] = it - } - // TODO: should $set_once also exist? it does not exist on Android - - capture("\$identify", properties = props) + capture("\$identify", properties = props, userProperties = userProperties) } public fun group(type: String, key: String, properties: Map? = null) { @@ -241,7 +264,8 @@ public class PostHog private constructor() { if (!isEnabled()) { return } - // TODO: reset stuff, delete cache + config?.preferences?.clear() + queue?.clear() } // TODO: groups, groupIdentify, group, feature flags, buildProperties (static context, dynamic context, distinct_id) @@ -257,23 +281,38 @@ public class PostHog private constructor() { // TODO: make it private and rely only on static methods that forward to shared? private val shared: PostHog = PostHog() + private val apiKeys = mutableSetOf() + // TODO: understand why with was used to return the custom instance public fun with(config: PostHogConfig): PostHog { + logIfApiKeyExists(config) + val instance = PostHog() instance.setup(config) return instance } + private fun logIfApiKeyExists(config: PostHogConfig) { + if (apiKeys.contains(config.apiKey)) { + config.logger.log("API Key: ${config.apiKey} already has a PostHog instance.") + } + } + public fun setup(config: PostHogConfig) { + logIfApiKeyExists(config) + shared.setup(config) } public fun close() { + shared.config?.let { + apiKeys.remove(it.apiKey) + } shared.close() } - public fun capture(event: String, properties: Map? = null) { - shared.capture(event, properties = properties) + public fun capture(event: String, properties: Map? = null, userProperties: Map? = null) { + shared.capture(event, properties = properties, userProperties = userProperties) } public fun identify(distinctId: String, properties: Map? = null, userProperties: Map? = null) { @@ -304,8 +343,14 @@ public class PostHog private constructor() { shared.reset() } - // TODO: add other methods + public fun optIn() { + shared.optIn() + } + + public fun optOut() { + shared.optOut() + } - // DISCUSS: Middleware, what does it stand for? + // TODO: add other methods } } diff --git a/posthog-v3/posthog/src/main/java/com/posthog/PostHogConfig.kt b/posthog-v3/posthog/src/main/java/com/posthog/PostHogConfig.kt index 2653ab11..1dea4f6f 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/PostHogConfig.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/PostHogConfig.kt @@ -1,10 +1,16 @@ package com.posthog +// TODO: bootstrap + public class PostHogConfig( // apiKey and host are immutable due to offline caching public val apiKey: String, public val host: String = "https://app.posthog.com", public var debug: Boolean = false, + public var enable: Boolean = true, + public var sendFeatureFlagEvent: Boolean = true, + public var preloadFeatureFlags: Boolean = true, + // min. allowed is 1 public var flushAt: Int = 20, public var maxQueueSize: Int = 1000, public var maxBatchSize: Int = 10, @@ -28,4 +34,7 @@ public class PostHogConfig( @PostHogInternal public var storagePrefix: String? = null + + @PostHogInternal + public var preferences: PostHogPreferences? = null } diff --git a/posthog-v3/posthog/src/main/java/com/posthog/PostHogEvent.kt b/posthog-v3/posthog/src/main/java/com/posthog/PostHogEvent.kt index 8a1ca038..eb52606a 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/PostHogEvent.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/PostHogEvent.kt @@ -1,9 +1,16 @@ package com.posthog +import com.google.gson.annotations.SerializedName import java.util.Date +import java.util.UUID public data class PostHogEvent( val event: String, val properties: Map, val timestamp: Date = Date(), + val uuid: UUID = UUID.randomUUID(), + @SerializedName("\$set") + val userProperties: Map? = null, +// @SerializedName("\$set_once") +// val setOnce: Map? = null, ) diff --git a/posthog-v3/posthog/src/main/java/com/posthog/PostHogPreferences.kt b/posthog-v3/posthog/src/main/java/com/posthog/PostHogPreferences.kt index 0c2bfc2a..3757c80b 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/PostHogPreferences.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/PostHogPreferences.kt @@ -1,4 +1,10 @@ package com.posthog // Properties -public interface PostHogPreferences +public interface PostHogPreferences { + public fun getValue(key: String, defaultValue: Any? = null): Any? + + public fun setValue(key: String, value: Any) + + public fun clear() +} diff --git a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogBatchEvent.kt b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogBatchEvent.kt index 9b014103..26a73ecd 100644 --- a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogBatchEvent.kt +++ b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogBatchEvent.kt @@ -4,7 +4,6 @@ import com.google.gson.annotations.SerializedName import com.posthog.PostHogEvent import java.util.Date -// TODO: message_id internal data class PostHogBatchEvent( @SerializedName("api_key") val apiKey: String, diff --git a/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogMemoryPreferences.kt b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogMemoryPreferences.kt new file mode 100644 index 00000000..066f4a23 --- /dev/null +++ b/posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogMemoryPreferences.kt @@ -0,0 +1,19 @@ +package com.posthog.internal + +import com.posthog.PostHogPreferences + +internal class PostHogMemoryPreferences : PostHogPreferences { + private val preferences = mutableMapOf() + + override fun getValue(key: String, defaultValue: Any?): Any? { + return preferences[key] ?: defaultValue + } + + override fun setValue(key: String, value: Any) { + preferences[key] = value + } + + override fun clear() { + preferences.clear() + } +} 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 fe774143..a5c93f7d 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 @@ -142,6 +142,10 @@ internal class PostHogQueue(private val config: PostHogConfig, private val stora synchronized(dequeLock) { deque.removeAll(files) } + + files.forEach { + it.delete() + } } fun flush() { @@ -212,4 +216,16 @@ internal class PostHogQueue(private val config: PostHogConfig, private val stora stopTimer() } } + + fun clear() { + executor.execute { + synchronized(dequeLock) { + // TODO: probably have to sync due to timers + deque.forEach { + it.delete() + } + deque.clear() + } + } + } } diff --git a/posthog-v3/posthog/src/test/resources/json/batch-request.json b/posthog-v3/posthog/src/test/resources/json/batch-request.json index fe1881f2..8f2a88a2 100644 --- a/posthog-v3/posthog/src/test/resources/json/batch-request.json +++ b/posthog-v3/posthog/src/test/resources/json/batch-request.json @@ -2,6 +2,7 @@ "api_key": "_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI", "batch": [ { + "uuid": "8837f4d8-01e3-4bb8-b3f3-16b8fa8173e5", "event": "testEvent", "properties": { "testProperty": "testValue",