Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In


This topic describes the OptimizelyUserContext object for the Optimizely Feature Experimentation Go SDK, which lets you make flag decisions and track events for a user context.

The OptimizelyUserContext object lets you make flag decisions and track events for a user context that you created using the Create User Context method.

Additionally, if you have the Advanced Audience Targeting integration between Optimizely Data Platform (ODP) and Optimizely Feature Experimentation enabled, you can evaluate if your user would qualify for a real-time audience segment.

OptimizelyUserContext minimum SDK version

OptimizelyUserContext is supported on SDK v1.7.0 and higher.

Forced decision methods minimum SDK version

SetForcedDecision(), GetForcedDecision(), RemoveForcedDecision() and RemoveAllForcedDecisions() methods are supported on v1.8.0 and higher.

Optimizely Data Platform Audience Targeting minimum SDK version

FetchQualifiedSegments() and IsQualifiedFor() methods are supported on version 2.0.0-beta and higher. They also require enabling the Advanced Audience Targeting integration.



Advanced Audience Targeting and FetchQualifiedSegments() and IsQualifiedFor() methods are currently in beta. Contact your Customer Success Manager for more information or register now on

OptimizelyUserContext definition

The following code shows the object definition for OptimizelyUserContext:

type OptimizelyUserContext struct {
  UserID     string                
  Attributes map[string]interface{}
// GetOptimizely returns optimizely client instance for Optimizely user context
func (o OptimizelyUserContext) GetOptimizely() *OptimizelyClient
// GetUserID returns userID for Optimizely user context
func (o OptimizelyUserContext) GetUserID() string
// GetUserAttributes returns user attributes for Optimizely user context
func (o OptimizelyUserContext) GetUserAttributes() map[string]interface{}
// SetAttribute sets an attribute for a given key.
func (o *OptimizelyUserContext) SetAttribute(key string, value interface{})
// Decide returns a decision result for a given flag key and a user context, which contains
// all data required to deliver the flag or experiment.
func (o *OptimizelyUserContext) Decide(key string, options []decide.OptimizelyDecideOptions) OptimizelyDecision
// DecideAll returns a key-map of decision results for all active flag keys with options.
func (o *OptimizelyUserContext) DecideAll(options []decide.OptimizelyDecideOptions) map[string]OptimizelyDecision
// DecideForKeys returns a key-map of decision results for multiple flag keys and options.
func (o *OptimizelyUserContext) DecideForKeys(keys []string, options []decide.OptimizelyDecideOptions) map[string]OptimizelyDecision
// TrackEvent generates a conversion event with the given event key if it exists and queues it up to be sent to the Optimizely
// log endpoint for results processing.
func (o *OptimizelyUserContext) TrackEvent(eventKey string, eventTags map[string]interface{}) (err error)
// OptimizelyDecisionContext
type OptimizelyDecisionContext struct {
  FlagKey string
  RuleKey string
// OptimizelyForcedDecision
type OptimizelyForcedDecision struct {
  VariationKey string
// SetForcedDecision sets the forced decision (variation key) for a given decision context (flag key and optional rule key).
// returns true if the forced decision has been set successfully.
func (o *OptimizelyUserContext) SetForcedDecision(context pkgDecision.OptimizelyDecisionContext, decision pkgDecision.OptimizelyForcedDecision) bool
// GetForcedDecision returns the forced decision for a given flag and an optional rule
func (o *OptimizelyUserContext) GetForcedDecision(context pkgDecision.OptimizelyDecisionContext) (pkgDecision.OptimizelyForcedDecision, error)
// RemoveForcedDecision removes the forced decision for a given flag and an optional rule.
func (o *OptimizelyUserContext) RemoveForcedDecision(context pkgDecision.OptimizelyDecisionContext) bool
// RemoveAllForcedDecisions removes all forced decisions bound to this user context.
func (o *OptimizelyUserContext) RemoveAllForcedDecisions() bool
// The following methods require the Audience targeting with Optimizely Data Platform 
// integration enabled. See note below code sample. 
// GetQualifiedSegments returns an array of segment names that the user is qualified for. 
// The result of **FetchQualifiedSegments()** will be saved here. 
// Can be nil if not properly updated with FetchQualifiedSegments(). 
func (o *OptimizelyUserContext) GetQualifiedSegments() []string
// SetQualifiedSegments can read and write directly to the qualified segments array. 
// This allows for bypassing the remote fetching process from ODP 
// or for utilizing your own fetching service.   
func (o *OptimizelyUserContext) SetQualifiedSegments(qualifiedSegments []string)
// FetchQualifiedSegments fetches all qualified segments for the user context.
// The segments fetched will be saved in the **qualifiedSegments** array
// and can be accessed any time. 
func (o *OptimizelyUserContext) FetchQualifiedSegments(options []pkgOdpSegment.OptimizelySegmentOption) (success bool)
// FetchQualifiedSegmentsAsync fetches all qualified segments aysnchronously for the user context. 
// This method will fetch segments in a separate go routine and invoke the provided 
// callback when results are available.
func (o *OptimizelyUserContext) FetchQualifiedSegmentsAsync(options []pkgOdpSegment.OptimizelySegmentOption, callback func(success bool))
// IsQualifiedFor returns true if the user is qualified for the given segment name
func (o *OptimizelyUserContext) IsQualifiedFor(segment string) bool



You must first enable the Optimizely Data Platform Advanced Audience Targeting integration to access theGetQualifiedSegments(), SetQualifiedSegments(), FetchQualifiedSegments(), FetchQualifiedSegmentsAsync(), and IsQualifiedFor() methods. Refer to Advanced Audience Targeting for more information.


The following table shows attributes for the OptimizelyUserContext object:

UserIDStringThe ID of the user
(optional) AttributesMapA map of custom key-value pairs specifying attributes for the user that are used for audience targeting. You can pass the map with the user ID when you create the user.


The following table shows methods for the OptimizelyUserContext object:

SetAttributePass a custom user attribute as a key-value pair to the user context.
GetUserAttributesGet attributes for the user
DecideReturns a decision result for a flag key for a user. The decision result is returned in an OptimizelyDecision object and contains all data required to deliver the flag rule.
See Decide methods
DecideAllReturns decisions for all active (unarchived) flags for a user.
See Decide methods
DecideForKeysReturns a map of flag decisions for specified flag keys.
See Decide methods
TrackEventSee Track Event
SetForcedDecisionForces a user into a specific variation.
See Set Forced Decision
GetForcedDecisionReturns what variation the user was forced into.
See Get Forced Decision
RemoveForcedDecisionRemoves a user from a specific forced variation.
See Remove Forced Decision
RemoveAllForcedDecisionsRemoves a user from all forced variations.
See Remove All Forced Decisions
FetchQualifiedSegments **Fetch all ODP real-time segments for which the user context qualified. See Advanced Audience Targeting segment qualification methods.
FetchQualifiedSegmentsAsync **Fetch all ODP real-time segments aysnchronously for which the user context qualified. See Advanced Audience Targeting segment qualification methods.
IsQualifiedFor **Check if the user context qualified for a given ODP real-time segment. See Advanced Audience Targeting segment qualification methods.

** Requires the Advanced Audience Targeting integration.

See Also

Create User Context

Source files

The language/platform source files containing the implementation for Go is client.go.