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 is_qualified_for
method to check if the user qualifies for the specified segment.
Note
Optimizely Data Platform (ODP) Advanced Audience Targetingaudience segment methods are in beta. Contact your Customer Success Manager for more information or register now on Optimizely.com.
fetch_qualified_segments
Minimum SDK Version
5.0.0-beta
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:
Parameter | Type | Description |
---|---|---|
options (optional) | list[str] | A list of options for fetching qualified segments from ODP. |
callback (optional) | Callback function | A 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
usingget_qualified_segments
andset_qualified_segments
.This allows for bypassing 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
fetch_qualified_segments
callattributes = { "app_version": "1.3.2" }
user = optimizely.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")
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-beta
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:
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:
attributes = {"laptop_os": "mac"}
user = optimizely.create_user_context("fs_id_12", attributes)
success = user.fetch_qualified_segments()
qualified = user.is_qualified_for("segment1")
Source files
The language/platform source files containing the implementation for Python are available on GitHub.
Updated 16 days ago