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

Advanced Audience Targeting segment qualification methods for the Android SDK

Use the Advanced Audience Targeting segment methods to fetch external audience mapping for the user. You can fetch user segments with the user identifier of the user context.

Prerequisites

You must enable the Advanced Audience Targeting integration before fetching qualified segments and checking if the user is qualified for the given audience segment.

👍

Beta

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

fetchQualifiedSegments

Minimum SDK version

4.0.0

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 Android SDK provides a synchronous and asynchronous version of the fetchQualifiedSegments method. The qualified segments are cached.

  • The caller will be blocked until the synchronous fetch has been completed.
  • The caller of asynchronous API will not be blocked.

fetchQualfiedSegments is a method of the UserContext object. See OptimizelyUserContext for details.

Parameters

The following table describes the parameters for the fetchQualifiedSegments method:

ParameterTypeDescription
options (optional)StringA set of options for fetching qualified segments.
completion (for asynchronous calls)Callback functionA completion handler to be called with the fetch result.

Returns - Synchronous call

The fetchQualifiedSegments synchronous method returns true if the qualified segments array in the user context was updated.

Returns - Asynchronous call

  • If the fetch completes successfully, the Android SDK will update the qualified segments array in the OptimizelyUserContextand then call the completion handler with a success status.
  • If the fetch fails, the SDK will call the handler with a failure status.

📘

Note

You can read and write directly to the qualified segments array. This allows for bypassing the remote fetching process from Optimizely Data Platform (ODP) or for utilizing your own fetching service. This can be helpful when testing or debugging.

Example fetchQualifiedSegments

The following is an example of calling the fetchQualifiedSegments method and accessing the returned completion object.

Kotlin example

val attributes: MutableMap<String, Any> = HashMap() 
       attributes.put("app_version", "1.3.2") 
       val user = optimizelyClient.createUserContext("user123", attributes) 
 
// Without segment option 
       user!!.fetchQualifiedSegments { isFetchSuccessful: Boolean? -> 
           println(isFetchSuccessful) 
           val decision = user.decide("flag1") 
           user.trackEvent("purchase_event") 
       } 
 
// With segment options 
       val odpSegmentOptions: MutableList<ODPSegmentOption> = ArrayList() 
       odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE) 
       odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE) 
       user.fetchQualifiedSegments( 
           { isFetchSuccessful: Boolean? -> 
               println(isFetchSuccessful) 
               val decision = user.decide("flag1") 
               user.trackEvent("purchase_event") 
           }, 
           odpSegmentOptions 
       ) 
val attributes: MutableMap<String, Any> = HashMap() 
       attributes.put("app_version", "1.3.2") 
 
// Method 1 create with user id 
       val userWithUserId = optimizelyClient.createUserContext("user123", attributes) 
 
// Without segment option 
       val response = userWithUserId!!.fetchQualifiedSegments() 

// With segment options 
       val odpSegmentOptions: MutableList<ODPSegmentOption> = ArrayList() 
       odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE) 
       odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE) 

       val response = userWithUserId!!.fetchQualifiedSegments(odpSegmentOptions) 
 
       val decision = userWithUserId!!.decide("flag1") 
       userWithUserId!!.trackEvent("myevent") 
 
// Method 2 create OptimizelyUserContext with auto-generated VUID instead of user id 
       val userWithVuid = optimizelyClient.createUserContext(attributes) 
 
// Without segment option 
       val response = userWithVuid!!.fetchQualifiedSegments() 
 
// With segment option 
val response = userWithVuid!!.fetchQualifiedSegments() 
val odpSegmentOptions: MutableList<ODPSegmentOption> = ArrayList() 
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE) 
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE) 
 
val response = userWithVuid.fetchQualifiedSegments(odpSegmentOptions) 
 
val decision = userWithVuid.decide("flag1") 
userWithVuid.trackEvent("myevent") 

Android example

Map<String, Object> attributes = new HashMap<>();
attributes.put("app_version", 1.3.2");
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);

