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

Advanced Audience Targeting segment qualification methods for the Go SDK

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.

👍

Beta

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

FetchQualifiedSegments

Minimum SDK version

2.0.0

Description

You can use the FetchQualifiedSegments method to retrieve the external audience mapping for a specific user from the Optimizely Data Platform (ODP) server. The Optimizely Feature Experimentation Go SDK provides a synchronous and asynchronous version of the 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 Optimizely Data Platform (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)

The following diagram shows the network calls between your application, the Go SDK, and the ODP server when calling FetchQualifiedSegments.

Go SDK to ODP network diagram
  1. Call FetchQualifiedSegments method.
  2. The Go SDK makes GraphQL call to ODP to fetch segments.
  3. ODP responds with segments.
  4. Fetched segments mapping user IDs to segments are cached, for more information see below.
  5. Appropriate variations are returned for user.

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

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 and platform source files containing the implementation for Go are available on GitHub.