Real-Time Audiences for Feature Experimentation segment qualification methods for the React Native 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 and configure Real-Time Audiences for Feature Experimentation before fetching qualified segments and checking if the user is qualified for the given audience segment.
fetchQualifiedSegments
Minimum SDK Version
v3.0.0 or higher
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 React Native SDK provides an asynchronous version of the ODP fetchQualifiedSegments
method.
fetchQualfiedSegments
is a method of the ReactSDKClient
object.
Parameters
The following table describes the parameters for the fetchQualifiedSegments
method:
Parameter | Type | Description |
---|---|---|
| String | A set of options for fetching qualified segments from ODP. |
Returns
This method returns a Promise<bool>
, which resolves with true
if segments are fetched successfully, and false
otherwise.
If the React Native 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.
NoteYou can read and write directly to the qualified segments array. 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 following is an example of calling the fetchQualifiedSegments
method and accessing the returned completion object:
import { createInstance } from "@optimizely/react-sdk";
const optimizelyClient = createInstance({
sdkKey: 'YOUR_SDK_KEY'
});
export default function App() {
optimizelyClient.onReady().then(() => {
optimizelyClient.fetchQualifiedSegments();
});
return (
<YourComponent />
);
}
The following diagram shows the network calls between your application, the React Native SDK, and the ODP server when calling fetchQualifiedSegments
:

- Your application calls the
FetchQualifiedSegments
method. - The Feature Experimentation React Native SDK makes a GraphQL call to ODP to fetch segments the user ID qualifes for.
- ODP responds with the qualified audience segments.
- The React Native SDK automatically caches the fetched segments mapping the user ID to the qualified segments.
- The
FetchQualifiedSegments
method returns a boolean if the qualified segments array was updated. - Call the useDecision hook to get the flag decision result for a flag key.
After the audience segments are fetched, they are cached. This means that if you request the segments again (when new user contexts are created), the audience segment information can be retrieved from the cache instead of being fetched again 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 audiences 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 least recently used (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.
If you would like to bypass caching, you can add the following options to your options
array:
OptimizelySegmentOption.IGNORE_CACHE
– Bypass segments cache for lookup and save.OptimizelySegmentOption.RESET_CACHE
– Reset all segments cache.
isQualifiedFor
Minimum SDK version
3.0.0 or higher
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 otherwise, false
Examples
The following is an example of whether or not the user is qualified for an ODP segment:
import { createInstance } from "@optimizely/react-sdk";
const optimizelyClient = createInstance({
sdkKey: 'YOUR_SDK_KEY'
});
export default function App() {
optimizelyClient.onReady().then(() => {
optimizelyClient.fetchQualifiedSegments();
const isQualifiedForSegment1 = optimizelyClient.getUserContext()?.isQualifiedFor("segment1");
console.log(isQualifiedForSegment1); // true
});
return (
<YourComponent />
);
}
Source files
The language and platform source files containing the implementation for React Native are available on Github.
Updated about 19 hours ago