Skip to content

Commit

Permalink
fix usage and feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Oct 3, 2023
1 parent 85fa286 commit f133403
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
12 changes: 8 additions & 4 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ PostHogAndroid.setup(applicationContext, config)

// At runtime
PostHog.optOut()
PostHog.optIn()

// Check it and opt-in
if (PostHog.isOptOut()) {
PostHog.optIn()
}
```

Capture a screen view event
Expand Down Expand Up @@ -118,15 +122,15 @@ Load feature flags automatically
val config = PostHogAndroidConfig(apiKey).apply {
preloadFeatureFlags = true
// get notified when feature flags are loaded
onFeatureFlags = PostHogOnFeatureFlags {
featureFlags -> print("has feature flags: ${featureFlags != null}")
onFeatureFlags = PostHogOnFeatureFlags {
print("feature flags loaded")
}
}
PostHogAndroid.setup(applicationContext, config)

// And/Or manually
PostHog.reloadFeatureFlags {
featureFlags -> print("has feature flags: ${featureFlags != null}")
print("feature flags loaded")
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ class MyApp : Application() {
val config = PostHogAndroidConfig("_6SG-F7I1vCuZ-HdJL3VZQqjBlaSb1_20hDPwqMNnGI").apply {
debug = true
flushAt = 5
maxBatchSize = 2
// flushIntervalSeconds = 5
onFeatureFlags = PostHogOnFeatureFlags { featureFlags -> print("has flags: ${featureFlags != null}") }
maxBatchSize = 5
onFeatureFlags = PostHogOnFeatureFlags { print("feature flags loaded") }
}
PostHogAndroid.setup(this, config)

// val config2 = PostHogConfig("test")
// val postHog = PostHog.with(config2)
// postHog.capture("testConfig2")
}
}
2 changes: 1 addition & 1 deletion posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public abstract interface annotation class com/posthog/PostHogInternal : java/la
}

public abstract interface class com/posthog/PostHogOnFeatureFlags {
public abstract fun notify (Ljava/util/Map;)V
public abstract fun loaded ()V
}

public abstract interface annotation class com/posthog/PostHogVisibleForTesting : java/lang/annotation/Annotation {
Expand Down
1 change: 1 addition & 0 deletions posthog/src/main/java/com/posthog/PostHogConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public open class PostHogConfig(

/**
* Preload feature flags automatically
* Docs https://posthog.com/docs/feature-flags and https://posthog.com/docs/experiments
* Defaults to true
*/
public var preloadFeatureFlags: Boolean = true,
Expand Down
18 changes: 12 additions & 6 deletions posthog/src/main/java/com/posthog/PostHogInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public interface PostHogInterface {
* Captures events
* @param distinctId the distinctId, the generated distinctId is used if not given
* @param properties the custom properties
* @param userProperties the user properties, set as a "$set" property
* @param userPropertiesSetOnce the user properties to set only once, set as a "$set_once" property
* @param groupProperties the group properties, set as a "$groups" property
* @param userProperties the user properties, set as a "$set" property, Docs https://posthog.com/docs/product-analytics/user-properties
* @param userPropertiesSetOnce the user properties to set only once, set as a "$set_once" property, Docs https://posthog.com/docs/product-analytics/user-properties
* @param groupProperties the group properties, set as a "$groups" property, Docs https://posthog.com/docs/product-analytics/group-analytics
*/
public fun capture(
event: String,
Expand All @@ -35,10 +35,11 @@ public interface PostHogInterface {

/**
* Identifies the user
* Docs https://posthog.com/docs/product-analytics/identify
* @param distinctId the distinctId
* @param properties the custom properties
* @param userProperties the user properties, set as a "$set" property
* @param userPropertiesSetOnce the user properties to set only once, set as a "$set_once" property
* @param userProperties the user properties, set as a "$set" property, Docs https://posthog.com/docs/product-analytics/user-properties
* @param userPropertiesSetOnce the user properties to set only once, set as a "$set_once" property, Docs https://posthog.com/docs/product-analytics/user-properties
*/
public fun identify(
distinctId: String,
Expand All @@ -55,20 +56,23 @@ public interface PostHogInterface {

/**
* Returns if a feature flag is enabled, the feature flag must be a Boolean
* Docs https://posthog.com/docs/feature-flags and https://posthog.com/docs/experiments
* @param key the Key
* @param defaultValue the default value if not found
*/
public fun isFeatureEnabled(key: String, defaultValue: Boolean = false): Boolean

/**
* Returns the feature flag
* Docs https://posthog.com/docs/feature-flags and https://posthog.com/docs/experiments
* @param key the Key
* @param defaultValue the default value if not found
*/
public fun getFeatureFlag(key: String, defaultValue: Any? = null): Any?

/**
* Returns the feature flag payload
* Docs https://posthog.com/docs/feature-flags and https://posthog.com/docs/experiments
* @param key the Key
* @param defaultValue the default value if not found
*/
Expand Down Expand Up @@ -97,9 +101,10 @@ public interface PostHogInterface {

/**
* Creates a group
* Docs https://posthog.com/docs/product-analytics/group-analytics
* @param type the Group type
* @param key the Group key
* @param groupProperties the Group properties, set as a "$group_set" property
* @param groupProperties the Group properties, set as a "$group_set" property, Docs https://posthog.com/docs/product-analytics/group-analytics
*/
public fun group(type: String, key: String, groupProperties: Map<String, Any>? = null)

Expand All @@ -112,6 +117,7 @@ public interface PostHogInterface {

/**
* Creates an alias for the user
* Docs https://posthog.com/docs/product-analytics/identify#alias-assigning-multiple-distinct-ids-to-the-same-user
* @param alias the alias
* @param properties the custom properties
*/
Expand Down
3 changes: 1 addition & 2 deletions posthog/src/main/java/com/posthog/PostHogOnFeatureFlags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package com.posthog
public fun interface PostHogOnFeatureFlags {
/**
* The method that is called when feature flags are loaded
* @param featureFlags the Feature flags property, can be null if there was an error
*/
public fun notify(featureFlags: Map<String, Any>?)
public fun loaded()
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class PostHogFeatureFlags(
config.logger.log("Loading feature flags failed: $e")
} finally {
try {
onFeatureFlags?.notify(getFeatureFlags())
onFeatureFlags?.loaded()
} catch (e: Throwable) {
config.logger.log("Executing the feature flags callback failed: $e")
} finally {
Expand Down

0 comments on commit f133403

Please sign in to comment.