HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

OptimizelyUserContext - Forced Decisions

This topic describes the OptimizelyUserContext Object which allows you to make flag decisions and track events for a user context.

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

OptimizelyUserContext version

OptimizelyUserContext is supported on SDK v1.7.0 and higher.

Forced decision methods version

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

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 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

See Also

Create User Context

Source files

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