Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

OptimizelyUserContext for the JavaScript (Node) SDK

Describes the OptimizelyUserContext object, which allows you to make flag decisions and track events for a user context for the Optimizely Feature Experimentation JavaScript (Node) SDK.

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 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 Segments 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 Segments for Feature Experimentation integration.

OptimizelyUserContext definition

The following code shows the object definition for OptimizelyUserContext:

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 Segments for Feature Experimentation. 
    // See note below code sample. 
    //

    // 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(). 
    // 
    //  You 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.   
    let qualifiedSegments = [];

    // Fetch all qualified segments for the user context. 
    //
    // The segments fetched will be saved in **qualifiedSegments** and can be accessed any time.
    // On failure, **qualifiedSegments** will be nil and one of these errors will be returned:
    // - OptimizelyError.invalidSegmentIdentifier
    // - OptimizelyError.fetchSegmentsFailed(String)
    //
    // - Parameters:
    //   - options: A set of options for fetching qualified segments (optional).
    //   - completionHandler: A completion handler to be called with the fetch result. 
    //     On success, it will pass a non-nil segments array (can be empty) with a nil error. 
    //     On failure, it will pass 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)
}

📘

Note

You must first configure Real-Time Segments for Feature Experimentation to access thequalifiedSegments array and fetchQualifiedSegments() and isQualifiedFor() methods.

Properties

The following table shows attributes for the OptimizelyUserContext object:

AttributeTypeComment
userIdStringThe ID of the user
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.
qualifiedSegmentsArrayAn array of segment names that the user is qualified for. Requires Real-Time Segments for Optimizely 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 Optimizely Data Platform (ODP) or utilize your own fetching service.

Methods

The following table shows methods for the OptimizelyUserContext object:

MethodComment
setAttributePass a custom user attribute as a key-value pair to the user context.
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.
trackEventTracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key doesn't match any existing events.
See 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 qualified segments for the user context.
See fetchQualfiedSegments.
isQualifiedFor**Check if the user is qualified for the given segment.
See isQualifiedFor.

** Requires Real-Time Segments for Feature Experimentation.

See Also

Create User Context

Source files

The language and platform source files containing the implementation for the JavaScript (Node) SDK are available on GitHub.