Con el SDK de Android de Kameleoon, puede activar feature flags en aplicaciones móviles nativas de Android. El SDK de Android es compatible tanto con Kotlin como con Java. El SDK es sencillo de integrar en sus aplicaciones y su uso de memoria y red es bajo.
Primeros pasos: Para obtener ayuda para comenzar, consulte la guía del desarrollador.
Changelog: Última versión del SDK de Android: 4.24.0 Changelog
Métodos del SDK: Para la documentación de referencia completa de los métodos del SDK de Android, consulte la sección referencia.
Guía del desarrollador
Follow this section to install and configure the Android SDK in your Android app and learn about advanced features.
Primeros pasos
Follow these steps to install and configure the Kameleoon Android SDK in your application.
Instalación
Puede instalar el SDK de Android añadiendo la siguiente dependencia al archivo build.gradle de su aplicación Android:
dependencies {
implementation 'com.kameleoon:kameleoon-client-android:4.20.0'
}
Configuración adicional
To customize the SDK’s behavior, create a .properties configuration file. The properties file’s name and location are important:
- Create the file in you app’s
assets/ directory.
- Name the file
kameleoon-client.properties.
También puede descargar un archivo de configuración de ejemplo.
Estas son las propiedades disponibles que puede establecer:
| Key | Description | Default value |
|---|
refreshIntervalMinute / refresh_interval_minute (opcional) | Specifies the refresh interval, in minutes, for the SDK to fetch the configuration for the active experiments and feature flags. The value determines the maximum time it takes to propagate changes, such as activating or deactivating feature flags or launching experiments. If left unspecified, the default interval is 60 minutes. Additionally, a streaming mode is available that uses server-sent events (SSE) to push new configurations to the SDK automatically and apply them in real-time. | 60 minutos |
dataExpirationIntervalMinute / data_expiration_interval_minute (opcional) | Designates the predefined time period, in minutes, that the SDK stores the visitor and their associated data. Each data instance is evaluated individually, allowing you to set the amount of time the SDK saves data before automatically deleting it. If no interval is specified, the SDK does not automatically delete data from the device. | Integer.MAX_VALUE |
defaultTimeoutMillisecond / default_timeout_millisecond (opcional) | Specifies the time interval, in milliseconds, that it takes for network requests from the SDK to time out. Set the value to 30000 milliseconds (30 seconds) or more if you do not have a stable connection. Some methods have additional parameters for method-specific timeouts, but if you do not specify them explicitly, el valor predeterminado es used. | 10000 milisegundos |
trackingIntervalMillisecond / tracking_interval_millisecond (opcional) | Specifies the interval for tracking requests, in milliseconds. All visitors who were evaluated for any feature flag or had data flushed will be included in this tracking request, which is performed once per interval. The minimum value is 1000 ms and the maximum value is 5000 ms. | 1000 ms |
environment / environment (opcional) | For customers using multi-environment experimentation and feature flagging, this option specifies which feature flag configuration to use. De forma predeterminada, each feature flag has the options production, staging, and development. If not specified, el valor predeterminado es production. More information. | nil |
isUniqueIdentifier / is_unique_identifier (opcional) | Indicates that the specified visitorCode is a unique identifier. | false |
networkDomain / network_domain (opcional) | Dominio personalizado utilizado por los SDKs para las solicitudes salientes, a menudo para proxy. Debe ser un dominio válido (por ejemplo, example.com o sub.example.com). Los formatos no válidos se sustituyen por el valor de Kameleoon. | nil |
defaultDataFile / default_datafile (opcional) | The default_datafile feature ensures the Kameleoon SDK is always READY by providing a fallback configuration when no cached data file exists. Developers can preload a valid configuration by fetching it from https://sdk-config.kameleoon.eu/v3/<sitecode> and passing it as default_datafile during initialization. When a dateModified timestamp (in milliseconds) is provided and is newer than the cached version, the SDK will use the default datafile instead of the cached version. If dateModified is omitted, the default datafile is only applied when no cached version exists. This ensures the SDK always has a valid configuration, whether default, cached, or updated. | nil |
activityTrackingIntervalMillisecond / activity_tracking_interval_millisecond (opcional) | Defines the interval at which activity events are sent. Modifying this parameter can have side effects. Check this section before using it. The minimum and el valor predeterminado es 15 000 ms. Setting this value to 0 disables periodic activity tracking; in this case, only a single activity event is sent at application startup. | 15 000 ms |
If you specify a visitorCode and set the isUniqueIdentifier parameter to true, the SDK methods use the visitorCode value as the unique visitor identifier, which is useful for cross-device experimentation. The SDK links the flushed data to the visitor that is associated with the specified identifier.isUniqueIdentifier puede ser útil en otros escenarios excepcionales, como cuando no puede acceder al visitorCode anónimo asignado originalmente al visitante, pero sí tiene acceso a un ID interno conectado al visitante anónimo mediante la fusión de sesiones.
Using activityTrackingIntervalMillisecond
El parámetro activityTrackingIntervalMillisecond está diseñado para ayudar a reducir el consumo de batería y el uso de red. Sin embargo, cambiar su valor puede tener efectos secundarios significativos que debe considerar cuidadosamente.
Review its impact on the following features:
-
Elapsed time triggers
- If the configured elapsed time is shorter than the tracking interval, the trigger will not fire as expected.
-
Elapsed time segments
- If the elapsed time is shorter than the tracking interval, users may not be included in the segment as intended.
-
Time spent goals
- If the elapsed time is shorter than the tracking interval, the goal may never be reached.
-
Time elapsed since last visit in the results page
- Measurements for “time elapsed since last visit” become less precise when the elapsed time is close to or below the tracking interval.
-
Visits count
- A new visit is created after 30 minutes of inactivity. If the tracking interval is longer than 30 thirty minutes, a new visit will be created at each tracking interval.
Setting activityTrackingIntervalMillisecond to 0 disables periodic activity tracking entirely. In this configuration, only a single activity event is sent at application startup, which renders all of the features listed above unusable.
Initialize the Kameleoon Client
After installing the SDK in your application and setting up the app properties, you must create the Kameleoon Client. A Client is a singleton object that acts as a bridge between your application and the Kameleoon platform. It includes all of the methods and properties you need to run a feature flag.
import com.kameleoon.KameleoonClient;
import com.kameleoon.KameleoonClientConfig;
import com.kameleoon.KameleoonClientFactory;
import com.kameleoon.KameleoonException;
public class MyApplication extends Application
{
private KameleoonClient kameleoonClient;
@Override
public void onCreate() {
super.onCreate();
try {
KameleoonClientConfig config = new KameleoonClientConfig.Builder()
.refreshIntervalMinute(15) // in minutes, 1 hour by default, optional
.defaultTimeoutMillisecond(10_000) // in milliseconds, 10 seconds by default, optional
.trackingIntervalMillisecond(1000) // in milliseconds, 1000 ms by default, optional
.dataExpirationIntervalMinute(1440 * 365) // in minutes, infinity by default, optional
.environment("staging") // optional
.isUniqueIdentifier(false) // optional, false by default. Set to true if the visitorCode corresponds to your customer's unique userId.
.networkDomain("example.com") // optional
.defaultDataFile("{...}") // optional
.activityTrackingIntervalMillisecond(20_000) // optional, 15_000 milliseconds by default
.build();
String siteCode = "a8st4f59bj";
String visitorCode = "yourVisitorCode";
kameleoonClient = KameleoonClientFactory.create(siteCode, visitorCode, config, getApplicationContext());
// or, if you want, the visitor code will be generated automatically
kameleoonClient = KameleoonClientFactory.create(siteCode, config, getApplicationContext());
} catch (KameleoonException.SiteCodeIsEmpty | KameleoonException.VisitorCodeInvalid exception) {
// Exceptions indicate that provided siteCode is empty or visitorCode is invalid
} catch (Exception exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
}
public KameleoonClient getKameleoonClient() {
return kameleoonClient;
}
}
import com.kameleoon.KameleoonClientConfig
import com.kameleoon.KameleoonClientFactory
import com.kameleoon.KameleoonException
class MyApplication : Application() {
var kameleoonClient: KameleoonClient? = null
private set
override fun onCreate() {
super.onCreate()
try {
val config = KameleoonClientConfig.Builder()
.refreshIntervalMinute(15) // in minutes, 1 hour by default, optional
.defaultTimeoutMillisecond(10_000) // in milliseconds, 10 seconds by default, optional
.trackingIntervalMillisecond(1000) // in milliseconds, 1000 ms by default, optional
.dataExpirationIntervalMinute(1440 * 365) // in minutes, infinity by default, optional
.environment("staging") // optional
.networkDomain("example.com") // optional
.defaultDataFile("{...}") // optional
.activityTrackingIntervalMillisecond(20_000) // optional, 15_000 milliseconds by default
.build()
val siteCode = "a8st4f59bj"
val visitorCode = "yourVisitorCode"
val kameleoonClient = KameleoonClientFactory.create(siteCode, visitorCode, config, applicationContext)
// or if you want that visitor code will be generated automaticallyd
kameleoonClient = KameleoonClientFactory.create(siteCode, config, applicationContext)
} catch (e: KameleoonException.SiteCodeIsEmpty) {
// Exception indicating that provided siteCode is empty
} catch (e: KameleoonException.VisitorCodeInvalid) {
// Exception indicating that provided visitor code is invalid
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
}
}
While executing, the KameleoonClientFactory.create() method initializes the client, but it is not immediately ready for use. This delay is because the Kameleoon Client must retrieve the current configuration of feature flags (along with their traffic repartition) from a Kameleoon remote server. This retrieval requires network access, which is not always available. Until the Kameleoon Client is fully ready, you should not attempt to run other methods in the Kameleoon Android SDK. Note that once the first configuration of the feature flags is fetched, it is then periodically refreshed, but even if the refresh fails for any reason, the Kameleoon client will continue to function using the previous configuration.
Puede usar el método isReady() para comprobar si la inicialización del cliente Kameleoon ha finalizado.
Alternatively, a helper callback can encapsulate the logic of feature flag triggering and variation implementation. The best approach (isReady() or callback) depends on preferences and the exact use case. Using isReady() is recommended when the SDK is expected to be ready for use soon. Por ejemplo, isReady() is appropriate when running a feature flag on a dialog that users likely won’t access for the first few seconds or minutes of navigating in the app. A callback is recommended when there is a high probability that the SDK is still initializing. Por ejemplo, a feature flag that appears onscreen at the application launch should use a callback that makes the application wait until the SDK is ready or a specified timeout has expired.
It’s your responsibility as the app developer to ensure the logic of your application code is correct within the context of A/B testing using Kameleoon. A good practice is to always assume that the application user can be left out of the feature flag when the Kameleoon client is not yet ready. This exclusion is easy to implement, because this corresponds to the implementation of the default or reference variation logic. The code samples in the next paragraph show examples of this approach.
You’re now ready to implement feature management and features flags. See the Reference section for details about additional methods.
Best practices for initialization and usage
- Initializing
KameleoonClient as a singleton as early as possible after the application starts is recommended, as initialization may take some time. Since initialization is asynchronous, it does not block or delay the application startup process.
- Before using
KameleoonClient, verify that it is initialized by calling the runWhenReady method. Otherwise, attempts to use the client before it is ready will result in errors.
- ⚠️ Most key methods may throw exceptions, so proper exception handling is required. Be sure to review the documentation for each method you use to understand its potential exceptions.
Java
Kotlin
Kotlin (Coroutines)
// Initialize `KameleoonClient` on application startup and use it as a singleton later
try {
KameleoonClient kameleoonClient = KameleoonClientFactory.create("<siteCode>", getApplicationContext());
} catch (KameleoonException ignored) {}
// Example: Apply a discount percentage based on an feature flag variable's value
void applyDiscountIfApplicable() {
kameleoonClient.runWhenReady(1000, result -> {
double discount = 0.0;
try {
if (result.getOrThrow()) {
Variation variation = kameleoonClient.getVariation("discount");
discount = (double) variation.getVariables().get("discount_value").getValue();
}
} catch (Exception ignored) { }
if (discount > 0) {
applyDiscount(discount);
}
});
}
// Initialize `KameleoonClient` on application startup and use it as a singleton later
try {
KameleoonClient kameleoonClient = KameleoonClientFactory.create("<siteCode>", applicationContext)
} catch (ignored: KameleoonException) {}
// Example: Apply a discount percentage based on a feature flag variable's value
fun applyDiscountIfApplicable() {
kameleoonClient.runWhenReady(1000) { result ->
val discount = runCatching {
if (result.getOrThrow()) {
val variation = kameleoonClient.getVariation("discount")
variation.variables["discount_value"]?.value as? Double
} else {
null
}
}.getOrNull() ?: 0.0
if (discount > 0) {
applyDiscount(discount)
}
}
}
// Initialize `KameleoonClient` on application startup and use it as a singleton later
try {
val kameleoonClient = KameleoonClientFactory.create("<siteCode>", applicationContext)
} catch (ignored: KameleoonException) {}
// Example: Apply a discount percentage based on a feature flag variable's value
suspend fun applyDiscountIfApplicable() {
kameleoonClient.runWhenReady(1000).getOrNull() ?: return // Exit if initialization fails
val discount = runCatching {
val variation = kameleoonClient.getVariation("discount")
variation.variables["discount_value"]?.value as? Double
}.getOrNull() ?: 0.0
if (discount > 0) {
applyDiscount(discount)
}
}
Activación de un feature flag
Recuperación de la configuración de un flag
Para implementar un feature flag en su código, primero debe crear el feature flag en su cuenta de Kameleoon.
Para determinar el estado o la variación de un feature flag para un usuario específico, debe utilizar el método getVariation() o isFeatureActive() para recuperar la configuración basada en el featureKey.
El método getVariation() gestiona tanto los feature flags simples con estados ON/OFF como los flags más complejos con múltiples variaciones. El método recupera la variación adecuada para el usuario comprobando las reglas de la funcionalidad, asignando la variación y devolviéndola en función del featureKey y el visitorCode.
El método isFeatureActive() puede utilizarse si desea recuperar la configuración de un feature flag simple que solo tiene un estado ON u OFF, a diferencia de los feature flags más complejos con múltiples variaciones u opciones de segmentación.
Si su feature flag tiene variables asociadas (como comportamientos específicos vinculados a cada variación), getVariation() también le permite acceder al objeto Variation, que proporciona detalles sobre la variación asignada y su experimento asociado. Este método comprueba si el usuario está segmentado, encuentra la variación asignada al visitante y la guarda en almacenamiento. Cuando track=true, el SDK enviará el evento de exposición al experimento especificado en la siguiente solicitud de seguimiento, que se desencadena automáticamente según el tracking_interval_millisecond del SDK. De forma predeterminada, este intervalo está configurado en 1000 milisegundos (1 segundo).
El método getVariation() le permite controlar si se realiza el seguimiento. Si track=false, el SDK no enviará eventos de exposición. Esto es útil si prefiere no realizar el seguimiento de los datos a través del SDK y, en su lugar, basarse en el seguimiento del lado del cliente gestionado por el motor de Kameleoon, por ejemplo. Además, establecer track=false resulta útil cuando se utiliza el método getVariations(), donde puede que solo necesite las variaciones de todos los flags sin desencadenar eventos de seguimiento. Si desea saber más sobre cómo funciona el seguimiento, consulte este artículo
Adición de puntos de datos para segmentar a un usuario o filtrar / desglosar visitas en informes
Para segmentar a un usuario, asegúrese de haber añadido los puntos de datos relevantes a su perfil antes de recuperar la variación de la funcionalidad o comprobar si el flag está activo. Utilice el método addData() para añadir estos puntos de datos al perfil del usuario.
To retrieve data points collected on other devices, use the getRemoteVisitorData() method. This method asynchronously fetches data from the servers. It is important to call getRemoteVisitorData() before retrieving the variation or checking if the feature flag is active, as this data might be required to assign a user to a given variation.
Para obtener más información sobre las condiciones de segmentación disponibles, consulte el artículo detallado sobre este tema.
Additionally, the data points you add to the visitor profile will be available when analyzing your experiments, allowing you to filter and break down your results by factors like device. See the complete list here.
Si necesita realizar el seguimiento de puntos de datos adicionales más allá de los que se recopilan automáticamente, puede utilizar la funcionalidad de Custom Data de Kameleoon. Custom Data le permite capturar y analizar información específica relevante para sus experimentos. No olvide llamar al método flush() para enviar los datos recopilados a los servidores de Kameleoon para su análisis.
Seguimiento de conversiones de objetivos
Cuando un usuario completa una acción deseada (como realizar una compra), se registra como una conversión. Para hacer seguimiento de las conversiones, utilice el método trackConversion() y proporcione el parámetro requerido goalId.
La solicitud de seguimiento de conversiones se enviará junto con la siguiente solicitud de seguimiento programada, que el SDK envía a intervalos regulares (definidos por tracking_interval_millisecond). Si prefiere enviar la solicitud de inmediato, utilice el método flush() con el parámetro instant=true.
Experimentación entre dispositivos
Para dar soporte a los visitantes que acceden a una aplicación desde varios dispositivos, Kameleoon permite sincronizar los datos del visitante previamente recopilados entre cada uno de sus dispositivos y reconciliar su historial de visitas entre dispositivos mediante la experimentación entre dispositivos. Los casos de estudio y la información detallada sobre cómo Kameleoon gestiona los datos entre dispositivos están disponibles en el artículo sobre experimentación entre dispositivos.
Sincronización de datos personalizados entre dispositivos
Aunque la sincronización de mapeo personalizado se utiliza para alinear los datos del visitante entre dispositivos, no siempre es necesaria. A continuación se presentan dos escenarios en los que no se requiere la sincronización de mapeo personalizado:
Mismo ID de usuario en todos los dispositivos
Si el mismo ID de usuario se utiliza de forma consistente en todos los dispositivos, la sincronización se gestiona automáticamente sin necesidad de una sincronización de mapeo personalizado. Basta con llamar al método getRemoteVisitorData() cuando desee sincronizar los datos recopilados entre varios dispositivos.
Instancias multi-servidor con IDs consistentes
En configuraciones complejas que involucran varios servidores (por ejemplo, instancias de servidor distribuidas), donde el mismo ID de usuario está disponible en todos los servidores, la sincronización entre servidores (con getRemoteVisitorData()) es suficiente sin necesidad de una sincronización adicional de mapeo personalizado.
Los clientes que necesiten datos adicionales pueden consultar la descripción del método getRemoteVisitorData() para obtener más orientación. En el código siguiente se asume que el mismo identificador único (en este caso, el visitorCode, que también puede denominarse userId) se utiliza de manera consistente entre los dos dispositivos para una recuperación precisa de los datos.
Si desea sincronizar los datos recopilados en tiempo real, debe elegir el ámbito Visitor para sus datos personalizados.
Java
Kotlin
Kotlin (Coroutines)
// In this, example Custom data with index `90` was set to "Visitor" scope in Kameleoon.
final int VISITOR_SCOPE_CUSTOM_DATA_INDEX = 90;
kameleoonClient.addData(new CustomData(VISITOR_SCOPE_CUSTOM_DATA_INDEX, "your data"));
kameleoonClient.flush();
// Before working with the data, call `getRemoteVisitorData`.
kameleoonClient.getRemoteVisitorData(result -> {
// After calling, the SDK on Device B will have access to CustomData of Visitor scope defined on Device A.
// So, "your data" will be available to target and track the visitor.
});
// In this example Custom data with index `90` was set to "Visitor" scope on Kameleoon Platform.
val VISITOR_SCOPE_CUSTOM_DATA_INDEX = 90
kameleoonClient.addData(CustomData(VISITOR_SCOPE_CUSTOM_DATA_INDEX, "your data"))
kameleoonClient.flush()
// Before working with the data, call the `getRemoteVisitorData` method.
kameleoonClient.getRemoteVisitorData { result ->
// After that the SDK on Device B will have an access to CustomData of Visitor scope defined on Device A.
// So "your data" will be available for targeting and tracking for the visitor.
}
// In this example Custom data with index `90` was set to "Visitor" scope on Kameleoon Platform.
val VISITOR_SCOPE_CUSTOM_DATA_INDEX = 90
kameleoonClient.addData(CustomData(VISITOR_SCOPE_CUSTOM_DATA_INDEX, "your data"))
kameleoonClient.flush()
// Before working with the data, call the `getRemoteVisitorData` method.
kameleoonClient.getRemoteVisitorData()
// After that the SDK on Device B will have an access to CustomData of Visitor scope defined on Device A.
// So "your data" will be available for targeting and tracking for the visitor.
Uso de datos personalizados para la fusión de sesiones
La experimentación entre dispositivos permite combinar el historial de un visitante en cada uno de sus dispositivos (reconciliación de historial). La reconciliación de historial permite fusionar diferentes sesiones de un visitante en una sola. Para reconciliar el historial de visitas, utilice CustomData para proporcionar un identificador único del visitante. Para más información, consulte la documentación dedicada.
Una vez habilitada la reconciliación entre dispositivos, al llamar a getRemoteVisitorData() con el parámetro userId se recuperan todos los datos conocidos para un usuario determinado.
Las sesiones con el mismo identificador siempre verán la misma variación en un experimento. En la vista Visitor de las páginas de resultados de su experimento, estas sesiones aparecerán como un único visitante.
La configuración del SDK garantiza que las sesiones asociadas siempre vean la misma variación del experimento. Sin embargo, existen algunas limitaciones en cuanto a la asignación de variaciones entre dispositivos. Estas limitaciones se describen aquí.
Siga la guía de activación de la reconciliación de historial entre dispositivos para configurar sus datos personalizados en la plataforma Kameleoon.
Posteriormente, puede usar el SDK de forma normal. Los siguientes métodos pueden ser útiles en el contexto de la fusión de sesiones:
getRemoteVisitorData() with passed isUniqueIdentifier=true to KameleoonClientConfig - to retrieve data for all linked visitors.
trackConversion() or flush() with passed isUniqueIdentifier=true to KameleoonClientConfig - to track some data for specific visitor that is associated with another visitor.
A continuación se muestra un ejemplo de cómo usar datos personalizados para la fusión de sesiones.
Java
Kotlin
Kotlin (Coroutines)
// In this example, `91` represents the Custom Data's index,
// configured as a unique identifier in Kameleoon.
final int MAPPING_INDEX = 91;
final String FEATURE_KEY = "ff123";
// 0. Initializing anonymous KameleoonClient
// Assume `anonymousVisitorCode` is the randomly generated ID for that visitor.
KameleoonClient anonymousKameleoonClient = KameleoonClientFactory.create(siteCode, anonymousVisitorCode, getApplicationContext());
anonymousKameleoonClient.runWhenReady(result -> {
// ...
});
// 1. Before the visitor is authenticated
// Retrieve the variation for an unauthenticated visitor.
Variation anonymousVariation = anonymousKameleoonClient.getVariation(FEATURE_KEY);
// 2. After the visitor is authenticated
// Assume `userId` is the authenticated visitor's visitor code.
anonymousKameleoonClient.addData(new CustomData(MAPPING_INDEX, userId));
anonymousKameleoonClient.flush(true);
KameleoonClient userKameleoonClient = KameleoonClientFactory.create(
siteCode, userId,
(new KameleoonClientConfig.Builder())
.isUniqueIdentifier(true) // Indicate that `userId` is a unique identifier
.build(),
getApplicationContext()
);
userKameleoonClient.runWhenReady(result -> {
// ...
});
// 3. After the visitor has been authenticated
// Retrieve the variation for the `userId`, which will match the anonymous visitor code's variation.
Variation userVariation = userKameleoonClient.getVariation(FEATURE_KEY);
boolean isSameVariation = userVariation.getKey().equals(anonymousVariation.getKey()); // true
// The `userId` and `anonymousVisitorCode` are now linked and tracked as a single visitor.
kameleoonClient.trackConversion(123, 10.0f);
// Additionally, the linked visitors will share all fetched remote visitor data.
kameleoonClient.getRemoteVisitorData(result -> {
// ...
});
// In this example, `91` represents the Custom Data's index
// configured as a unique identifier in Kameleoon.
val MAPPING_INDEX = 91
val FEATURE_KEY = "ff123"
// 0. Initializing anonymous KameleoonClient
// Assume `anonymousVisitorCode` is the randomly generated ID for that visitor.
val anonymousKameleoonClient = KameleoonClientFactory.create(siteCode, anonymousVisitorCode, applicationContext)
anonymousKameleoonClient.runWhenReady { result ->
// ...
}
// 1. Before the visitor is authenticated
// Retrieve the variation for an unauthenticated visitor.
val anonymousVariation = anonymousKameleoonClient.getVariation(FEATURE_KEY)
// 2. After the visitor is authenticated
// Assume `userId` is the authenticated visitor's visitor code.
anonymousKameleoonClient.addData(CustomData(MAPPING_INDEX, userId))
anonymousKameleoonClient.flush(true)
val userKameleoonClient = KameleoonClientFactory.create(
siteCode, userId,
KameleoonClientConfig.Builder()
.isUniqueIdentifier(true) // Indicate that `userId` is a unique identifier
.build(),
applicationContext
)
userKameleoonClient.runWhenReady { result ->
// ...
}
// 3. After the visitor has been authenticated
// Retrieve the variation for the `userId`, which will match the anonymous visitor code's variation.
val userVariation = userKameleoonClient.getVariation(FEATURE_KEY)
val isSameVariation = userVariation.getKey() == anonymousVariation.getKey() // true
// The `userId` and `anonymousVisitorCode` are now linked and tracked as a single visitor.
userKameleoonClient.trackConversion(123, 10.0f)
// Additionally, the linked visitors will share all fetched remote visitor data.
userKameleoonClient.getRemoteVisitorData { result ->
// ...
}
// In this example, `91` represents the Custom Data's index
// configured as a unique identifier in Kameleoon.
val MAPPING_INDEX = 91
val FEATURE_KEY = "ff123"
// 0. Initializing anonymous KameleoonClient
// Assume `anonymousVisitorCode` is the randomly generated ID for that visitor.
val anonymousKameleoonClient = KameleoonClientFactory.create(siteCode, anonymousVisitorCode, applicationContext)
anonymousKameleoonClient.runWhenReady()
// 1. Before the visitor is authenticated
// Retrieve the variation for an unauthenticated visitor.
val anonymousVariation = anonymousKameleoonClient.getVariation(FEATURE_KEY)
// 2. After the visitor is authenticated
// Assume `userId` is the authenticated visitor's visitor code.
anonymousKameleoonClient.addData(CustomData(MAPPING_INDEX, userId))
anonymousKameleoonClient.flush(true)
val userKameleoonClient = KameleoonClientFactory.create(
siteCode, userId,
KameleoonClientConfig.Builder()
.isUniqueIdentifier(true) // Indicate that `userId` is a unique identifier
.build(),
applicationContext
)
userKameleoonClient.runWhenReady()
// 3. After the visitor has been authenticated
// Retrieve the variation for the `userId`, which will match the anonymous visitor code's variation.
val userVariation = userKameleoonClient.getVariation(FEATURE_KEY)
val isSameVariation = userVariation.getKey() == anonymousVariation.getKey() // true
// The `userId` and `anonymousVisitorCode` are now linked and tracked as a single visitor.
userKameleoonClient.trackConversion(123, 10.0f)
// Additionally, the linked visitors will share all fetched remote visitor data.
userKameleoonClient.getRemoteVisitorData()
En este ejemplo, la aplicación tiene una página de inicio de sesión. Como el ID de usuario es desconocido en el momento del inicio de sesión, se utiliza un visitante anónimo generado automáticamente por el SDK. El visitor code puede recuperarse con el método getVisitorCode(). Después de que el usuario inicia sesión, el visitante anónimo se asocia con el ID de usuario y se utiliza como identificador único del visitante.
Uso de una clave de bucketing personalizada
De forma predeterminada, Kameleoon utiliza un ID de visitante anónimo y único (visitorCode) para asignar usuarios a las variaciones de los feature flags. Este ID se genera y almacena habitualmente en el dispositivo del usuario (en una cookie del navegador para los SDKs del lado del cliente y del lado del servidor, y en almacenamiento persistente para los SDKs móviles). Sin embargo, en determinados escenarios puede necesitar asegurarse de que todos los usuarios de la misma organización vean la misma variante de un feature flag.
La opción Custom Bucketing Key le permite anular este comportamiento predeterminado proporcionando su propio identificador personalizado para el bucketing. Esta anulación garantiza que la lógica de asignación de Kameleoon utilice la clave que usted especifique en lugar del visitorCode predeterminado.
Casos de uso
El uso de una clave de bucketing personalizada es esencial para mantener la consistencia y precisión en las asignaciones de sus feature flags, especialmente en estas situaciones:
- Experimentos a nivel de cuenta u organización: Para productos B2B o escenarios en los que desea asignar a todos los usuarios de la misma organización a la misma variación, puede utilizar un identificador como
accountId. Las claves de bucketing personalizadas son cruciales para probar mediante A/B funcionalidades que afecten a todo un equipo o empresa.
Al implementar una clave de bucketing personalizada, garantiza una mayor consistencia y precisión en sus experimentos, lo que se traduce en resultados más fiables y en una mejor experiencia de usuario.
Detalles técnicos
Cuando configura una clave de bucketing personalizada para un feature flag, proporciona a Kameleoon un identificador específico de los datos de su aplicación:
kameleoonClient.addData(new CustomData(index, "newVisitorCode"));
kameleoonClient.addData(CustomData(index, "newVisitorCode"))
- Proporcionar la clave personalizada: Usted proporciona su identificador personalizado al SDK de Kameleoon mediante el método
addData(). En este método, pasará la clave de bucketing personalizada que haya elegido como un objeto CustomData. Aquí, newVisitorCode hace referencia al identificador que desea usar para el bucketing (por ejemplo, el nuevo userId o accountId).
Para que la clave de bucketing personalizada funcione correctamente, también debe definirse y configurarse para el feature flag durante el proceso de creación o edición del flag. Sin esta configuración correspondiente, el bucketing del SDK no aplicará su clave personalizada. Para obtener instrucciones detalladas sobre cómo configurar esto en Kameleoon, consulte este artículo.
- Lógica de bucketing: Una vez que se proporciona una clave de bucketing personalizada a través del método
addData(), todos los cálculos de hash para asignar usuarios a las variaciones utilizarán este newVisitorCode (su clave personalizada) en lugar del visitorCode predeterminado. Usar el newVisitorCode significa que la decisión de bucketing queda ligada a su identificador personalizado, garantizando asignaciones consistentes en los diversos contextos en los que esté presente ese identificador.
- Seguimiento de datos y analítica: Es crucial tener en cuenta que, aunque el
newVisitorCode (su clave personalizada) se utiliza para las decisiones de bucketing, todos los datos posteriores (eventos de seguimiento y conversiones, por ejemplo) se envían y se asocian con el visitorCode original. Esta separación garantiza que su analítica refleje con precisión los recorridos e interacciones individuales de los usuarios dentro del contexto más amplio de su experimento, incluso cuando el bucketing se realiza a un nivel superior (como una cuenta) o a través de varios dispositivos/sesiones. Sus datos originales del visitante permanecen intactos para una elaboración de informes completa.
Requisitos técnicos
Para utilizar eficazmente una clave de bucketing personalizada:
- La clave debe ser un
String.
- Debe ser única para la entidad que pretende agrupar (por ejemplo, si utiliza un
userId, el ID de cada usuario debe ser único).
- La clave debe estar disponible para el SDK en el momento exacto en que se evalúa la decisión del feature flag para ese usuario o solicitud.
Condiciones de segmentación
Los SDKs de Kameleoon admiten una variedad de condiciones de segmentación predefinidas que puede usar para segmentar a los usuarios en sus campañas. Para ver la lista de condiciones que admite este SDK, consulte usar el historial de visitas para segmentar a los usuarios.
También puede utilizar sus propios datos externos para segmentar a los usuarios.
Error Handling
All methods of the Kameleoon SDK can throw only KameleoonException or its documented inherited exceptions (listed in the Exceptions Thrown section for each method).
These exceptions are expected behavior of the SDK. If you want to handle specific scenarios differently, you can catch individual inherited exceptions; otherwise, catching KameleoonException will handle all SDK‑related errors.
Although our unit and integration tests confirm that the SDK never throws Exception or RuntimeException, we understand that patching SDK versions on Android can be difficult, and unexpected issues may arise from third‑party libraries that could throw a RuntimeException. To prevent your application from crashing in such rare cases, we recommend that you also catch Exception (or RuntimeException) as an additional safeguard. This is strictly a precaution and not an expected behavior of the SDK.
Por ejemplo:
try {
// Calling a method of the SDK
} catch (KameleoonException e) {
// Handling expected exceptions
} catch (Exception e) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
try {
// Calling a method of the SDK
} catch (e: KameleoonException) {
// Handling expected exceptions
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
Registro de eventos
El SDK genera registros que reflejan diversos procesos internos y problemas.
Niveles de registro
El SDK admite la configuración para limitar el registro mediante un nivel de log.
// The `NONE` log level does not allow logging.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.NONE);
// The `ERROR` log level only allows logging issues that may affect the SDK's main behaviour.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.ERROR);
// The `WARNING` log level allows logging issues which may require additional attention.
// It extends the `ERROR` log level.
// The `WARNING` log level is a default log level.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.WARNING);
// The `INFO` log level allows logging general information on the SDK's internal processes.
// It extends the `WARNING` log level.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.INFO);
// The `DEBUG` level logs additional details about the SDK’s internal processes and extends the `INFO` level
// with more granular. diagnostic output.
// This information is not intended for end-user interpretation but can be sent to our support team
// to assist with internal troubleshooting.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.DEBUG);
// The `NONE` log level allows no logging.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.NONE)
// The `ERROR` log level allows to log only issues that may affect the SDK's main behaviour.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.ERROR)
// The `WARNING` log level allows to log issues which may require an attention.
// It extends the `ERROR` log level.
// The `WARNING` log level is a default log level.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.WARNING)
// The `INFO` log level allows to log general information on the SDK's internal processes.
// It extends the `WARNING` log level.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.INFO)
// The `DEBUG` log level allows to log extra information on the SDK's internal processes.
// It extends the `INFO` log level.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.DEBUG)
Gestión personalizada de los registros
El SDK escribe sus registros en la salida de la consola de forma predeterminada. Este comportamiento puede anularse.
El filtrado por nivel de log se realiza de forma independiente de la lógica de gestión de los registros.
public class CustomLogger implements com.kameleoon.logging.Logger {
// `log` method accepts logs from the SDK
@Override
public void log(com.kameleoon.logging.LogLevel level, String message) {
// Custom log handling logic here. For example:
switch (level) {
case ERROR:
android.util.Log.e("your-log-tag", message);
break;
case WARNING:
android.util.Log.w("your-log-tag", message);
break;
case INFO:
android.util.Log.i("your-log-tag", message);
break;
case DEBUG:
android.util.Log.d("your-log-tag", message);
break;
default:
}
}
}
// Log level filtering is applied separately from log handling logic.
// The custom logger will only accept logs that meet or exceed the specified log level.
// Ensure the log level is set correctly.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.DEBUG); // Optional, defaults to `LogLevel.WARNING`.
com.kameleoon.logging.KameleoonLogger.setLogger(new CustomLogger());
class CustomLogger : com.kameleoon.logging.Logger {
override fun log(level: com.kameleoon.logging.LogLevel, message: String) {
// Custom log handling logic here. For example:
when (level) {
com.kameleoon.logging.LogLevel.ERROR -> android.util.Log.e("your-log-tag", message)
com.kameleoon.logging.LogLevel.WARNING -> android.util.Log.w("your-log-tag", message)
com.kameleoon.logging.LogLevel.INFO -> android.util.Log.i("your-log-tag", message)
com.kameleoon.logging.LogLevel.DEBUG -> android.util.Log.d("your-log-tag", message)
else -> {
// Optional: handle default case if needed
}
}
}
}
// Log level filtering is applied separately from log handling logic.
// The custom logger will only accept logs that meet or exceed the specified log level.
// Ensure the log level is set correctly.
com.kameleoon.logging.KameleoonLogger.setLogLevel(com.kameleoon.logging.LogLevel.DEBUG) // Optional, defaults to `LogLevel.WARNING`.
com.kameleoon.logging.KameleoonLogger.setLogger(CustomLogger())
Passing the visitor code to a WebView
In some cases, you may need to pass the visitor code from the native application to a WebView that uses Engine.js or the web JavaScript or React SDKs. The following example demonstrates the recommended way to achieve this:
Kotlin
Kotlin (Jetpack Compose)
Java
class WebViewActivity : AppCompatActivity() {
private var webView: WebView? = null
private val DEFAULT_URL = "https://example.com"
private val COOKIE_NAME = "kameleoonVisitorCode"
private val COOKIE_DOMAIN = ".example.com"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
webView = WebView(this).also { webView ->
setContentView(webView)
configureWebView(DEFAULT_URL, kameleoonClient)
webView.loadUrl(DEFAULT_URL)
}
}
private fun configureWebView(url: String, kameleoonClient: KameleoonClient) {
CookieManager.getInstance().apply {
setCookie(
url,
"$COOKIE_NAME=${kameleoonClient.visitorCode}; Domain=$COOKIE_DOMAIN; Path=/; Secure"
)
flush()
}
}
}
@Composable
fun KameleoonCookieWebView(url: String, kameleoonClient: KameleoonClient) {
AndroidView(
factory = { context ->
WebView(context).apply {
configureWebView(url, kameleoonClient)
loadUrl(url)
}
},
)
}
private fun WebView.configureWebView(url: String, kameleoonClient: KameleoonClient) {
val COOKIE_NAME = "kameleoonVisitorCode"
val COOKIE_DOMAIN = ".example.com"
CookieManager.getInstance().apply {
setCookie(
url,
"$COOKIE_NAME=${kameleoonClient.visitorCode}; Domain=$COOKIE_DOMAIN; Path=/; Secure"
)
flush()
}
}
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
private static final String DEFAULT_URL = "https://example.com";
private static final String COOKIE_NAME = "kameleoonVisitorCode";
private static final String COOKIE_DOMAIN = ".example.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
setContentView(webView);
configureWebView(webView, DEFAULT_URL, kameleoonClient);
webView.loadUrl(url);
}
private void configureWebView(WebView webView, String url, KameleoonClient kameleoonClient) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(
url,
COOKIE_NAME + "=" + kameleoonClient.getVisitorCode() + "; Domain=" + COOKIE_DOMAIN + "; Path=/; Secure"
);
cookieManager.flush();
}
}
Referencia
This is the full reference documentation for the Kameleoon Android SDK.
Inicialización
Once you have installed the SDK in your application, the first step is initializing Kameleoon. All of your application’s interactions with the SDK, such as triggering an experiment, are accomplished using this Kameleoon client object.
create()
Llame a este método antes que a cualquier otro para inicializar el SDK. Este método se encuentra en com.kameleoon.KameleoonClientFactory. Su aplicación realiza todas las interacciones con el SDK utilizando el objeto KameleoonClient resultante que este método crea.
Puede personalizar el comportamiento del SDK (por ejemplo, el entorno, las credenciales, etc.) proporcionando un objeto de configuración. De lo contrario, el SDK intentará encontrar y usar su archivo de configuración.
String siteCode = "a8st4f59bj";
try {
// pass client configuration and visitorCode as arguments
KameleoonClientConfig config = new KameleoonClientConfig.Builder()
.refreshIntervalMinute(15) // in minutes, 1 hour by default, optional
.defaultTimeoutMillisecond(10_000) // in milliseconds, 10 seconds by default, optional
.dataExpirationIntervalMinute(1440 * 365) // in minutes, infinity by default, optional
.isUniqueIdentifier(false) // optional, false by default. Set to true if the visitorCode corresponds to your customer's unique userId.
.environment("staging") // optional
.build();
String visitorCode = "yourVisitorCode";
KameleoonClient kameleoonClient = KameleoonClientFactory.create(siteCode, visitorCode, config, getApplicationContext());
} catch (KameleoonException.SiteCodeIsEmpty | KameleoonException.VisitorCodeInvalid exception) {
// Exception indicates that the provided siteCode is empty or the visitorCode is invalid
} catch (Exception exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
try {
// generate visitorCode automatically and read client configuration from a file 'kameleoon-client.properties'
KameleoonClient kameleoonClient = KameleoonClientFactory.create(siteCode, getApplicationContext());
} catch (KameleoonException.SiteCodeIsEmpty | KameleoonException.VisitorCodeInvalid exception) {
// Exception indicates that the provided siteCode is empty or the visitorCode is invalid
} catch (Exception exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
val siteCode = "a8st4f59bj"
try {
// pass client configuration and visitor code as arguments
val config = KameleoonClientConfig.Builder()
.refreshIntervalMinute(15) // in minutes, 1 hour by default, optional
.defaultTimeoutMillisecond(10_000) // in milliseconds, 10 seconds by default, optional
.dataExpirationIntervalMinute(1440 * 365) // in minutes, infinity by default, optional
.environment("staging") // optional
.build();
val visitorCode = "yourVisitorCode"
val kameleoonClient = KameleoonClientFactory.create(siteCode, visitorCode, config, applicationContext)
} catch (e: KameleoonException.SiteCodeIsEmpty) {
// Exception indicating that the provided siteCode is empty
} catch (e: KameleoonException.VisitorCodeInvalid) {
// Exception indicating that the provided visitorCode is invalid
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
try {
// generate visitorCode automatically and read client configuration from the 'kameleoon-client.properties' file
val kameleoonClient = KameleoonClientFactory.create(siteCode, applicationContext)
} catch (e: KameleoonException.SiteCodeIsEmpty) {
// Exception indicating that the provided siteCode is empty
} catch (e: KameleoonException.VisitorCodeInvalid) {
// Exception indicating that the provided visitorCode is invalid
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
Argumentos
| Name | Type | Description | Default |
|---|
| siteCode (obligatorio) | String | A unique key identifying the Kameleoon project used with the SDK. | |
| visitorCode (opcional) | String | An optional visitor identifier. If available, use your internal user ID; otherwise, the SDK will generate one automatically. | nil |
| config (opcional) | KameleoonClientConfig | Optional SDK configuration. If provided, it is used instead of reading from an external configuration file. If not provided, the SDK attempts to read the file, but if the file is missing, it falls back to default behavior. | nil |
| applicationContext (obligatorio) | Context | The application’s context. | |
Valor de retorno
| Tipo | Descripción |
|---|
KameleoonClient | Una instancia de la clase KameleoonClient que su aplicación puede utilizar para gestionar sus experimentos y feature flags. |
Excepciones lanzadas
| Tipo | Descripción |
|---|
VisitorCodeInvalid | Excepción que indica que el visitor code proporcionado no es válido. It is either empty or longer than 255 characters. |
SiteCodeIsEmpty | Excepción que indica que el site code especificado es una cadena vacía, lo que no es un valor válido. |
isReady()
For mobile SDKs, the Kameleoon Client can’t initialize immediately, as it must perform a server call to retrieve the current configuration for the active feature flags. Use este método para check if the SDK is ready by calling isReady() before triggering any feature flags.
Alternatively, you can use a callback (see the runWhenReady() method for details).
boolean ready = kameleoonClient.isReady();
val ready = kameleoonClient.isReady
Valor de retorno
| Type | Description |
|---|
| boolean | Boolean representing the SDK’s status. true if client is fully initialized and false if it is not yet ready to be used. |
runWhenReady()
- 🔄 Performs an asynchronous request (if the configuration is outdated or missing)
For mobile SDKs, the KameleoonClient cannot initialize immediately, as it must perform a server call to retrieve the current configuration for all feature flags. Use the runWhenReady() method to handle the time until the client is ready for use. Additionally, you can set a maximum timeout period to control how long the client will wait before it becomes ready.
If result.getOrThrow()=true, the KameleoonClient is initialized and ready, and the feature flags will be triggered with their respective variations. If the result is false or a timeout occurs, the initialization will not complete successfully.
El callback o el código basado en corutinas debe incluir lógica para aplicar la variación de referencia, ya que el usuario será excluido del feature flag si se produce un timeout.
Since the initial configuration may require a server call, this mechanism is asynchronous. Therefore, you should either:
- Provide a
completion callback as an argument to the method to ensure you are notified when the KameleoonClient is fully initialized and ready for use.
- Use coroutines to handle asynchronous operations.
Java
Kotlin
Kotlin (Coroutines)
kameleoonClient.runWhenReady(1000, result -> {
int recommendedProductsNumber = 5; // Default control number for recommended products
try {
if (result.getOrThrow()) {
Variation variation = kameleoonClient.getVariation("featureKey");
recommendedProductsNumber = (int) variation.getVariables().get("recommendedProductsNumber").getValue();
}
} catch (Exception ignored) {
// The user will not be included in the experiment results and should see the control variation
}
applyVariation(recommendedProductsNumber);
});
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| timeoutMilliseconds (opcional) | int | Timeout for the initialization process | defaultTimeoutMillisecond or default_timeout_millisecond |
| completion (obligatorio) | ResultCompletion<Boolean, TimeoutException> | The callback that processes the received data. | |
kameleoonClient.runWhenReady(1000) { result ->
val recommendedProductsNumber = runCatching {
if (result.getOrThrow()) {
val variation = kameleoonClient.getVariation("featureKey")
variation.variables["recommendedProductsNumber"]?.value as Int
} else {
null // The user will not be included in the experiment results and should see the control variation
}
}.getOrDefault(5) // Default control number for recommended products
applyVariation(recommendedProductsNumber)
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| timeoutMilliseconds (opcional) | Int | Timeout for the initialization process | defaultTimeoutMillisecond or default_timeout_millisecond |
| completion (obligatorio) | ResultCompletion<Boolean, TimeoutException> | The callback that processes the received data. | |
A common mistake is using suspended functions inside mapCatching, runCatching, or a try-catch block without properly re-throwing CancellationException, which can interfere with coroutine cancellation. To ensure correct behavior, try to avoid calling suspend functions within these blocks.
viewModelScope.launch {
kameleoonClient.runWhenReady(1000).getOrNull() ?: return@launch
val recommendedProductsNumber = runCatching {
val variation = kameleoonClient.getVariation("featureKey")
variation.variables["recommendedProductsNumber"]?.value as? Int
}.getOrNull() ?: 5 // Default control number for recommended products
applyVariation(recommendedProductsNumber)
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| timeoutMilliseconds (opcional) | Int | Timeout for the initialization process | defaultTimeoutMillisecond or default_timeout_millisecond |
Valor de retorno
| Tipo | Descripción |
|---|
Result<Unit> | A Kotlin Result that contains either the success result or the exception that occurred. |
Feature flags y variaciones
isFeatureActive()
- 📨 Envía datos de seguimiento a Kameleoon (dependiendo del parámetro
track)
This method was previously called activateFeature, which was removed in SDK version 4.0.0.
Llame a este método para activar un feature toggle. Este método acepta un featureKey como argumento obligatorio para comprobar si la funcionalidad especificada estará activa para un visitante.
If the visitor has never been associated with this feature flag, the method returns a random boolean value (true if the visitor should be shown this feature, otherwise false). If the visitor is already registered with this feature flag, this method returns the previous featureFlag value.
Ensure you properly set up error handling como se muestra en el ejemplo code to catch potential exceptions.
Kameleoon uses tracking to count sessions and visitors when you call certain methods, such as isFeatureActive(), getVariation() or getVariations().Use el valor predeterminado true para el parámetro track cuando exponga a los visitantes a una variación y necesite contarlos. Establezca el parámetro track en false solo si llama a estos métodos antes de exponer a los visitantes.Por ejemplo, if you call getVariations() to retrieve all variations before you expose visitors, set the track parameter to false. This setting prevents Kameleoon from prematurely counting a session. You can then trigger tracking later when you explicitly expose the visitor.Kameleoon sends tracking data every second by default. You can configure this interval up to five seconds using the tracking interval configuration option. Kameleoon groups tracking events into a single session as long as the interval between events is less than 30 minutes. If more than 30 minutes elapse between tracking events, Kameleoon counts the events as separate sessions. A visit appears in your reports 30 minutes after the last recorded event in the session.
String featureKey = "new_checkout";
boolean hasNewCheckout = false;
try {
hasNewCheckout = kameleoonClient.isFeatureActive(featureKey);
// disabling tracking
hasNewCheckout = kameleoonClient.isFeatureActive(featureKey, false);
} catch (KameleoonException.SDKNotReady e) {
// Exception indicating that the SDK has not completed its initialization yet.
} catch (KameleoonException.FeatureNotFound e) {
// SDK not initialized, or feature toggle not yet activated in Kameleoon - we consider the feature inactive
} catch (Exception exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
if (hasNewCheckout)
{
// Implement new checkout code here
}
val featureKey = "new_checkout"
var hasNewCheckout = false
try {
hasNewCheckout = kameleoonClient.isFeatureActive(featureKey)
// disabling tracking
hasNewCheckout = kameleoonClient.isFeatureActive(featureKey, false)
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicating that the SDK has not completed its initialization yet.
hasNewCheckout = false
} catch (e: KameleoonException.FeatureNotFound) {
// SDK not initialized or feature toggle not yet activated on Kameleoon's side - we consider the feature inactive
hasNewCheckout = false
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
hasNewCheckout = false
}
if (hasNewCheckout) {
// Implement new checkout code here
}
The isFeatureActive() method evaluates the served variant, not the master flag state. If you exclude rules, the method uses the Then, for everyone else serve default state. If you select Off for this default state, the method always returns false even when the master feature flag is On.
Argumentos
| Name | Type | Description |
|---|
| featureKey | String | Unique key of the feature you want to expose to a user. Este campo es obligatorio. |
| track | boolean | An optional parameter to enable or disable tracking of the feature evaluation (true by default). |
Valor de retorno
| Tipo | Descripción |
|---|
| Boolean | Value of the feature that is registered for a visitor. |
Excepciones lanzadas
| Type | Description |
|---|
| SDKNotReady | Exception indicating that the SDK has not completed its initialization. |
| FeatureNotFound | Exception indicating that the requested feature ID was not found in the SDK’s internal configuration. This exception usually means the feature flag has not been activated on the Kameleoon side (but code implementing the feature is already deployed in the application). |
getVariation()
- 📨 Envía datos de seguimiento a Kameleoon (dependiendo del parámetro
track)
Recupera la Variation asignada a un visitante dado para un feature flag específico.
Este método toma un visitorCode and featureKey as mandatory arguments. The track argument is optional and defaults to true.
Devuelve la Variation asignada al visitante. Si el visitante no está asociado con ninguna regla de feature flag, el método devuelve la Variation predeterminada para el feature flag dado.
Asegúrese de implementar un manejo de errores adecuado en su código para gestionar las posibles excepciones.
La variación predeterminada se refiere a la variación asignada a un visitante cuando no coincide con ninguna regla de entrega predefinida para un feature flag. In other words, it is the fallback variation applied to all users who are not targeted by specific rules. Se representa como la variación en la sección “Then, for everyone else…” de la interfaz de administración.
final String featureKey = "featureKey";
Variation variation = null;
try {
variation = kameleoonClient.getVariation(featureKey);
// disabling tracking
variation = kameleoonClient.getVariation(featureKey, false);
} catch (KameleoonException.SDKNotReady ex) {
// Exception indicating that the SDK has not completed its initialization yet.
} catch (KameleoonException.FeatureNotFound ex) {
// The feature key is not in the configuration file that has been fetched by the SDK.
} catch (KameleoonException.FeatureEnvironmentDisabled ex) {
// The feature flag is disabled for the environment.
}
if (variation != null) {
String title = (String) variation.getVariables().get("title").getValue();
switch (variation.getKey()) {
case "on":
// Main variation key is selected for visitorCode
break;
case "alternative_variation":
// Alternative variation key
break;
default:
// Default variation key
break;
}
}
val featureKey = "featureKey"
var variation: Variation? = null
try {
variation = kameleoonClient.getVariation(featureKey)
// disabling tracking
variation = kameleoonClient.getVariation(featureKey, false)
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicating that the SDK has not completed its initialization yet.
} catch (e: KameleoonException.FeatureNotFound) {
// The feature key is not yet in the configuration file that has been fetched by the SDK.
} catch (e: KameleoonException.FeatureEnvironmentDisabled) {
// The feature flag is disabled for the environment
}
val title = variation?.variables?.get("title")?.value as? String
when (variation?.key) {
"on" -> {
// Main variation key is selected for visitorCode
}
"alternative_variation" -> {
// Alternative variation key
}
else -> {
// Default variation key
}
}
Argumentos
| Name | Type | Description | Default |
|---|
| visitorCode (obligatorio) | String | Identificador único del visitante. | |
| featureKey (obligatorio) | String | Clave de la funcionalidad que desea exponer a un visitante. | |
| track (opcional) | boolean | Parámetro opcional para habilitar o deshabilitar el seguimiento de la evaluación de la funcionalidad. | true |
Valor de retorno
| Tipo | Descripción |
|---|
Variation | An assigned Variation to a given visitor for a specific feature flag. |
Excepciones lanzadas
| Tipo | Descripción |
|---|
VisitorCodeInvalid | Excepción que indica que el visitor code proporcionado no es válido. It is either empty or longer than 255 characters. |
FeatureNotFound | Excepción que indica que la clave de funcionalidad solicitada no se encontró en la configuración interna del SDK. Esto suele significar que el feature flag no está activado en la aplicación de Kameleoon (pero el código que implementa la funcionalidad ya está desplegado en la aplicación). |
FeatureEnvironmentDisabled | Excepción que indica que el feature flag está deshabilitado para el entorno actual del visitante (por ejemplo, production, staging o development). |
getVariations()
- 📨 Envía datos de seguimiento a Kameleoon (dependiendo del parámetro
track)
Recupera un map de objetos Variation asignados a un visitante dado para todos los feature flags.
Este método itera sobre todos los feature flags disponibles y devuelve la Variation asignada para cada flag asociado con el visitante especificado. It takes onlyActive and track as optional arguments.
- Si
onlyActive se establece en true, el método getVariations() devolverá las variaciones de los feature flags siempre que el usuario no esté asignado a la variación off.
- El parámetro
track controla si el método realizará el seguimiento de las asignaciones de variación. De forma predeterminada, it is set to true. Si se establece en false, el seguimiento estará deshabilitado.
El map devuelto consta de claves de feature flags como claves y su Variation correspondiente como valores. Si no se asigna ninguna variación para un feature flag, el método devuelve la Variation predeterminada para ese flag.
Se debe implementar un manejo de errores adecuado para gestionar las posibles excepciones.
La variación predeterminada se refiere a la variación asignada a un visitante cuando no coincide con ninguna regla de entrega predefinida para un feature flag. In other words, it is the fallback variation applied to all users who are not targeted by specific rules. Se representa como la variación en la sección “Then, for everyone else…” de la interfaz de administración.
try {
Map<String, Variation> variations = kameleoonClient.getVariations();
// only active variations
Map<String, Variation> variations = kameleoonClient.getVariations(true);
// disable tracking
Map<String, Variation> variations = kameleoonClient.getVariations(false, false);
} catch (KameleoonException.SDKNotReady ex) {
// Exception indicating that the SDK has not completed its initialization yet.
}
try {
val variations = kameleoonClient.getVariations()
// only active variations
val variations = kameleoonClient.getVariations(true)
// disable tracking
val variations = kameleoonClient.getVariations(false, false)
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicating that the SDK has not completed its initialization yet.
}
Argumentos
| Name | Type | Description | Default |
|---|
| onlyActive (opcional) | boolean | Parámetro opcional que indica si se deben devolver las variaciones para los feature flags activos (true) o todos (false). | false |
| track (opcional) | boolean | Parámetro opcional para habilitar o deshabilitar el seguimiento de la evaluación de la funcionalidad. | true |
Valor de retorno
| Tipo | Descripción |
|---|
Map<String, Variation> | Map que contiene los objetos Variation asignados de los feature flags utilizando las claves de las funcionalidades correspondientes. |
Excepciones lanzadas
| Tipo | Descripción |
|---|
SDKNotReady | Indicates that the SDK is not yet fully initialized. |
setForcedVariation()
El método permite you to programmatically assign a specific Variation to a user, bypassing the standard evaluation process. Esto es especialmente valioso para experimentos controlados donde la lógica de evaluación habitual no es necesaria o debe omitirse. It can also be helpful in scenarios like debugging or custom testing.
Cuando se establece una variación forzada, esta anula la lógica de evaluación en tiempo real de Kameleoon. Processes like segmentation, targeting conditions, and algorithmic calculations are skipped. To preserve segmentation and targeting conditions during an experiment, set forceTargeting=false instead.
A forced variation is treated the same as an evaluated variation. It is tracked in analytics and stored in the user context like any standard evaluated variation, ensuring consistency in reporting.
El método puede lanzar excepciones bajo ciertas condiciones (por ejemplo, parámetros no válidos, contexto del usuario o problemas internos). Proper exception handling is essential to ensure that your application remains stable and resilient.
final int experimentId = 9516;
try {
// Forcing the variation "on" for the experiment 9516 for the visitor
kameleoonClient.setForcedVariation(experimentId, "on");
// Forcing the variation "on" while preserving segmentation and targeting conditions during the experiment
kameleoonClient.setForcedVariation(experimentId, "on", false);
// Resetting the forced variation for the experiment 9516 for the visitor
kameleoonClient.setForcedVariation(experimentId, null);
} catch (KameleoonException e) {
// Handling the exception
}
val experimentId = 9516
try {
// Forcing the variation "on" for the experiment 9516 for the visitor
kameleoonClient.setForcedVariation(experimentId, "on")
// Forcing the variation "on" while preserving segmentation and targeting conditions during the experiment
kameleoonClient.setForcedVariation(experimentId, "on", false)
// Resetting the forced variation for the experiment 9516 for the visitor
kameleoonClient.setForcedVariation(experimentId, null)
} catch (e: KameleoonException) {
// Handling the exception
}
Argumentos
| Name | Type | Description | Default |
|---|
| experimentId (obligatorio) | int | Experiment Id que será segmentado y seleccionado durante el proceso de evaluación. | |
| variationKey (obligatorio) | String | Variation Key correspondiente a una Variation que debe forzarse como valor devuelto para el experimento. Si el valor es null, la variación forzada se restablecerá. | |
| forceTargeting (opcional) | boolean | Indica si la segmentación para el experimento debe forzarse y omitirse (true) o aplicarse como en el proceso de evaluación estándar (false). | true |
Excepciones lanzadas
| Tipo | Descripción |
|---|
SDKNotReady | Indicates that the SDK is not yet fully initialized. |
FeatureExperimentNotFound | Excepción que indica que el experiment id solicitado no se encontró en la configuración interna del SDK. Esto suele ser normal y significa que el experimento correspondiente a la regla todavía no se ha activado del lado de Kameleoon. |
FeatureVariationNotFound | Excepción que indica que la variation key (id) solicitada no se ha encontrado en la configuración interna del SDK. Esto suele ser normal y significa que el experimento correspondiente a la variación todavía no se ha activado del lado de Kameleoon. |
En la mayoría de los casos, solo es necesario gestionar el error básico KameleoonException, como se muestra en el ejemplo. However, if different types of errors require a response, handle each one separately based on specific requirements. Additionally, for enhanced reliability, general language errors can be handled by including Exception.
evaluateAudiences()
- 📨 Envía datos de seguimiento a Kameleoon
Este método evalúa a los visitantes con respecto a todos los segmentos disponibles de Audiences Explorer y realiza el seguimiento de los que coinciden.
evaluateAudiences() should be called after all relevant visitor data has been set or updated, and just before getting a feature variation or checking a feature flag. Este enfoque garantiza que el visitante se evalúe con los datos más recientes disponibles, lo que permite una asignación precisa de audiencia basada en todos los criterios.
After calling this method, you can perform a detailed analysis of segment performance in Audiences Explorer.
try {
kameleoonClient.evaluateAudiences();
} catch (KameleoonException e) {
// Handling the exception
}
try {
kameleoonClient.evaluateAudiences()
} catch (e: KameleoonException) {
// Handling the exception
}
Excepciones lanzadas
| Tipo | Descripción |
|---|
SDKNotReady | Indicates that the SDK is not yet fully initialized. |
En la mayoría de los casos, solo es necesario gestionar el error básico KameleoonException, como se muestra en el ejemplo. However, if different types of errors require a response, handle each one separately based on specific requirements. Additionally, for enhanced reliability, general language errors can be handled by including Exception.
getDataFile()
To evaluate all feature flags, use getVariations(). Este método es más eficiente que llamar a DataFile e iterar por los flags con getVariation().
Devuelve la configuración actual del SDK como un objeto DataFile.
try {
DataFile dataFile = kameleoonClient.getDataFile();
} catch (KameleoonException.SDKNotReady e) {
// Exception indicates that the SDK has not completed its initialization yet.
} catch (Exception e) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
try {
val dataFile = kameleoonClient.dataFile
val dateModified = dataFile.dateModified
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicates that the SDK has not completed its initialization yet.
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
Valor de retorno
| Tipo | Descripción |
|---|
DataFile | El DataFile que contiene la configuración del SDK |
Errores thrown
| Tipo | Descripción |
|---|
SDKNotReady | Indicates that the SDK is not yet fully initialized. |
Goals
trackConversion()
- 📨 Envía datos de seguimiento a Kameleoon
Use este método para realizar el seguimiento de conversiones. Este método requiere goalId para hacer seguimiento de la conversión en este objetivo en particular. Además, este método también acepta los argumentos revenue, metadata y negative.
El método trackConversion() no devuelve ningún valor. Este método no es bloqueante, ya que la llamada al servidor se realiza de forma asíncrona.
final int goalId = 83023;
kameleoonClient.trackConversion(goalId); // default revenue
kameleoonClient.trackConversion(goalId, 10); // provided revenue == 10
kameleoonClient.trackConversion(goalId, new CustomData(1, "metadata")); // Add metadata
val goalId = 83023
kameleoonClient.trackConversion(goalId) // default revenue
kameleoonClient.trackConversion(goalId, 10f) // provided revenue == 10
kameleoonClient.trackConversion(goalId, CustomData(1, "metadata")) // Add metadata
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| goalId (obligatorio) | int | ID del objetivo. | |
| revenue (opcional) | float | Ingreso de la conversión. | 0 |
| negative (opcional) | boolean | Define si el ingreso es positivo o negativo. | false |
| metadata (opcional) | CustomData... | Metadatos de la conversión. Must be defined beforehand in the Kameleoon App. | new CustomData[0] |
metadata values are accessible through raw data exports and the results page.Si se proporciona el parámetro metadata, Kameleoon utilizará estos valores especificados para la conversión actual en lugar de lo recopilado previamente mediante el método addData(). Si se omite el parámetro, Kameleoon utilizará los últimos valores rastreados para esos CustomData antes de la conversión y dentro de la misma visita.Kameleoon will only consider the metadata values that are explicitly passed as parameters to the trackConversion() method.En el siguiente ejemplo, Kameleoon will associate the conversion only with the custom data value explicitly provided as a parameter (here: index 5 with the value ‘Amex Credit Card’).kameleoonClient.addData(new CustomData(5, "Credit Card"), new CustomData(9, "Express Delivery"));
kameleoonClient.trackConversion(1000, new CustomData(5, "Amex Credit Card"));
kameleoonClient.addData(CustomData(5, "Credit Card"), CustomData(9, "Express Delivery"))
kameleoonClient.trackConversion(1000, CustomData(5, "Amex Credit Card"))
Events
onUpdateConfiguration()
This method was previously named updateConfigurationHandler, which was removed in SDK version 4.0.0 release.
El método onUpdateConfiguration() le permite gestionar el evento cuando la configuración tiene datos actualizados. Toma un parámetro de entrada, completion. El completion que se llamará cuando la configuración se actualice mediante un evento de configuración en tiempo real.
Argumentos
| Nombre | Tipo | Descripción |
|---|
| completion | ResultCompletion<Long, Exception> | The handler that will be called when the configuration is updated using a real-time configuration event. |
kameleoonClient.onUpdateConfiguration(result -> {
if (result.isSuccess()) {
// result value contains the value of Unix time (number of seconds that have elapsed since January 1, 1970) when configuration was updated
}
});
kameleoonClient.onUpdateConfiguration { result ->
if (result.isSuccess) {
// result value contains the value of Unix time (number of seconds that have elapsed since January 1, 1970) when configuration was updated
}
}
Datos del visitante
getVisitorCode()
Returns unique visitor code used in SDK.
String visitorCode = kameleoonClient.getVisitorCode();
val visitorCode = kameleoonClient.visitorCode
Valor de retorno
| Tipo | Descripción |
|---|
String | String representing a unique visitor code used in SDK. |
addData()
El método addData() añade datos de segmentación al almacenamiento para que otros métodos puedan utilizar los datos para decidir si segmentar o no al visitante actual.
El método addData() no devuelve ningún valor y no interactúa por sí mismo con los servidores backend de Kameleoon. En su lugar, todos los datos declarados se guardan para su transmisión futura mediante el método flush(). Este enfoque reduce el número de llamadas al servidor, ya que los datos se agrupan habitualmente en una única llamada al servidor que desencadena el flush().
El método trackConversion() también envía cualquier dato previamente asociado, al igual que flush(). The same holds true for getVariation() and getVariations() methods if an experimentation rule is triggered.
Cada visitante solo puede tener una instancia de datos asociados para la mayoría de los tipos de datos. However, CustomData is an exception. Visitors can have one instance of associated CustomData per index.
// Add a single data item (tracked by default)
kameleoonClient.addData(new CustomData(1, "value"));
// Add multiple data items (tracked by default)
kameleoonClient.addData(new CustomData(1, "value"), new Geolocation("France"));
// Add multiple data items stored locally for targeting only (not sent to the Kameleoon Data API)
kameleoonClient.addData(false, new CustomData(1, "value"), new Geolocation("France"));
// Add a single data item (tracked by default)
kameleoonClient.addData(CustomData(1, "value"))
// Add multiple data items (tracked by default)
kameleoonClient.addData(CustomData(1, "value"), Geolocation("France"))
// Add multiple data items stored locally for targeting only (not sent to the Kameleoon Data API)
kameleoonClient.addData(false, CustomData(1, "value"), Geolocation("France"))
Argumentos
| Nombre | Tipo | Descripción | Valor predeterminado |
|---|
| track (opcional) | boolean | Especifica si los datos añadidos son aptos para el seguimiento. Cuando se establece en false, los datos se almacenan localmente y se utilizan solo para la evaluación de segmentación; no se envían a la Data API de Kameleoon. | true |
| data (obligatorio) | Data... | Colección de tipos de datos de Kameleoon. | |
flush()
- 📨 Envía datos de seguimiento a Kameleoon
flush() takes the Kameleoon data associated with a visitor, and sends a tracking request along with all of the data that were added previously using the addData() method that has not yet been sent when calling one of these methods. flush() is non-blocking, as the server call is made asynchronously.
flush() provides control over when the data associated with a visitor is sent to the servers. For instance, if addData() is called a dozen times, sending data to the server after each addData() invocation would be inefficient. Call flush() once at the end.
El método flush() usa visitorCode como identificador único del visitante, lo cual es útil para la experimentación entre dispositivos. Si establece el parámetro de configuración isUniqueIdentifier en true, el SDK vincula los datos vaciados con el visitante asociado al identificador especificado.
kameleoonClient.addData(Device.phone());
kameleoonClient.addData(new Conversion(32, 10f, false));
kameleoonClient.flush(); // Interval tracking (most performant tracking method)
kameleoonClient.flush(true); // Instant tracking
kameleoonClient.addData(Device.phone())
kameleoonClient.addData(Conversion(32, 10f, false))
kameleoonClient.flush() // Interval tracking (most performant tracking method)
kameleoonClient.flush(true) // Instant tracking
Argumentos
| Name | Type | Description |
|---|
| instant | boolean | Boolean flag indicating whether the data should be sent instantly (true) or according to the scheduled tracking interval (false). Este campo es opcional. El valor predeterminado es false. |
getRemoteData()
- 🔄 Performs an asynchronous request
Este método se llamaba anteriormente retrieveDataFromRemoteSource, que fue eliminado en la versión 4.0.0 del SDK.
Use este método para retrieve data from a remote Kameleoon server based on the active siteCode and the key argument (or the active visitorCode if the key is omitted). The visitorCode and siteCode are specified in KameleoonClientFactory.create(). Data can be stored quickly and conveniently on highly scalable remote servers using the Kameleoon Data API. The application can then retrieve the data using this method.
Since a server call is required, this mechanism is asynchronous. Therefore, you should either:
- Provide a
completion callback as an argument to the method to ensure you are notified when the data has been successfully fetched.
- Use coroutines for asynchronous handling.
Java
Kotlin
Kotlin (Coroutines)
kameleoonClient.getRemoteData("key", result -> {
try {
JSONObject jsonObject = result.getOrThrow();
// jsonObject contains result of request
} catch (Exception ex) {
// request failed with an exception
}
});
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| key (opcional) | String | The key that the data you’re retrieving is associated with. | null |
| completion (obligatorio) | ResultCompletion<JSONObject, Exception> | The callback that processes the received data. | |
kameleoonClient.getRemoteData("key") { result ->
try {
val jsonObject: JSONObject = result.getOrThrow()
// jsonObject contains result of request
} catch (ex: Exception) {
// request failed with an exception
}
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| key (opcional) | String | The key that the data you’re retrieving is associated with. | null |
| completion (obligatorio) | ResultCompletion<JSONObject, Exception> | The callback that processes the received data. | |
A common mistake is using suspended functions inside mapCatching, runCatching, or a try-catch block without properly re-throwing CancellationException, which can interfere with coroutine cancellation. To ensure correct behavior, try to avoid calling suspend functions within these blocks.
viewModelScope.launch {
val jsonObject = kameleoonClient.getRemoteData("key").getOrNull() ?: return@launch
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| key (opcional) | String | The key that the data you’re retrieving is associated with. | null |
Valor de retorno
| Tipo | Descripción |
|---|
Result<JSONObject> | A Kotlin Result that contains either the fetched value (JSONObject) or the exception that occurred. |
getRemoteVisitorData()
- 🔄 Performs an asynchronous request
getRemoteVisitorData() is an asynchronous method for retrieving Kameleoon Visits Data for the visitor from the Kameleoon Data API. El método añade data to storage for other methods to use when making targeting decisions.
Los datos obtenidos mediante este método desempeñan un papel importante cuando desea:
- utilizar datos recopilados desde otros dispositivos.
- access a user’s history, such as custom data collected during previous visits.
Read this article for a better understanding of possible use cases.
De forma predeterminada, getRemoteVisitorData() automatically retrieves the latest stored custom data with scope=Visitor and attaches it to the visitor without having to call the method addData(). It is particularly useful for synchronizing custom data between multiple devices.Checking only for failed results is recommended. However, if necessary, it can be verified that the data has been added to the visitor and is available for targeting purposes (or for debugging, though using logging is better for debugging). Additionally, data can be managed manually if the shouldAddData=false parameter is passed.
Since a server call is required, this mechanism is asynchronous. Therefore, you should either:
- Provide a
completion callback as an argument to the method to ensure you are notified when the data has been successfully fetched and added to the visitor.
- Use coroutines for asynchronous handling.
Java
Kotlin
Kotlin (Coroutines)
// Visitor data will be fetched and automatically added for `visitorCode`.
kameleoonClient.getRemoteVisitorData(result -> {
if (result.isSuccess()) {
// Data was successfully retrieved from the Kameleoon servers and added to the visitor.
} else {
// The request failed due to an exception.
}
});
// If you only want to fetch data and add it yourself manually, set shouldAddData == `false`.
kameleoonClient.getRemoteVisitorData(false, result -> {
try {
List<Data> visitorData = result.getOrThrow();
// visitorData contains the fetched visitor data from Kameleoon servers, which can be manually added.
} catch (Exception ex) {
// The request failed due to an exception.
}
});
// If you want to fetch custom list of data types
RemoteVisitorDataFilter filter = RemoteVisitorDataFilter.builder()
.previousVisitAmount(25)
.experiments(true)
.conversion(true)
.build();
kameleoonClient.getRemoteVisitorData(filter, result -> {
if (result.isSuccess()) {
// Data was successfully retrieved from the Kameleoon servers and added to the visitor.
} else {
// The request failed due to an exception.
}
});
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| filter (opcional) | RemoteVisitorDataFilter | Filter that selects which data should be retrieved from visit history. De forma predeterminada, the method retrieves CustomData from the current and latest previous visit. | null |
| shouldAddData (opcional) | boolean | Booleano que indica si el método debe añadir automáticamente los datos recuperados para un visitante. | true |
| completion (obligatorio) | ResultCompletion<List<Data>, Exception> | The callback that processes the received visitor data. | |
// Visitor data will be fetched and automatically added to the visitor.
kameleoonClient.getRemoteVisitorData { result ->
if (result.isSuccess) {
// Data was successfully retrieved from the Kameleoon servers and added to the visitor.
} else {
// The request failed due to an exception.
}
}
// If you only want to fetch data and add it yourself manually, set shouldAddData == `false`
kameleoonClient.getRemoteVisitorData(false) { result ->
try {
val visitorData = result.getOrThrow()
// visitorData contains the fetched visitor data from Kameleoon servers, which can be manually added.
} catch (e: Exception) {
// The request failed due to an exception.
}
}
val filter = RemoteVisitorDataFilter.builder()
.previousVisitAmount(25)
.experiments(true)
.conversion(true)
.build()
kameleoonClient.getRemoteVisitorData(filter) { result ->
if (result.isSuccess) {
// Data was successfully retrieved from the Kameleoon servers and added to the visitor.
} else {
// The request failed due to an exception.
}
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| filter (opcional) | RemoteVisitorDataFilter | Filter that selects which data should be retrieved from visit history. De forma predeterminada, the method retrieves CustomData from the current and latest previous visit. | null |
| shouldAddData (opcional) | Boolean | Booleano que indica si el método debe añadir automáticamente los datos recuperados para un visitante. | true |
| completion (obligatorio) | ResultCompletion<List<Data>, Exception> | The callback that processes the received visitor data. | |
A common mistake is using suspended functions inside mapCatching, runCatching, or a try-catch block without properly re-throwing CancellationException, which can interfere with coroutine cancellation. To ensure correct behavior, try to avoid calling suspend functions within these blocks.
// Visitor data will be fetched and automatically added for `visitorCode`
viewModelScope.launch {
kameleoonClient.getRemoteVisitorData() ?: return@launch
}
// If you only want to fetch data and add it yourself manually, set shouldAddData == `false`
viewModelScope.launch {
kameleoonClient.getRemoteVisitorData(shouldAddData = false)
.onSuccess { visitorData ->
// visitorData contains the fetched visitor data from Kameleoon servers, which can be manually added.
}.onFailure { ex ->
// request failed with exception
}
}
viewModelScope.launch {
val filter = RemoteVisitorDataFilter.builder()
.previousVisitAmount(25)
.experiments(true)
.conversion(true)
.build()
// In general, we recommend checking only if the request fails.
kameleoonClient.getRemoteVisitorData(filter) ?: return@launch
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| filter (opcional) | RemoteVisitorDataFilter | Filter that selects which data should be retrieved from visit history. De forma predeterminada, the method retrieves CustomData from the current and latest previous visit. | null |
| shouldAddData (opcional) | Boolean | Booleano que indica si el método debe añadir automáticamente los datos recuperados para un visitante. | true |
Valor de retorno
| Tipo | Descripción |
|---|
Result<List<Data>> | A Kotlin Result that contains either the fetched value (List<Data>) or the exception that occurred. |
Using parameters of RemoteVisitorDataFilter
El método getRemoteVisitorData() ofrece flexibilidad al permitirle definir varios parámetros al recuperar datos de los visitantes. Ya sea que esté segmentando en función de objetivos, experimentos o variaciones, el mismo enfoque se aplica a todos los tipos de datos.
Por ejemplo, suppose you want to retrieve data on visitors who completed a goal “Order transaction”. Puede especificar parámetros dentro del método getRemoteVisitorData() para refinar su segmentación. For instance, if you want to target only users who converted on the goal in their last five visits, you can set the previousVisitAmount parameter to 5 and conversions to true.
La flexibilidad mostrada en este ejemplo no se limita a los datos de objetivos. Puede usar parámetros dentro del método getRemoteVisitorData() para recuperar datos sobre una variedad de comportamientos del visitante.
Here is the list of available RemoteVisitorDataFilter options:| Name | Type | Description | Default |
|---|
| previousVisitAmount (opcional) | int | Número de visitas previas de las que recuperar datos. Number between 1 and 25 | 1 |
| currentVisit (opcional) | boolean | If true, current visit data will be retrieved | true |
| customData (opcional) | boolean | If true, custom data will be retrieved. | true |
| geolocation (opcional) | boolean | If true, geolocation data will be retrieved. | false |
| conversions (opcional) | boolean | If true, conversion data will be retrieved. | false |
| experiments (opcional) | boolean | If true, experiment data will be retrieved. | false |
| kcs (opcional) | boolean | If true, Kameleoon Conversion Score (KCS) will be retrieved. Requires the AI Predictive Targeting add-on | false |
| visitorCode (opcional) | boolean | If true, Kameleoon will retrieve the visitorCode from the most recent visit and use it for the current visit. This is necessary if you want to ensure that the visitor, identified by their visitorCode, always receives the same variation across visits for Cross-device experimentation. | true |
| personalization (opcional) | boolean | If true, personalization data will be retrieved. This is required for the personalization condition. | false |
| cbs (opcional) | boolean | If true, Contextual Bandit score data will be retrieved. | false |
getVisitorWarehouseAudience()
- 🔄 Performs an asynchronous request
Retrieves all audience data associated with the visitor in your data warehouse. The optional warehouseKey parameter is typically your internal user ID. The customDataIndex parameter corresponds to the Kameleoon custom data that Kameleoon uses to target your visitors. You can refer to the warehouse targeting documentation for additional details.
Since a server call is required, this mechanism is asynchronous. Therefore, you should either:
- Provide a
completion callback as an argument to the method to ensure you are notified when the data has been successfully fetched and added to the visitor.
- Use coroutines for asynchronous handling.
Checking only for failed results is recommended. However, if necessary, it can be verified that the data has been added to the visitor and is available for targeting purposes (or for debugging, though using logging is better for debugging).
Java
Kotlin
Kotlin (Coroutines)
// Visitor data will be fetched and automatically added for the visitor
kameleoonClient.getVisitorWarehouseAudience(customDataIndex, result -> {
if (result.isSuccess()) {
// Due to method called before this callback, data was automatically added to the visitor.
} else {
Exception exception = result.failure();
// The request failed due to an exception.
}
});
// If you need to specify warehouse key
kameleoonClient.getVisitorWarehouseAudience("warehouseKey", customDataIndex, result -> {
// Due to method called before this callback, data was automatically added to the visitor,
// but you can evaluate the added data if necessary.
try {
CustomData data = result.getOrThrow();
} catch (Exception exception) {
// The request failed due to an exception.
}
});
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| warehouseKey (opcional) | String | A unique key to identify the warehouse data (usually, your internal user ID). | null |
| customDataIndex (obligatorio) | int | An integer representing the custom data index you want to use to target your BigQuery Audiences. | |
| completion (obligatorio) | ResultCompletion<CustomData, Exception> | The callback that processes the received data. | |
// Visitor data will be fetched and automatically added for the visitor
kameleoonClient.getVisitorWarehouseAudience(customDataIndex) { result ->
if (result.isSuccess) {
// Due to method called before this callback, data was automatically added to the visitor.
} else {
val exception = result.failure()
// The request failed due to an exception.
}
}
// If you need to specify warehouse key
kameleoonClient.getVisitorWarehouseAudience("warehouseKey", customDataIndex) { result ->
// As a result of the method before this callback is called, data was automatically added to the visitor
// but you can evaluate the added data if you need to check it.
try {
val data = result.getOrThrow()
} catch (e: Exception) {
// The request failed due to an exception.
}
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| warehouseKey (opcional) | String | A unique key to identify the warehouse data (usually, your internal user ID). | null |
| customDataIndex (obligatorio) | Int | An integer representing the custom data index you want to use to target your BigQuery Audiences. | |
| completion (obligatorio) | ResultCompletion<CustomData, Exception> | The callback that processes the received data. | |
A common mistake is using suspended functions inside mapCatching, runCatching, or a try-catch block without properly re-throwing CancellationException, which can interfere with coroutine cancellation. To ensure correct behavior, try to avoid calling suspend functions within these blocks.
// Visitor data will be fetched and automatically added for the visitor
viewModelScope.launch {
// As a result of the method call, the data was automatically added to the visitor.
kameleoonClient.getVisitorWarehouseAudience(customDataIndex = customDataIndex) ?: return@launch
}
// If you need to specify warehouse key
viewModelScope.launch {
// As a result of the method call, the data was automatically added to the visitor.
kameleoonClient.getVisitorWarehouseAudience("warehouseKey", customDataIndex)
.onSuccess { customData ->
// But you can evaluate the added data if you need to check it.
}
.onFailure { ex ->
// The request failed due to an exception.
}
}
Argumentos
| Nombre | Tipo | Descripción | Default |
|---|
| warehouseKey (opcional) | String | A unique key indentifying the warehouse data (usually your internal user ID). | null |
| customDataIndex (obligatorio) | Int | An integer representing the custom data index you want to use to target your BigQuery Audiences. | |
Valor de retorno
| Tipo | Descripción |
|---|
Result<CustomData> | A Kotlin Result that contains either the fetched value (CustomData) or the exception that occurred. |
setLegalConsent()
You must use this method to specify whether the visitor has given legal consent to use their personal data. Setting the legalConsent parameter to false limits the types of data that you can include in tracking requests. This method helps you adhere to legal and regulatory requirements while responsibly managing visitor data. You can find more information on personal data in the consent management policy.
kameleoonClient.setLegalConsent(true);
kameleoonClient.setLegalConsent(true)
Argumentos
| Name | Type | Description |
|---|
| legalConsent | boolean | Valor booleano que representa el estado del consentimiento legal. true indicates the visitor has given legal consent, false indicates the visitor has never provided, or has withdrawn, legal consent. Este campo es obligatorio. |
Tipos de datos
This section lists the com.Kameleoon.Data types supported by Kameleoon. Several standard data types are provided, as well as the CustomData type for defining custom data types.
Conversion
El conjunto de datos Conversion almacenado aquí puede utilizarse para filtrar los informes de experimentación y personalización por cualquier objetivo asociado a él.
- Each visitor can have multiple
Conversion objects.
- You can find the
goalId in the Kameleoon app.
| Nombre | Tipo | Descripción | Default |
|---|
| goalId (obligatorio) | int | ID del objetivo. | |
| revenue (opcional) | float | Revenue of the conversion | 0 |
| negative (opcional) | boolean | Define si el ingreso es positivo o negativo. | false |
| metadata (opcional) | CustomData... | Metadatos de la conversión. | new CustomData[0] |
kameleoonClient.addData(new Conversion(32, 10f));
kameleoonClient.addData(new Conversion(33, 0f, true));
kameleoonClient.addData(
new Conversion(34, 5f, new CustomData(3, "metadata1", "md2"), new CustomData(5, "md3"))
);
kameleoonClient.addData(Conversion(32, 10f))
kameleoonClient.addData(Conversion(33, 0f, true))
kameleoonClient.addData(
Conversion(34, 5f, CustomData(3, "metadata1", "md2"), CustomData(5, "md3"))
)
Device
Since Android SDK 4.13.0, the Device is automatically detected based on the android.content.Context. However, you can still manually override it if needed.
Store information about the user’s device.
| Nombre | Tipo | Descripción |
|---|
| device (obligatorio) | Devices | List of devices: phone, tablet, desktop. |
kameleoonClient.addData(Device.tablet());
Geolocation
Geolocation contains the visitor’s geolocation details.
kameleoonClient.addData(new Geolocation("France", "Île-de-France", "Paris"));
kameleoonClient.addData(Geolocation("France", "Île-de-France", "Paris"))
| Nombre | Tipo | Descripción |
|---|
| country (obligatorio) | String | The country of the visitor. |
| region (opcional) | String | The region of the visitor. |
| city (opcional) | String | The city of the visitor. |
| postalCode (opcional) | String | The postal code of the visitor. |
| latitude (opcional) | float | The latitude coordinate representing the location of the visitor. Coordinate number represents decimal degrees. |
| longitude (opcional) | float | The longitude coordinate representing the location of the visitor. Coordinate number represents decimal degrees. |
- Each visitor can have only one
Geolocation. Adding a second Geolocation overwrites the first one.
CustomData
Define your own custom data types in the Kameleoon app or the Data API and use them from the SDK.
| Nombre | Tipo | Descripción | Default |
|---|
| index/name (obligatorio) | int/String | Índice o nombre del dato personalizado. Either index or name must be provided to identify the data. | |
| values (obligatorio) | String.../Collection<String> | Values of the custom data to be stored. | |
| overwrite (opcional) | boolean | Flag para controlar explícitamente cómo se almacenan los valores y cómo aparecen en los informes. See more | true |
- The index of the custom data is available in the Custom data configuration page of the Kameleoon app. Be careful: this index starts at 0, so the first custom data you create for a given site would have the index 0, not 1.
- Adding a
CustomData instance created with a name when the SDK instance configuration is not up to date or the name is not registered, will result in the data being ignored.
kameleoonClient.addData(new CustomData(1, "value"));
// With several values
kameleoonClient.addData(new CustomData(1, "value1", "value2"));
// To set the 'overwrite' flag to false
kameleoonClient.addData(new CustomData(1, false, "value"));
// To use a name instead of the index
kameleoonClient.addData(new CustomData("my-custom-data", "value"));
kameleoonClient.addData(CustomData(1, "value"))
// With several values
kameleoonClient.addData(CustomData(1, "value1", "value2"))
// To set the 'overwrite' flag to false
kameleoonClient.addData(CustomData(1, false, "value"))
// To use a name instead of the index
kameleoonClient.addData(CustomData("my-custom-data", "value"))
Returned Types
DataFile
El DataFile contiene los detalles de configuración del SDK.
It can be extended with additional information if required by clients. If you need more details, please contact your Customer Success Manager.
| Nombre | Tipo | Descripción |
|---|
| featureFlags | Map<String, FeatureFlag> | A map of FeatureFlag objects, keyed by feature flag keys. |
| dateModified | long | The timestamp (in milliseconds) indicating when the DataFile was last modified. |
// Retrieves the map of feature flags from the DataFile.
// The map is keyed by feature flag identifiers, with each value being a FeatureFlag object.
Map<String, FeatureFlag> featureFlags = dataFile.getFeatureFlags();
// Retrieves the last modification timestamp of the DataFile.
// The value is a long representing milliseconds since the Unix epoch.
long dateModified = dataFile.getDateModified();
// Retrieves the map of feature flags from the DataFile.
// The map is keyed by feature flag identifiers, with each value being a FeatureFlag object.
val featureFlags = dataFile.featureFlags
// Retrieves the last modification timestamp of the DataFile.
// The value is a long representing milliseconds since the Unix epoch.
val dateModified = dataFile.dateModified
FeatureFlag
FeatureFlag representa un conjunto de propiedades que definen un feature flag en sí — por ejemplo, sus Variations, Rules, estado del entorno y otros detalles relacionados.
It can be extended with additional information if required by clients. If you need more details, please contact your Customer Success Manager.
| Nombre | Tipo | Descripción |
|---|
| environmentEnabled | boolean | Indica si el feature flag está habilitado en el entorno actual. |
| defaultVariationKey | String | The key of the default variation associated with the feature flag. |
| variations | Map<String, Variation> | A map of Variation objects, keyed by variation keys. |
| rules | List<Rule> | A list of Rule objects |
// Check whether the feature flag is enabled in the current environment
boolean isEnvironmentEnabled = featureFlag.isEnvironmentEnabled();
// Retrieve the key of the default variation
String defaultVariationKey = featureFlag.getDefaultVariationKey();
// Retrieve the default variation object
Variation defaultVariation = featureFlag.getDefaultVariation();
// Retrieve all variations of the feature flag as a map (key = variation key, value = Variation object)
Map<String, Variation> variations = featureFlag.getVariations();
// Retrieve all targeting rules associated with the feature flag
List<Rule> rules = featureFlag.getRules();
// Check whether the feature flag is enabled in the current environment
val isEnvironmentEnabled = featureFlag.isEnvironmentEnabled
// Retrieve the key of the default variation
val defaultVariationKey = featureFlag.defaultVariationKey
// Retrieve the default variation object
val defaultVariation = featureFlag.defaultVariation
// Retrieve all variations of the feature flag as a map (key = variation key, value = Variation object)
val variations = featureFlag.variations
// Retrieve all targeting rules associated with the feature flag
val rules = featureFlag.rules
Rule
Rule representa un conjunto de propiedades que definen una regla en sí — por ejemplo, sus Variations.
It can be extended with additional information if required by clients. If you need more details, please contact your Customer Success Manager.
| Nombre | Tipo | Descripción |
|---|
| variations | Map<String, Variation> | A map of Variation objects, keyed by variation keys. |
// Retrieve all variations of the rule as a map (key = variation key, value = Variation object)
Map<String, Variation> variations = rule.getVariations();
// Retrieve all variations of the rule as a map (key = variation key, value = Variation object)
val variations = rule.variations
Variation
Variation contains information about the assigned variation to the visitor (or the default variation, if no specific assignment exists).
| Nombre | Tipo | Descripción |
|---|
| name | String | The name of the variation. |
| key | String | The unique key identifying the variation. |
| id | Integer | The ID of the assigned variation (or null if it’s the default variation). |
| experimentId | Integer | The ID of the experiment associated with the variation (or null if default). |
| variables | Map<String, Variable> | A map containing the variables of the assigned variation, keyed by variable names. This could be an empty collection if no variables are associated. |
- The
Variation object provides details about the assigned variation and its associated experiment, while the Variable object contains specific details about each variable within a variation.
- Ensure that your code handles the case where
id or experimentId may be null, indicating a default variation.
- The
variables map might be empty if no variables are associated with the variation.
// Retrieving the variation name
String variationName = variation.getName();
// Retrieving the variation key
String variationKey = variation.getKey();
// Retrieving the variation id
Integer variationId = variation.getId();
// Retrieving the experiment id
Integer experimentId = variation.getExperimentId();
// Retrieving the variables map
Map<String, Variable> variables = variation.getVariables();
// Retrieving the variation name
val variationName = variation.name
// Retrieving the variation key
val variationKey = variation.key
// Retrieving the variation id
val variationId = variation.id
// Retrieving the experiment id
val experimentId = variation.experimentId
// Retrieving the variables map
val variables = variation.variables
Variable
Variable contains information about a variable associated with the assigned variation.
| Nombre | Tipo | Descripción |
|---|
| key | String | The unique key identifying the variable. |
| type | String | The type of the variable. Possible values: BOOLEAN, NUMBER, STRING, JSON. |
| value | Object | The value of the variable, which can be of the following types: boolean, int, long, double, String, JSONObject, JSONArray, null. |
// Retrieving the variables map
Map<String, Variable> variables = variation.getVariables();
// Variable type can be retrieved for further processing
String type = variables.get("isDiscount").getType();
// Get the Boolean value of "isDiscount"
Boolean isDiscount = (Boolean) variables.get("isDiscount").getValue();
// Get the numeric value of "number" as an Integer
Integer number = (Integer) variables.get("number").getValue();
// Get the String value of "title"
String title = (String) variables.get("title").getValue();
// Retrieving the variables map
val variables = variation.variables
// Variable type can be retrieved for further processing
val type = variables["isDiscount"]?.type
// Get the Boolean value of "isDiscount"
val isDiscount = variables["isDiscount"]?.value as? Boolean
// Get the numeric value of "number" as an Integer
val number = variables.get("number").value as? Int
// Get the String value of "title"
val title = variables["title"]?.value as? String
Deprecated methods
Estos métodos están obsoletos y se eliminarán en la versión 5.0.0 del SDK.
getFeatureVariationKey()
- 📨 Envía datos de seguimiento a Kameleoon
Use este método para obtener la clave de variación de funcionalidad para un visitante. Este método toma un featureKey como argumento obligatorio para recuperar la clave de variación para el usuario especificado.
If the visitor has never been associated with this feature flag, the SDK returns a randomly assigned variation key (according to the feature flag rules). If the visitor is already registered with this feature flag, this method returns the previous variation key. If the user does not match any of the rules, the default value will be returned, which is defined in your customer’s account.
Ensure you set up proper error handling como se muestra en el ejemplo code to catch potential exceptions.
String featureKey = "new_checkout";
String variationKey = "";
try {
variationKey = kameleoonClient.getFeatureVariationKey(featureKey);
} catch (KameleoonException.SDKNotReady e) {
// Exception indicates that the SDK has not completed its initialization yet.
} catch (KameleoonException.FeatureNotFound e) {
// The error has occurred; feature flag isn't found in current configuration.
} catch (KameleoonException.FeatureEnvironmentDisabled e) {
// The feature flag is disabled for the environment
} catch (Exception e) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
switch (variationKey) {
case "on":
//main variation key is selected for visitorCode
break;
case "alternative_variation":
//alternative variation key
break;
default:
//default variation key
break;
}
val featureKey = "new_checkout"
var variationKey = ""
try {
variationKey = kameleoonClient.getFeatureVariationKey(featureKey)
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicates that the SDK has not completed its initialization yet.
} catch (e: KameleoonException.FeatureNotFound) {
// Exception indicates that the SDK not initialized or the feature toggle is not yet activated on Kameleoon's side. We consider the feature inactive.
} catch (e: KameleoonException.FeatureEnvironmentDisabled) {
// The feature flag is disabled for the environment
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
when (variationKey) {
"on" -> {}
"alternative_variation" -> {}
else -> {}
}
getFeatureVariationKey()
- 📨 Envía datos de seguimiento a Kameleoon
Use este método para obtener la clave de variación de funcionalidad para un visitante. Este método toma un featureKey como argumento obligatorio para recuperar la clave de variación para el usuario especificado.
If the visitor has never been associated with this feature flag, the SDK returns a randomly assigned variation key (according to the feature flag rules). If the visitor is already registered with this feature flag, this method returns the previous variation key. If the user does not match any of the rules, the default value will be returned, which is defined in your customer’s account.
Ensure you set up proper error handling como se muestra en el ejemplo code to catch potential exceptions.
String featureKey = "new_checkout";
String variationKey = "";
try {
variationKey = kameleoonClient.getFeatureVariationKey(featureKey);
} catch (KameleoonException.SDKNotReady e) {
// Exception indicates that the SDK has not completed its initialization yet.
} catch (KameleoonException.FeatureNotFound e) {
// The error has occurred; feature flag isn't found in current configuration.
} catch (KameleoonException.FeatureEnvironmentDisabled e) {
// The feature flag is disabled for the environment
} catch (Exception e) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
switch (variationKey) {
case "on":
//main variation key is selected for visitorCode
break;
case "alternative_variation":
//alternative variation key
break;
default:
//default variation key
break;
}
val featureKey = "new_checkout"
var variationKey = ""
try {
variationKey = kameleoonClient.getFeatureVariationKey(featureKey)
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicates that the SDK has not completed its initialization yet.
} catch (e: KameleoonException.FeatureNotFound) {
// Exception indicates that the SDK not initialized or the feature toggle is not yet activated on Kameleoon's side. We consider the feature inactive.
} catch (e: KameleoonException.FeatureEnvironmentDisabled) {
// The feature flag is disabled for the environment
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
when (variationKey) {
"on" -> {}
"alternative_variation" -> {}
else -> {}
}
getActiveFeatures()
- Use
getVariations() instead.
- Previously called
getFeatureListForVisitorCode, which was removed in SDK version 4.0.0 release.
getActiveFeatures method retrieves information about the active feature flags that are available for the visitor.
Map<String, Variation> listActiveFeatureFlags = kameleoonClient.getActiveFeatures();
val listActiveFeatureFlags = kameleoonClient.getActiveFeatures()
Valor de retorno
| Tipo | Descripción |
|---|
Map<String, Variation> | A dictionary that contains the assigned variations of the active features using the keys of the corresponding active features. |
getFeatureVariable()
- 📨 Envía datos de seguimiento a Kameleoon
Este método obtiene el valor de variable de una clave de variación para un usuario específico. Toma un featureKey y un variableKey como argumentos obligatorios.
If the visitor has never been associated with the featureKey, the SDK returns a randomly assigned variable value for the specified variation key (according to the feature flag rules). If the visitor is already registered with this feature flag, the method returns the variable value for the previously registered variation. If the user does not match any of the rules, the default variable value is returned.
Ensure you set up proper error handling como se muestra en el ejemplo code to catch potential exceptions.
String featureKey = "feature_key";
String variableKey = "variableKey";
try {
Object variableValue = kameleoonClient.getFeatureVariable(featureKey, variableKey);
// your custom code, depending on variableValue
} catch (KameleoonException.SDKNotReady e) {
// Exception indicates that the SDK has not completed its initialization yet.
} catch (KameleoonException.FeatureNotFound e) {
// The error has occurred; feature flag isn't found in current configuration.
} catch (KameleoonException.FeatureVariableNotFound e) {
// Requested variable not defined in Kameleoon
} catch (KameleoonException.FeatureEnvironmentDisabled e) {
// The feature flag is disabled for the environment.
} catch (Exception e) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
val featureKey = "new_checkout"
val variableKey = "var"
try {
val variableValue = kameleoonClient.getFeatureVariable(featureKey, variableKey)
// your custom code depending of variableValue
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicating that the SDK has not completed its initialization yet.
} catch (e: KameleoonException.FeatureNotFound) {
// The error is happened, feature flag isn't found in current configuraiton
} catch (e: KameleoonException.FeatureVariableNotFound) {
// Requested variable not defined on Kameleoon's side
} catch (e: KameleoonException.FeatureEnvironmentDisabled) {
// The feature flag is disabled for the environment
} catch (e: Exception) {
// Recommended (but optional) safeguard for unexpected exceptions from third-party libraries
}
Argumentos
| Nombre | Tipo | Descripción |
|---|
| featureKey | String | Key of the feature you want to display to a user. Este campo es obligatorio. |
| variableName | String | Nombre de la variable cuyo valor desea obtener. Este campo es obligatorio. |
Valor de retorno
| Tipo | Descripción |
|---|
| object | Value of variation variable that is registered for the specified visitorCode for this feature flag. Valid types: boolean, int, double, String, JSONObject, JSONArray |
Excepciones lanzadas
| Tipo | Descripción |
|---|
| SDKNotReady | Exception indicating that the SDK has not completed its initialization. |
| FeatureNotFound | Exception indicating that the requested feature key was found in the SDK’s internal configuration. This exception usually means that the feature flag has not been activated on Kameleoon’s side (but code implementing the feature is already deployed in the application). |
| FeatureVariableNotFound | Exception indicating that the specified variable was not found. Check that the variable key in the Kameleon app matches the one in your code. |
| FeatureEnvironmentDisabled | Excepción que indica que el feature flag está deshabilitado para el entorno actual del visitante (por ejemplo, production, staging o development). |
getFeatureVariationVariables()
- Use
getVariation() instead.
- This method was previously called
getFeatureAllVariables, which was removed in SDK version 4.0.0 release.
To retrieve all of a feature’s variables, call this method. You can modify your feature variables in the Kameleoon app.
Este método toma one input parameter: featureKey. It returns the data as a Map<String, Object> type, as defined in the Kameleoon app. It throws an exception (FeatureNotFound) if the requested feature was not found in the SDK’s internal configuration.
String featureKey = "myFeature";
String variationKey = "variation1";
try {
Map<String, Object> variables = kameleoonClient.getFeatureVariationVariables(featureKey, variationKey);
} catch (KameleoonException.SDKNotReady e) {
// Exception indicating that the SDK has not completed its initialization yet.
} catch (KameleoonException.FeatureNotFound e) {
// The feature is not yet activated on Kameleoon's side
} catch (KameleoonException.FeatureEnvironmentDisabled e) {
// The feature flag is disabled for the environment
} catch (Exception e) {
// This is a generic Exception handler which will handle all exceptions.
System.out.println("Exception occurred");
}
val featureKey = "myFeature"
val variationKey = "variation1"
try {
val variables = kameleoonClient.getFeatureVariationVariables(featureKey, variationKey)
} catch (e: KameleoonException.SDKNotReady) {
// Exception indicating that the SDK has not completed its initialization yet.
} catch (e: KameleoonException.FeatureNotFound) {
// The feature is not yet activated on Kameleoon's side
} catch (e: KameleoonException.FeatureEnvironmentDisabled) {
// The feature flag is disabled for the environment
} catch (e: Exception) {
// This is a generic Exception handler which will handle all exceptions.
println("Exception occurred")
}
Argumentos
| Nombre | Tipo | Descripción |
|---|
| featureKey | String | Unique identifier of the feature you need to obtain. Este campo es obligatorio. |
Valor de retorno
| Tipo | Descripción |
|---|
Map<String,Object> | Data representing the variables associated with this feature flag. Values can be int, String, boolean, JSONObject or JSONArray (depending on the types defined in the web interface). |
Excepciones lanzadas
| Type | Description |
|---|
| SDKNotReady | Exception indicating that the SDK has not completed its initialization. |
| FeatureNotFound | Exception indicating that the requested feature was not found in the SDK’s internal configuration. This exception usually means that the feature flag has not yet been activated on Kameleoon’s side. |
| FeatureVariableNotFound | Exception indicating that the specified variable was not found. Check that the variable key in the Kameleoon app matches the key in your code. |
| FeatureEnvironmentDisabled | Excepción que indica que el feature flag está deshabilitado para el entorno actual del visitante (por ejemplo, production, staging o development). |
getFeatureList()
Devuelve una lista de claves de feature flags disponibles actualmente en el SDK.
List<String> allFeatureFlagListId = kameleoonClient.getFeatureList();
val allFeatureFlagListId = kameleoonClient.getFeatureList()
Valor de retorno
| Tipo | Descripción |
|---|
List<String> | List of feature flag keys |