Create a user context using the Flutter SDK
Describes the create user context method, which creates a user context for flag decisions and events in Optimizely Feature Experimentation.
The purpose of this method is to create a user and set user context once, so you do not have to specify the user each time you make a flag decision or track an event. You can define multiple user contexts. The system returns user context as a runtime object that is not otherwise persisted.
Version
1.0.1 or higher
Description
This call creates a user context for flag decisions and events. You can call this method successfully on the Optimizely client instance, even before you have fully configured the instance.
Parameters
The following table lists the required and optional parameters:
Parameter | Type | Description |
---|---|---|
userId required | String | The ID of the user. |
attributes optional | Map | A map of custom key-value string pairs specifying attributes for the user that the system users for audience targeting. See the following section for more details. |
Audience attributes
Set custom audience attributes for the user, which you can use to target audiences. You can pass strings, numbers, Booleans and nil as custom user attribute values. If you want to target audiences based on the version of your application they are using, you can also pass in a string formatted as a semantic version, then define a version
audience condition in the Optimizely app.
Important
During audience evaluation, if you do not pass a valid attribute value for a given audience condition—for example, if you pass a string when the audience condition requires a Boolean or if you forget to pass a value—then the system skips that condition. The SDK logs include warnings when this occurs.
Returns
Returns an OptimizelyUserContext object. For details, see OptimizelyUserContext.
Example
// option 1: create a user, then set attributes
var user = await flutterSDK.createUserContext("user123");
var attributes = <String, dynamic>{};
attributes["is_logged_in"] = false;
attributes["app_version"] = "1.3.2";
user!.setAttributes(attributes);
// option 2: pass attributes when creating the user
var attributes = <String, dynamic>{};
attributes["is_logged_in"] = false;
attributes["app_version"] = "1.3.2";
var optimizelyUserContext = await optimizelyClient.createUserContext(userId: "user123", attributes: attributes);
See Also
Source files
The language/platform source files containing the implementation for the Flutter SDK for Android Optimizely.java and for Swift is OptimizelyClient.swift.
Updated 10 months ago