Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback

OptimizelyUserContext for the Go SDK

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 have already created using the Create User Context method.

OptimizelyUserContext minimum version

v1.7.0 and higher.

Forced decision methods minimum version

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

OptimizelyUserContext definition

The 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

Properties

The table shows attributes for the OptimizelyUserContext object:

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

Methods

The table shows methods for the OptimizelyUserContext object:

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