Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Advanced Audience Targeting segment qualification methods

Use the Optimizely Data Platform (ODP) 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.

📘

Note

Optimizely Data Platform (ODP) Advanced Audience Targeting segment methods are in beta. Contact your Customer Success Manager for more information or register now on Optimizely.com.

fetchQualifiedSegments (asynchronous)

Minimum SDK Version

4.0.0-beta

Description

Fetches all qualified segments for the user context.

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)[OptimizelySegmentOption]A set of options for fetching qualified segments from ODP.
completion(OptimizelyError?) -> VoidA completion handler to be called with the fetch result.

Returns

The completion handler to be called on completion that updates the qualified segments array --comment can you just say "The completion handler that updates the qualified segments array"? does "completion handler" imply completion?

  • On success, it passes a nil error.
  • On failure, it passes one of these errors:
    • OptimizelvError.invalidSegmentidentifier
    • OptimizelyError.fetchSegmentsFailed.

📘

Qualified segments array

You can read and write directly to the qualified segments array to bypass the remote fetching process from ODP or utilize your own fetching service. This can be helpful when testing or debugging.

Example fetchQualifiedSegments (asynchronous)

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

let attributes: [String: Any] = ["app_version": "1.3.2"]
let user = optimizelyClient.createUserContext(userId: "user123", attributes: attributes)
    
// Without segment option
user.fetchQualifiedSegments { err in
    print(err == nil)
            
    let decision = user.decide(key: "flag1")
    try? user.trackEvent(eventKey: "purchase_event")
}
    
// With segment options
var odpSegmentOptions: [OptimizelySegmentOption] = [.ignoreCache, .resetCache]
user.fetchQualifiedSegments(options: odpSegmentOptions) { err in
    print(err == nil)
            
    let decision = user.decide(key: "flag1")
    try? user.trackEvent(eventKey: "purchase_event")
}

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.

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

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

fetchQualifiedSegments (synchronous)

Minimum SDK Version

4.0.0-beta

Description

This is a synchronous version of the fetchQualifiedSegments asynchronous version above.

📘

Note

This call blocks the calling thread until fetching is completed.

Parameters

The following table describes the parameters for the fetchQualifiedSegments method:

ParameterTypeDescription
options (optional)[OptimizelySegmentOption]A set of options for fetching qualified segments from ODP.

Returns

This method does not provide a return value. If it fails, it throws one of these errors:

  • OptimizelyError.invalidSegmentIdentifier
  • OptimizelyError-fetchSegmentsFailed(String)

Example fetchQualifiedSegments (synchronous)

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

let optimizely = OptimizelyClient(sdkKey: <Your_SDK_Key>)

optimizely.start { result in
        let attributes: [String: Any] = ("app version": "1.3.2")
        let user = optimizelyClient.createUserContext(userld: "user123", attributes: attributes)
                  
        do {
        // this will block the calling thread until fetch is completed. 
        try user.fetchQualifiedSegments()

        let decision = user. decide(key: "flag1")
    } catch OptimizelyError.invalidSeqmentIdentifier {
                print("[AudienceSegments] audience segments fetch failed (user id is not reoistered yet or invalid)")
    } catch {
                print("[AudienceSegments] \(error)")
    }
}

isQualifiedFor

Minimum SDK Version

4.0.0-beta

Description

Check if the user qualifies for the given audience segment.

Parameters

The following table describes the parameters for the isQualifiedFor method:

ParameterTypeDescription
segmentStringThe ODP audience segment name to check if the user qualifies

Returns

true if the user is qualified.

Examples

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

  let attributes: [String: Any] = ["laptop_os": "mac"]
  let user = optimizelyClient.createUserContext(userId: "user123", attributes: attributes)

  user.fetchQualifiedSegments { segments, err in
    let isQualified = user.isQualifiedFor(segment: "segment1")
  }

Source files

The language/platform source files containing the implementation for Swift are available on Github.