Real-Time Segments for Feature Experimentation segment qualification methods for the C# SDK
Use the FetchQualifiedSegments
method to retrieve the external audience mapping for the user from the Optimizely Data Platform (ODP) server. Use the IsQualifiedFor
method to check if the user qualifies for the specified segment.
Prerequisites
You must enable the Real-Time Segments for Feature Experimentation integration before fetching qualified segments and checking if the user is qualified for the given audience segment.
FetchQualifiedSegments
Minimum SDK Version
4.0.0
Description
You can use the FetchQualifiedSegments
method to retrieve the external audience mapping for a specific user from the Optimizely Data Platform (ODP) server. The Optimizely Feature Experimentation C# SDK provides a synchronous and asynchronous version of the FetchQualifiedSegments
method.
- The SDK blocks the caller until after completing the synchronous fetch.
- The SDK does not block the caller of asynchronous API.
FetchQualfiedSegments
is a method of the UserContext
object. See OptimizelyUserContext for details.
Parameters
The following table describes the parameters for the fetchQualifiedSegments
method:
Parameter | Type | Description |
---|---|---|
callback (for asynchronous calls) | Callback function | A completion handler to be called with the fetch result. |
segmentOptions (optional) | List | A set of options for fetching qualified segments from ODP. |
Returns – Synchronous call
The FetchQualifiedSegments
synchronous method returns true
if the qualified segments array is in the OptimizelyUserContext.
Returns – Asynchronous call
- If the fetch completes successfully, the C# SDK updates the qualified segments array in the OptimizelyUserContext and then invokes the callback handler with a success status.
- If the fetch fails, the SDK calls the handler with a failure status.
The asynchronous FetchQualifiedSegments
returns a Task
object, which can be used to .Wait()
for the task to complete.
If the C# SDK does not find an ODP audience in the datafile, it returns an empty qualified segments array without sending a request to the ODP server.
Note
You can read and write directly to the qualified segments list instead of calling
FetchQualifiedSegments()
using theGetQualifiedSegments()
andSetQualifiedSegments()
.This lets you bypass the remote fetching process from ODP or utilize your own fetching service. This can be helpful when testing or debugging.
Example FetchQualifiedSegments
call
FetchQualifiedSegments
callThe code creates a user context by instantiating an OptimizelyUserContext
object with a user ID of "user123" and a set of attributes that includes an "app_version" attribute with a value of "1.3.2".
The next section of the code shows the use of theFetchQualifiedSegments
method to fetch all qualified segments with and without segment options.
After that, the code uses the Decide
method to make a decision on whether to show the feature flag with the key "flag1" to the user. Finally, the code calls the trackEvent
method to track a custom event called "myevent".
var optimizely = OptimizelyFactory.NewDefaultInstance();
var attributes = new UserAttributes
{
{ "app_version", "1.3.2" },
};
var userContext = optimizely.CreateUserContext("user123", attributes);
var success = false;
var task = userContext.FetchQualifiedSegments(result => success = result);
// Do other work here
task.Wait();
// Do more work that depends on `success`
if (success)
{
userContext.Decide("flag1");
userContext.TrackEvent("myevent");
}
var optimizely = OptimizelyFactory.NewDefaultInstance();
var userContext = optimizely.CreateUserContext("user123", new UserAttributes
{
{ "app_version", "1.3.2" },
});
var success = userContext.FetchQualifiedSegments();
if (success)
{
userContext.Decide("flag1");
userContext.TrackEvent("myevent");
}
The following diagram shows the network calls between your application, the C# SDK, and the ODP server when calling FetchQualifiedSegments
:
- Call
FetchQualifiedSegments
method. - The C# SDK makes GraphQL call to ODP to fetch segments.
- ODP responds with the qualified segments.
- Fetched segments mapping user IDs to segments are cached, see more below.
- Appropriate variations are returned for user.
The SDK then caches the fetched segments. This means that if the same user requests the segments again (when new user contexts are created), you can retrieve the audience segment information from the cache rather than fetch it from the remote ODP server.
The cache is used for the FetchQualifiedSegments
call. This method is called on the user context (the user context is fixed, including the real-time segments the user qualifies for).
The cache only applies when calling the FetchQualifiedSegments
call. If you set the cache timeout to 0, the cache is disabled. Optimizely uses the LRU algorithm, so the oldest record is bumped out when the maximum size is reached. If there is a cache miss upon the method call, Optimizely makes a network request.
You can bypass caching by adding the following options to your OdpSegmentOptions
array:
- IGNORE_CACHE – Bypass segments cache for lookup and save.
- RESET_CACHE – Reset all segments cache.
IsQualifiedFor
Minimum SDK version
v4.0.0
Description
Check if the user is qualified for the given audience segment.
Parameters
The following table describes the parameters for the IsQualifiedFor
method:
Parameter | Type | Description |
---|---|---|
segment | string | The ODP audience segment name to check if the user is qualified for. |
Returns
true
if the user is qualified.
Examples
The following is an example of whether or not the user is qualified for an ODP segment:
var projectConfigurationManager = new HttpProjectConfigManager.Builder()
.WithSdkKey("YOUR_SDK_KEY")
.WithPollingInterval(TimeSpan.FromSeconds(5))
.WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(500))
.Build(defer: true);
var optimizely = new Optimizely(projectConfigurationManager);
var attributes = new UserAttributes
{
{ "laptop_os", "mac" },
};
var userContext = optimizely.CreateUserContext("user123", attributes);
userContext.FetchQualifiedSegments();
userContext.IsQualifiedFor("segment1");
Source files
The language and platform source files containing the implementation for C# are available on GitHub
Updated 5 months ago