Skip to content
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
6 changes: 6 additions & 0 deletions .changeset/polite-memes-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"posthog": minor
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need to add for posthog-android since its the SDK that people install

"posthog-android": minor
---

add getAllFeatureFlags support
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public class PostHogFake : PostHogInterface {
return null
}

override fun getAllFeatureFlags(): List<FeatureFlagResult>? {
return null
}

override fun flush() {
flushes++
}
Expand Down
3 changes: 3 additions & 0 deletions posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public final class com/posthog/PostHog : com/posthog/PostHogStateless, com/posth
public fun distinctId ()Ljava/lang/String;
public fun endSession ()V
public fun flush ()V
public fun getAllFeatureFlags ()Ljava/util/List;
public fun getConfig ()Lcom/posthog/PostHogConfig;
public fun getDeviceId ()Ljava/lang/String;
public fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Boolean;)Ljava/lang/Object;
Expand Down Expand Up @@ -73,6 +74,7 @@ public final class com/posthog/PostHog$Companion : com/posthog/PostHogInterface
public fun distinctId ()Ljava/lang/String;
public fun endSession ()V
public fun flush ()V
public fun getAllFeatureFlags ()Ljava/util/List;
public fun getConfig ()Lcom/posthog/PostHogConfig;
public fun getDeviceId ()Ljava/lang/String;
public fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Boolean;)Ljava/lang/Object;
Expand Down Expand Up @@ -312,6 +314,7 @@ public abstract interface class com/posthog/PostHogInterface : com/posthog/PostH
public abstract fun captureFeatureView (Ljava/lang/String;Ljava/lang/String;)V
public abstract fun distinctId ()Ljava/lang/String;
public abstract fun endSession ()V
public abstract fun getAllFeatureFlags ()Ljava/util/List;
public abstract fun getConfig ()Lcom/posthog/PostHogConfig;
public abstract fun getDeviceId ()Ljava/lang/String;
public abstract fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Boolean;)Ljava/lang/Object;
Expand Down
15 changes: 15 additions & 0 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,17 @@ public class PostHog private constructor(
return flagValue
}

public override fun getAllFeatureFlags(): List<FeatureFlagResult>? {
if (!isEnabled()) return null
val flags = remoteConfig?.getFeatureFlags()
val results =
flags?.mapNotNull { item ->
val featureFlagResult = remoteConfig?.getFeatureFlagResult(item.key)
featureFlagResult
}
return results
}

public override fun getFeatureFlagPayload(
key: String,
defaultValue: Any?,
Expand Down Expand Up @@ -1551,6 +1562,10 @@ public class PostHog private constructor(
sendFeatureFlagEvent: Boolean?,
): Any? = shared.getFeatureFlag(key, defaultValue = defaultValue, sendFeatureFlagEvent)

override fun getAllFeatureFlags(): List<FeatureFlagResult>? {
return shared.getAllFeatureFlags()
}

public override fun getFeatureFlagPayload(
key: String,
defaultValue: Any?,
Expand Down
6 changes: 6 additions & 0 deletions posthog/src/main/java/com/posthog/PostHogInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public interface PostHogInterface : PostHogCoreInterface {
sendFeatureFlagEvent: Boolean? = null,
): Any?

/**
* Returns list of all feature flag result containing both value and payload.
* Docs https://posthog.com/docs/feature-flags and https://posthog.com/docs/experiments
*/
public fun getAllFeatureFlags(): List<FeatureFlagResult>?

/**
* Returns the feature flag payload
* Docs https://posthog.com/docs/feature-flags and https://posthog.com/docs/experiments
Expand Down
34 changes: 34 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
Expand Down Expand Up @@ -519,6 +520,39 @@ internal class PostHogTest {
sut.close()
}

@Test
fun `getAllFeatureFlags returns list of feature flags if enabled`() {
val file = File("src/test/resources/json/basic-flags-with-non-active-flags.json")
val responseFlagsApi = file.readText()
val http =
mockHttp(
response =
MockResponse()
.setBody(responseFlagsApi),
)
val url = http.url("/")

val sut = getSut(url.toString(), preloadFeatureFlags = false)

sut.reloadFeatureFlags()

remoteConfigExecutor.shutdownAndAwaitTermination()

val flags = sut.getAllFeatureFlags()

val result = flags?.first()

assertNotNull(result)
assertIs<FeatureFlagResult>(result)
assertEquals("4535-funnel-bar-viz", result.key)
assertTrue(result.enabled)
assertNull(result.variant)
assertTrue(result.payload as Boolean)
assertTrue(result.value as Boolean)

sut.close()
}

@Test
fun `getFeatureFlagResult returns the result after reloaded`() {
val http =
Expand Down
Loading