Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

Advanced Audience Targeting segment qualification methods

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

📘

Note

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

FetchQualifiedSegments

Minimum SDK Version

2.0.0-beta

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 Go SDK provides a synchronous and asynchronous version of the Optimizely Data Platform (ODP) FetchQualifiedSegments method.

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

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

Parameters

The following table describes the parameters for the FetchQualifiedSegments and FetchQualifiedSegmentsAsync methods:

ParameterTypeDescription
options (optional)[]segment.OptimizelySegmentOptionA set of options for fetching qualified segments from ODP.
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 Go SDK updates the qualified segments array in the user context and then calls the completion handler with a success status.
  • If the fetch fails, the SDK calls the handler with a failure status.

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

📘

Note

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

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

Example FetchQualifiedSegments 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.8.4".

The next section of the code shows the use of theFetchQualifiedSegments method to fetch all qualified segments with and without segment options.

After that, the code uses the decide method to make a decision on whether to show the feature flag with the key "flag1" to the user. Finally, the trackEvent method is called to track a custom event called "purchase_event".

attributes := map[string]interface{}{"app_version": "1.8.4"}
user := optimizelyClient.CreateUserContext("user123", attributes)

// Without segment option
user.FetchQualifiedSegmentsAsync(nil, func(isFetchSuccessful bool) {
    fmt.Println(isFetchSuccessful)
    decision := user.Decide("flag1", nil)
    user.TrackEvent("purchase_event", nil)
})

// With segment options
odpSegmentOptions := []segment.OptimizelySegmentOption{segment.IgnoreCache, segment.ResetCache}
user.FetchQualifiedSegmentsAsync(odpSegmentOptions, func(isFetchSuccessful bool) {
    fmt.Println(isFetchSuccessful)
    decision := user.Decide("flag1", nil)
    user.TrackEvent("purchase_event", nil)
})
attributes := map[string]interface{}{"app_version": "1.8.4"}
user := optimizelyClient.CreateUserContext("user123", attributes)

// Without segment option
response := user.FetchQualifiedSegments(nil)

// With segment options
odpSegmentOptions := []segment.OptimizelySegmentOption{segment.IgnoreCache, segment.ResetCache}

response = user.FetchQualifiedSegments(odpSegmentOptions)

decision := user.Decide("flag1", nil)
user.TrackEvent("purchase_event", nil)

Once the segments have been fetched, they are cached. This means that if the same user requests 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.

If you want to bypass caching, add the following options to your odpSegmentOptions array:

  • IgnoreCache – Bypass segments cache for lookup and save
  • ResetCache – Reset all segments cache

IsQualifiedFor

Minimum SDK Version

2.0.0-beta

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 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 := map[string]interface{}{"laptop_os": "mac"}
user := optimizelyClient.CreateUserContext("user123", attributes)

response := user.FetchQualifiedSegments(nil)
isQualified = user.IsQualifiedFor("segment1")

Source files

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