---
title: OptimizelyUserContext for the JavaScript (Node) SDK prior to v6
slug: feature-experimentation/docs/optimizelyusercontext-javascript-node-prior-to-v6
docTags: 
createdAt: 2026-08-05T20:39:59.000Z
---

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\`](docId:7WZjge5drvVl3_iYhV4IQ) method.

## OptimizelyUserContext minimum SDK version

OptimizelyUserContext is supported on SDK v3.7 and higher.

### Forced decision methods minimum SDK version

`setForcedDecision()`, `getForcedDecision()`, `removeForcedDecision()` and `removeAllForcedDecisions()` methods are supported on v4.9.0 and higher.

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

`fetch_qualified_segments()` and `is_qualified_for()` methods are supported on version 5.0.0 and higher. They also require configuring the [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) integration.

## `OptimizelyUserContext` definition

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

```typescript
interface OptimizelyUserContext {
    // set an attribute for the user
    setAttribute(key: string, value: unknown): void;

    // return user attributes
    getAttributes(): UserAttributes;

    // make a decision about which flag variation the user buckets into for the flag key 
    decide(
      key: string,
      options: OptimizelyDecideOption[] = []
      ): OptimizelyDecision;

    // make decisions about which flag variations the user buckets into for flag keys
    decideForKeys(
      keys: string[],
      options: OptimizelyDecideOption[] = [],
      ): { [key: string]: OptimizelyDecision };

    // make decisions about which flag variations the user buckets into for all flags
    decideAll(
      options: OptimizelyDecideOption[] = []
      ): { [key: string]: OptimizelyDecision };

    // track user event
    trackEvent(eventName: string, eventTags?: EventTags): void;
    
    // Sets the forced decision (variationKey) for a given decision context
    setForcedDecision(context: OptimizelyDecisionContext, decision: OptimizelyForcedDecision): boolean;

    // Returns the forced decision for a given decision context
    getForcedDecision(context: OptimizelyDecisionContext): OptimizelyForcedDecision | null;

    // Removes the forced decision for a given decision context
    removeForcedDecision(context: OptimizelyDecisionContext): boolean;

    // Removes all forced decisions bound to this user context
    removeAllForcedDecisions(): boolean;
  
    //
    // The following methods require the Real-Time Audiences for Feature Experimentation. 
    // See the note following this code sample. 
    //

    // An array of audience segment names that the user is qualified for. 
    // The result of **fetchQualifiedSegments()** is saved here. 
    // Can be nil if not properly updated with fetchQualifiedSegments(). 
    // 
    //  You can read and write directly to the qualified segments array. 
    //  This lets you bypass the remote fetching process from ODP 
    //  or use your  your own fetching service.   
    let qualifiedSegments = [];

    // Fetch all qualified segments for the user context. 
    //
    // The audience segments fetched are saved in **qualifiedSegments** and can be accessed at any time.
    // On failure, **qualifiedSegments** is nil and one of the following errors is returned:
    // - OptimizelyError.invalidSegmentIdentifier
    // - OptimizelyError.fetchSegmentsFailed(String)
    //
    // - Parameters:
    //   - options – (Optional) A set of options for fetching qualified segments.
    //   - completionHandler – A completion handler to be called with the fetch result. 
    //     On success, passes a non-nil segments array (can be empty) with a nil error. 
    //     On failure, passes a non-nil error with a nil segments array.
    fetchQualifiedSegments(options = [], completionHandler) 
  
    //  Check is the user qualified for the given segment. 
    // - Parameter segment – The segment name to check qualification for.
    // - Returns – True, if qualified.
    isQualifiedFor(segment)
}
```

:::hint{type="info"}
**Note**
You must first configure [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) to access the`qualifiedSegments` array and the `fetchQualifiedSegments()` and `isQualifiedFor()` methods.
:::

## Properties

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

| Attribute             | Type   | Comment                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| --------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \`userId\`            | String | The ID of the user                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| \`attributes\`        | 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.                                                                                                                                                                                                                                                                                                                                     |
| \`qualifiedSegments\` | Array  | An array of audience segment names that the user is qualified for. Requires \[Real-Time Audiences for Optimizely Feature Experimentation]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation). The result of the \`fetchQualifiedSegments()\` array is saved here. The array can be nil if not properly updated with \`fetchQualifiedSegments()\`. You can read and write directly to the qualified segments array. This lets you bypass the remote fetching process from ODP or use your own fetching service. |

## Methods

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

| Method                         | Comment                                                                                                                                                                                                                                                                                                           |
| ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \`setAttribute\`               | Pass a custom user attribute as a key-value pair to the user context.                                                                                                                                                                                                                                             |
| \`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-javascript-node#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-javascript-node#decide-all).                                                                                                           |
| \`decideForKeys\`              | Returns a map of flag decisions for specified flag keys. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-javascript-node).                                                                                                                             |
| \`trackEvent\`                 | Tracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key does not match any existing events. See \[Track Event]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/track-event-javascript-node).                        |
| \`setForcedDecision\`          | Forces a user into a specific variation. See \[Set Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-javascript-node#set-forced-decision-method---setforceddecision).                                                                                |
| \`getForcedDecision\`          | Returns what variation the user was forced into. See \[Get Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-javascript-node#get-forced-decision-method---getforceddecision).                                                                        |
| \`removeForcedDecision\`       | Removes a user from a specific forced variation. See \[Remove Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-javascript-node#remove-forced-decision-method---removeforceddecision).                                                               |
| \`removeAllForcedDecisions\`   | Removes a user from all forced variations. See \[Remove All Forced Decisions]\(doc:).                                                                                                                                                                                                                             |
| \`fetchQualifiedSegment\` \*\* | Fetch all qualified audience segments for the user context. See \[fetchQualfiedSegments]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-segments-for-feature-experimentation-segment-qualification-methods-javascript-node).                                                     |
| \`isQualifiedFor\` \*\*        | Check if the user is qualified for the given audience segment. See \[isQualifiedFor]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-segments-for-feature-experimentation-segment-qualification-methods-javascript-node#isqualifiedfor).                                          |

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

## See Also

[Create User Context](docId\:MXN8iOzOMT-fcdpbgwg8C)

## Source files

The language and platform source files containing the implementation for the JavaScript (Node) SDK are available on [GitHub](https://github.com/optimizely/javascript-sdk/blob/master/lib/optimizely/index.ts).
