Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Advanced Audience Targeting segment qualification methods

Use the fetch_qualified_segments method to retrieve the external audience mapping for the user from the Optimizely Data Platform (ODP) server. Use the qualified_for? method to check if the user qualifies for the specified segment.



Advanced Audience Targeting and the segment qualification methods are in beta. Contact your Customer Success Manager for more information or register now on


Minimum SDK Version



You can use the fetch_qualified_segments method to retrieve the external audience mapping for a specific user from the ODP server. The Optimizely Feature Experimentation Ruby SDK lets you make the call to Optimizely Data Platform (ODP) in a synchronous or asynchronous fashion, depending on the block parameter.

  • If no block is provided, the caller will be blocked until the synchronous fetch has been completed. --comment I want to say "Not providing a block blocks the caller...but then we have block blocks lol
  • If a block is provided, the caller will not be blocked.

fetch_qualified_segments is a method of the OptimizelyUserContext object. See OptimizelyUserContext for details.


The following table describes the parameters for the fetch_qualified_segments method:

options (optional)StringA set of options for fetching qualified segments from ODP.
block (optional)Callback functionA completion handler to be called with the fetch result.

Returns - Synchronous call

If the SDK does not have a block, thefetch_qualified_segments method returns true if the qualified segments array in the user context was updated.

Returns - Asynchronous call

If the SDK has a block, the fetch_qualified_segments method fetches the segments on a new thread and returns the thread handle.

  • If the fetch completes successfully, the Ruby SDK updates the qualified segments array in the user context and then calls the block with a success status.
  • If the fetch fails, the SDK calls the block with a failure status.

If the Ruby SDK does not find an ODP audience in the datafile, it will return an empty qualified segments array without sending a request to the ODP server.



You can read and write directly to the qualified segments array instead of calling fetch_qualified_segments.

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 fetch_qualified_segments call

The 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 thefetch_qualified_segments method to fetch all qualified segments with and without segment options.

After that, the code uses the decide method to decide whether to show the feature flag with the key "flag1" to the user. Finally, the SDK calls the track_event method to track a custom event called "myevent".

If a callback is provided, the fetch runs on a spawned thread and runs the callback upon completion. If no callback is provided, the fetch will be synchronous. --comment that whole passive voice thing

attributes = { 'app_version' => '1.3.2' }
user = optimizely.create_user_context('user123', attributes)

# spawned thread is returned
fetch_thread = user.fetch_qualified_segments do |success|

    return unless success

    decision = user.decide('flag1')

# thread must eventually be joined to ensure callback is run to completion
attributes = { "app_version"=> "1.3.2" }
user = optimizely.create_user_context("user123", attributes)

# Without segment option
success = user.fetch_qualified_segments

# With segment options
odp_segment_options = [Optimizely::OptimizelyOdpOption.IGNORE_CACHE, Optimizely::OptimizelyOdpOption.RESET_CACHE]

success = user.fetch_qualified_segments(options: odp_segment_options)

if success
    decision = user.decide("flag1")

The SDK fetches the segments then caches them. This means that if the same user is requesting the segments again (when new user contexts are created), you can retrieve the audience segment information from the cache rather than from the remote ODP server. --comment check my rewording

You can add the following options to your OptimizelyOdpOption array to bypass caching:

  • IGNORE_CACHE – Bypass segments cache for lookup and save
  • RESET_CACHE – Reset all segments cache


Minimum SDK Version



Check if the user is qualified for the given audience segment.


The following table describes the parameters for the qualified_for? method:

segmentStringThe ODP audience segment name to check if the user is qualified for.


true if the user is qualified.


The following is an example of whether or not the user is qualified for an ODP segment:

attributes = {'laptop_os' => 'mac'}
user = optimizely.create_user_context('fs-id-12', attributes)

success = user.fetch_qualified_segments
qualified = user.qualified_for?('segment1');

Source files

The language/platform source files containing the implementation for Ruby is is available on GitHub.