// Without segment option 
Boolean response = user.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
            System.out.println(isFetchSuccessful);
            
            OptimizelyDecision decision = user.decide("flag1");
            user.trackEvent("purchase_event");
        });

// With segment options
List<ODPSegmentOption> odpSegmentOptions = new ArrayList<>();
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE);
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE);

user.fetchQualifiedSegments((Boolean isFetchSuccessful) -> {
            System.out.println(isFetchSuccessful);
            
            OptimizelyDecision decision = user.decide("flag1");
            user.trackEvent("purchase_event");
        },
        odpSegmentOptions);
Map<String, Object> attributes = new HashMap<>();
attributes.put("app_version", "1.3.2");

// Method 1 create with user id
OptimizelyUserContext userWithUserId = optimizelyClient.createUserContext("user123", attributes);

// Without segment option 
Boolean response = userWithUserId.fetchQualifiedSegments();

// With segment options
List<ODPSegmentOption> odpSegmentOptions = new ArrayList<>();
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE);
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE);

Boolean response = userWithUserId.fetchQualifiedSegments(odpSegmentOptions);


OptimizelyDecision decision = userWithUserId.decide("flag1");
userWithUserId.trackEvent("myevent");


// Method 2 create OptimizelyUserContext with auto-generated VUID instead of user id
OptimizelyUserContext userWithVuid = optimizelyClient.createUserContext(attributes);

// Without segment option 
Boolean response = userWithVuid.fetchQualifiedSegments();


// With segment options
List<ODPSegmentOption> odpSegmentOptions = new ArrayList<>();
odpSegmentOptions.add(ODPSegmentOption.IGNORE_CACHE);
odpSegmentOptions.add(ODPSegmentOption.RESET_CACHE);

Boolean response = userWithVuid.fetchQualifiedSegments(odpSegmentOptions);


OptimizelyDecision decision = userWithVuid.decide("flag1");
userWithVuid.trackEvent("myevent");

The following diagram shows the network calls between your application, the Android SDK, and the Optimizely Data Platform (ODP) server when calling fetchQualifiedSegments:

  1. Call fetchQualifiedSegments method.
  2. Android SDK makes GraphQL call to ODP to fetch segments.
  3. ODP responds with segments.
  4. Fetched segments mapping user IDs to segments are cached, see OdpSegmentManager.
  5. Appropriate variations are returned for user.

OdpSegmentManager

Once the segments have been fetched, they are cached. This means that if the same user is requesting 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 Android SDK has an OdpSegmentManager module which manages a segments cache that is shared for all user contexts. The cache is in memory (not persistent), so it will be reset when the application is terminated/device is rebooted.

Specifics about the cache:

  • Cache stores mapping of user IDs to segments.
  • The Android SDK calls the ODP server on cache miss/expiration.
  • Cache is least recently used (LRU).
  • Caches is expired on timeout.
  • Both cache size and timeout are configurable (default values provided).
  • Caching is enabled by default and can be disabled by setting ignoreCache to true.

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

  • ignoreCache – Bypass segments cache for lookup and save.
  • resetCache – Reset all segments cache.

See the Kotlin and Android code samples under the "With segment options" comment to see how call fetchQualifiedSegments with caching options.

isQualifiedFor

Minimum SDK Version

4.0.0

Description

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

Parameters

The following table describes the parameters for the isQualifiedFor method:

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

val attributes: MutableMap<String, Any> = HashMap() 
attributes.put("mobile_os", "ios") 
val user: OptimizelyUserContext = optimizely.createUserContext("user123", attributes) 
 
val response = user.fetchQualifiedSegments() 
val isQualified = user.isQualifiedFor("segment1") 
Map<String, Object> attributes = new HashMap<>();
attributes.put("mobile_os", "ios");
OptimizelyUserContext user = optimizely.createUserContext("user123", attributes);

Boolean response = user.fetchQualifiedSegments();
Boolean isQualified = user.isQualifiedFor("segment1");

Source files

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