---
title: OptimizelyUserContext for the Go SDK
slug: feature-experimentation/docs/optimizelyusercontext-go
docTags: 
createdAt: 2026-08-05T20:39:55.000Z
---

The OptimizelyUserContext object lets you make flag decisions and track events for a user context that you created using the [Create User Context](docId\:UnFDhdqan0QQo54R4_7a3) method.

Additionally, if you have [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) configured, you can evaluate if your user would qualify for a real-time audience.

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

### Real-Time Audiences for Feature Experimentation minimum SDK version

`FetchQualifiedSegments()` and `IsQualifiedFor()` methods are supported on version 2.0.0 and higher.

## OptimizelyUserContext definition

The following code shows the object definition for `OptimizelyUserContext`:

```go
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 Real-Time Audiences for Feature Experimentation. 
// See note following this code sample. 
//
 
// GetQualifiedSegments returns an array of segment names that the user is qualified for. 
// The result of **FetchQualifiedSegments()** is 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 lets you bypass 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 are 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 asynchronously for the user context. 
// This method fetches 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
```

:::hint{type="info"}
**Note**
You must first configure  [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) to access the`GetQualifiedSegments()`, `SetQualifiedSegments()`, `FetchQualifiedSegments()`, `FetchQualifiedSegmentsAsync()`, and `IsQualifiedFor()` methods.
:::

## Properties

The following table shows attributes for the `OptimizelyUserContext` object:

| Attribute                 | Type   | Comment                                                                                                                                                                                                                                        |
| ------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \`UserID\`                | String | The ID of the user                                                                                                                                                                                                                             |
| \`Attributes\` (optional) | Map    | A map of custom key-value pairs specifying attributes for the user that are used for \[audience targeting]\(https\://support.optimizely.com/hc/en-us/articles/38683417490957). You can pass the map with the user ID when you create the user. |

## Methods

The following table shows methods for the `OptimizelyUserContext` object:

| Method                           | Comment                                                                                                                                                                                                                                                                                                                                            |
| -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| SetAttribute                     | Pass a custom user attribute as a key-value pair to the user context.                                                                                                                                                                                                                                                                              |
| GetUserAttributes                | Get attributes for the user                                                                                                                                                                                                                                                                                                                        |
| Decide                           | Returns 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]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-go#example-decide)                                        |
| DecideAll                        | Returns decisions for all active (unarchived) flags for a user. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-go#example-decideall)                                                                                                                                                   |
| DecideForKeys                    | Returns a map of flag decisions for specified flag keys. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-go)                                                                                                                                                                            |
| TrackEvent                       | See \[Track Event]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/track-event-go)                                                                                                                                                                                                                                           |
| SetForcedDecision                | Forces a user into a specific variation. See \[Set Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-go#set-forced-decision-method)                                                                                                                                                   |
| GetForcedDecision                | Returns what variation the user was forced into. See \[Get Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-go#get-forced-decision-method)                                                                                                                                           |
| RemoveForcedDecision             | Removes a user from a specific forced variation. See \[Remove Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-go#remove-forced-decision-method---removeforceddecision)                                                                                                              |
| RemoveAllForcedDecisions         | Removes a user from all forced variations. See \[Remove All Forced Decisions]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-go#remove-all-forced-decisions-method---removeallforceddecisions)                                                                                                      |
| FetchQualifiedSegments \*\*      | Fetch all ODP real-time audiences for which the user context qualified. See \[Real-Time Audiences for Feature Experimentation segment qualification methods for the Go SDK]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-go).               |
| FetchQualifiedSegmentsAsync \*\* | Fetch all ODP real-time audiences that the user context qualified for asynchronously. See \[Real-Time Audiences for Feature Experimentation segment qualification methods for the Go SDK]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-go). |
| IsQualifiedFor \*\*              | Check if the user context qualifies for a given ODP real-time audience. See \[Real-Time Audiences for Feature Experimentation segment qualification methods for the Go SDK]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-go).               |

\*\* Requires [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8).

## See also

[Create User Context](docId\:UnFDhdqan0QQo54R4_7a3)

## Source files

The language and platform source files containing the implementation for Go is available on [GitHub](https://github.com/optimizely/go-sdk/blob/master/pkg/client/client.go).
