Optimizely Data Platform audience segment qualification methods
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.
Note
Advanced Audience Targeting and audience segment qualification methods are currently in beta. Contact your Customer Success Manager for more information or register now on Optimizely.com.
FetchQualifiedSegments
Minimum SDK Version
4.0.0-beta
Description
You can use the FetchQualifiedSegments
method to retrieve the external audience mapping for a specific user from the ODP server. The Optimizely Feature Experimentation C# SDK provides a synchronous and asynchronous version of the Optimizely Data Platform (ODP) 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 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 needed.
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 callingFetchQualifiedSegments()
using theGetQualifiedSegments()
andSetQualifiedSegments()
.This allows you to 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 "user123" and a set of attributes which 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 userContext = optimizely.CreateUserContext("user123", new UserAttributes
{
{ "app_version", "1.3.2" },
});
var success = false;
var task = userContext.FetchQualifiedSegments(result => success = result);
// do other work
task.Wait();
// other 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 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.
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-beta
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 userContext = optimizely.CreateUserContext("user123", new UserAttributes
{
{ "laptop_os", "mac" },
});
userContext.FetchQualifiedSegments();
userContext.IsQualifiedFor("segment1");
Source files
The language/platform source files containing the implementation for C# are available on GitHub
Updated 3 days ago