Skip to content

Latest commit

 

History

History
183 lines (141 loc) · 6.53 KB

README.md

File metadata and controls

183 lines (141 loc) · 6.53 KB

Split OpenFeature Provider for Android

Overview

This Provider is designed to enable the use of OpenFeature in Android, with Split as the backing feature flag & experimentation platform.

Compatibility

The Split OpenFeature Provider supports:

  • Android API Level 21 (Android 5.0 Lollipop) and higher.

Getting started

Below is a simple example that describes the instantiation of the Split Provider. Please see the OpenFeature Documentation for details on how to use the OpenFeature SDK.

Installation

Add the Split OpenFeature Provider dependency to your app's build.gradle.kts file:

dependencies {
    implementation("io.split.openfeature:split-openfeature-android:1.1.0")
}

Or for Groovy DSL (build.gradle):

dependencies {
    implementation 'io.split.openfeature:split-openfeature-android:1.1.0'
}

Usage

The Split OpenFeature Provider requires an Android Context and your Split SDK key. You must also provide an evaluation context with a targeting key when initializing the provider.

import dev.openfeature.kotlin.sdk.OpenFeatureAPI
import dev.openfeature.kotlin.sdk.ImmutableContext
import io.split.openfeature.android.provider.SplitProvider

// Create provider configuration
val config = SplitProvider.Config(
    applicationContext = applicationContext,
    sdkKey = "YOUR_SDK_KEY"
)

// Create the Split provider
val provider = SplitProvider(config = config)

// Set the provider with an initial context containing a targeting key
val initialContext = ImmutableContext(targetingKey = "user-123")
OpenFeatureAPI.setProvider(provider, initialContext = initialContext)

// Get a client and evaluate flags
val client = OpenFeatureAPI.getClient()
val showNewFeature = client.getBooleanValue("new-feature", false)

Evaluation Context

The Split OpenFeature Provider requires a targeting key to be set in the evaluation context. This key identifies the user or entity for which you are evaluating feature flags.

Setting a targeting key during initialization:

val initialContext = ImmutableContext(targetingKey = "user-123")
OpenFeatureAPI.setProvider(provider, initialContext = initialContext)

Changing the targeting key at runtime:

val newContext = ImmutableContext(targetingKey = "user-456")
OpenFeatureAPI.setEvaluationContext(newContext)

Using attributes for targeting:

val context = ImmutableContext(
    targetingKey = "user-123",
    attributes = mapOf(
        "email" to Value.String("user@example.com"),
        "age" to Value.Integer(30),
        "plan" to Value.String("premium")
    )
)

val client = OpenFeatureAPI.getClient()
val result = client.getBooleanDetails("premium-feature", false, context)

Bucketing key

Split can bucket on a key other than the targeting key (Split "bucketing key"). Set the reserved bucketingKey attribute (a non-blank string) next to the targeting key; the provider then binds the Split client to Key(targetingKey, bucketingKey). The targeting key is still used for targeting rules, segments and individual targets, and track keeps using the targeting key only.

val context = ImmutableContext(
    targetingKey = "user-123",
    attributes = mapOf("bucketingKey" to Value.String("account-42"))
)
OpenFeatureAPI.setEvaluationContext(context)
  • bucketingKey requires a targeting key: without one (or with an empty one) it is ignored with a warning and the current client is kept.
  • Changing only bucketingKey switches to a new Split client, like changing the targeting key. Set it through the evaluation context (setProvider initial context or setEvaluationContext); an invocation context with a different bucketingKey returns PROVIDER_NOT_READY, like a different targeting key.
  • bucketingKey is reserved: it is not forwarded to Split as an attribute.
  • Leading/trailing whitespace is trimmed. A blank or non-string value is ignored with a warning and the targeting key is used alone. Warnings go to the Split SDK logger, so they are visible only with a Split log level of WARNING or lower (SplitClientConfig.builder().logLevel(...)).
  • The length is not checked by the provider: a value longer than 250 characters is rejected by the Split SDK, and evaluations return the default value (control, error code FLAG_NOT_FOUND).

Observing Provider Events

The Split OpenFeature Provider emits events when the provider state changes. You can observe these events to react to provider readiness, configuration changes, or errors.

import dev.openfeature.kotlin.sdk.events.OpenFeatureProviderEvents
import kotlinx.coroutines.launch

// Observe provider events
lifecycleScope.launch {
    provider.observe().collect { event ->
        when (event) {
            is OpenFeatureProviderEvents.ProviderReady -> {
                // Provider is ready to evaluate flags
                Log.d("Split", "Provider is ready")
            }
            is OpenFeatureProviderEvents.ProviderConfigurationChanged -> {
                // Flag configuration has been updated
                Log.d("Split", "Configuration changed")
            }
            is OpenFeatureProviderEvents.ProviderStale -> {
                // Provider is serving cached data
                Log.d("Split", "Provider is stale")
            }
            is OpenFeatureProviderEvents.ProviderError -> {
                // An error occurred
                Log.e("Split", "Provider error: ${event.error}")
            }
        }
    }
}

Tracking Events

The Split OpenFeature Provider supports tracking events. To track events, you must set a trafficType in the evaluation context.

// Set context with trafficType
val context = ImmutableContext(targetingKey = "user-123")
    .withTrafficType("user")

OpenFeatureAPI.setEvaluationContext(context)

// Track an event
val client = OpenFeatureAPI.getClient()
client.track("button_clicked")

// Track with a value
client.track(
    "purchase_completed",
    TrackingEventDetails(value = 99.99)
)

// Track with properties
client.track(
    "page_viewed",
    TrackingEventDetails(
        structure = ImmutableStructure(
            mapOf(
                "page" to Value.String("home"),
                "referrer" to Value.String("google")
            )
        )
    )
)

Contributing

Please see Contributors Guide to find all you need to submit a Pull Request (PR).

License

Licensed under the Apache License, Version 2.0. See: Apache License.