HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Is Feature Enabled

This topic describes the Is Feature Enabled method, which determines whether a feature is enabled for a given user.

Determines whether a feature is enabled for a given user. The purpose of this method is to allow you to separate the process of developing and deploying features from the decision to turn on a feature. Build your feature and deploy it to your application behind this flag, then turn the feature on or off for specific users.

Version

SDK v1.0 and higher

Description

This method traverses the client's datafile and checks the feature flag for the feature key that you specify.

  1. Analyzes the user's attributes.
  2. Hashes the userID.

The method evaluates if the feature is being experimented and the user falls in this experiment based on Target audiences and traffic allocation. The method returns true if this is the case otherwise evaluates if the user is in feature rollout. The method checks whether the rollout is enabled, whether the user qualifies for the audience targeting, and then randomly assigns either on or off based on the appropriate traffic allocation. If the feature rollout is on for a qualified user, the method returns true.

Parameters

The table below lists the required and optional parameters in Go.

Parameter

Type

Description

featureKey
required

string

The key of the feature to check.

The feature key is defined from the Features dashboard. For more information, see Create feature flags.

userContext
required

entities.UserContext

Holds information about the user, such as the userID and the user's attributes.

Returns

The value this method returns is determined by your feature flags. This example shows the specific value returned for Go:

True if feature is enabled. Otherwise, false or null.

Examples

This section shows a simple example of how you can use the IsFeatureEnabled method.

attributes := map[string]interface{}{
        "DEVICE": "iPhone",
        "hey":    2,
}

user := entities.UserContext{
        ID:         "userId",
        Attributes: attributes,
}

isEnabled, err := optlyClient.IsFeatureEnabled("feature_key", user)

Side Effects

The table lists other Optimizely functionality that may be triggered by using this method.

Functionality

Description

Impressions

Accessing this method triggers an impression if the user is included in an active Feature test.

Notifications

Invokes the DECISION [notification] callback if this notification is subscribed to.

The example code below shows how to add and remove a decision listener.

import (
    "fmt"

    "github.com/optimizely/go-sdk/pkg/client"
    "github.com/optimizely/go-sdk/pkg/notification"
)

// Callback for decision notification
callback := func(notification notification.DecisionNotification) {

  // Access type on decisionObject to get type of decision
  fmt.Print(notification.Type)
  // Access decisionInfo on decisionObject which
  // will have form as per type of decision.
  fmt.Print(notification.DecisionInfo)
}

optimizelyClient, err := optimizelyFactory.Client()

// Add callback for decision notification
id, err := optimizelyClient.DecisionService.OnDecision(callback)

// Remove callback for decision notification
err = optimizelyClient.DecisionService.RemoveOnDecision(id)

Exceptions

None

Source files

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


Did this page help you?