Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Advanced Audience Targeting segment qualification methods for the Python SDK

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

👍

Beta

Advanced Audience Targeting is currently in beta. Apply on the Optimizely beta signup or contact your Customer Success Manager.

fetch_qualified_segments

Minimum SDK version

5.0.0

Description

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

  • If you do not provide a callback, the caller is blocked until the synchronous fetch is complete.
  • Providing a callback does not block the caller.

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

Parameters

The following table describes the parameters for the fetch_qualified_segments method:

ParameterTypeDescription
options (optional)list[str]A list of options for fetching qualified segments from ODP.
callback (optional)Callback functionA callback function to be called with the fetch result.

Returns - synchronous call

If you do not provide a callback, the fetch_qualified_segments method returns True if the qualified segments array in the user context was updated.

Returns - asynchronous call

If you provide a callback, the fetch_qualified_segments method fetches the segments on a new thread and returns the thread handle.

  • If the fetch completes successfully, the Python SDK updates the qualified segments list in the user context and then calls the callback function with a success status.
  • If the fetch fails, the SDK calls the callback function with a failure status.

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

📘

Note

You can read and write to the qualified segments array instead of calling fetch_qualified_segments using get_qualified_segments and set_qualified_segments.

This lets you bypass the remote fetching process from ODP or for utilizing your own fetching service. This can be helpful when testing or debugging.

Example fetch_qualified_segments call

attributes = { "app_version": "1.3.2" }
user = optimizely_client.create_user_context("user123", attributes)

def fetch_callback(fetch_successful):
    print(fetch_successful)

    if fetch_successful:
        decision = user.decide("flag1")
        user.track_event("purchase_event")

# spawned thread is returned
fetch_thread: threading.Thread = user.fetch_qualified_segments(fetch_callback)

# thread must eventually be joined to ensure callback is run to completion
fetch_thread.join()
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 = [OptimizelyOdpOption.IGNORE_CACHE, OptimizelyOdpOption.RESET_CACHE]

success = user.fetch_qualified_segments(options=odp_segment_options)

if success:
    decision = user.decide("flag1")
    user.track_event("myevent")

The following diagram shows the network calls between your application, the Python SDK, and the ODP server when calling fetch_qualified-segments:

Python SDK fetch_qualifed_segments ODP network diagram
  1. Call fetch_qualified-segments method.
  2. The Python SDK makes GraphQL call to ODP to fetch segments.
  3. ODP responds with segments.
  4. Fetched segments mapping user IDs to segments are cached.
  5. Appropriate variations are returned for user.

After you fetch the segments, they are cached. This means that if the same user requests the segments again (when new user contexts are created), the Python SDK can retrieve the audience segment information from the cache rather than from the remote ODP server.

If you would like to bypass caching, you can add the following options to your OptimizelyOdpOption array:

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

is_qualified_for

Minimum SDK version

5.0.0

Description

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

Parameters

The following table describes the parameters for the is_qualified_for method:

ParameterTypeDescription
segmentStringThe 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:

attributes = {"laptop_os": "mac"}
user = optimizely_client.create_user_context("fs_id_12", attributes)

success = user.fetch_qualified_segments()
qualified = user.is_qualified_for("segment1")

Source files

The language and platform source files containing the implementation for Python are available on GitHub.