Class GrowthBook

java.lang.Object
growthbook.sdk.java.GrowthBook

public class GrowthBook extends Object
GrowthBook SDK class. Build a context with GBContext.builder() or the GBContext constructor and pass it as an argument to the class constructor.
  • Field Details

  • Constructor Details

    • GrowthBook

      public GrowthBook(GBContext context)
      Initialize the GrowthBook SDK with a provided GBContext
      Parameters:
      context - GBContext
    • GrowthBook

      public GrowthBook()
      No-args constructor. A GBContext with default values is created. It's recommended to create your own context with GBContext.builder() or the GBContext constructor
  • Method Details

    • evalFeature

      @Nullable public <ValueType> FeatureResult<ValueType> evalFeature(String key, Class<ValueType> valueTypeClass)
      The evalFeature method takes a single string argument, which is the unique identifier for the feature and returns a FeatureResult object.

      There are a few ordered steps to evaluate a feature

      1. If the key doesn't exist in context.getFeatures() 1.1 Return getFeatureResult(null, "unknownFeature") 2. Loop through the feature rules (if any) 2.1 If the rule has parentConditions (prerequisites) defined, loop through each one: 2.1.1 Call evalFeature on the parent condition 2.1.1.1 If a cycle is detected, break out of feature evaluation and return getFeatureResult(null, "cyclicPrerequisite") 2.1.2 Using the evaluated parent's result, create an object

      Type Parameters:
      ValueType - Gson deserializable type
      Parameters:
      key - name of the feature
      valueTypeClass - the class of the generic, e.g. MyFeature.class
      Returns:
      ValueType instance
    • setFeatures

      public void setFeatures(String featuresJsonString)
      Method for pass feature json in format String to GbContext
      Parameters:
      featuresJsonString - features JSON from the GrowthBook API
    • setSavedGroups

      public void setSavedGroups(com.google.gson.JsonObject savedGroups)
      Method for pass saved groups JsonObject to GbContext
      Parameters:
      savedGroups - features JSON from the GrowthBook API
    • setAttributes

      public void setAttributes(String attributesJsonString)
      Update the user's attributes
      Parameters:
      attributesJsonString - user attributes JSON
    • run

      public <ValueType> ExperimentResult<ValueType> run(Experiment<ValueType> experiment)
      The run method takes an Experiment object and returns an ExperimentResult. There are a bunch of ordered steps to run an experiment: 1. If experiment.variations has fewer than 2 variations, return getExperimentResult(experiment) 2. If context.enabled is false, return getExperimentResult(experiment) 3. If context.url exists 4. Return if forced via context 5. If experiment.active is set to false, return getExperimentResult(experiment) 6. Get the user hash value and return if empty 6.1 If sticky bucketing is permitted, check to see if a sticky bucket value exists. If so, skip steps 7-8. 7. Apply filters and namespace 7.1 If experiment.filters is set 7.2 Else if experiment.namespace is set, return if not in range 8. Return if any conditions are not met, return 8.1 If experiment.condition is set, return if it evaluates to false 8.2 If experiment.parentConditions is set (prerequisites), return if any of them evaluate to false. See the corresponding logic in evalFeature for more details. (Note that the gate flag should not be set in an experiment) 8.3 Apply any url targeting based on experiment.urlPatterns, return if no match 9. Choose a variation 9.1 If a sticky bucket value exists, use it. 9.1.1 If the found sticky bucket version is blocked (doesn't exceed experiment.minBucketVersion), then skip enrollment 9.2 Else, calculate bucket ranges for the variations and choose one 10. If assigned == -1, return getExperimentResult(experiment) 11. If experiment has a forced variation, return 12. If context.qaMode, return getExperimentResult(experiment) 13. Build the result object 14. Fire context.trackingCallback if set and the combination of hashAttribute, hashValue, experiment.key, and variationId has not been tracked before 15. Return result
      Type Parameters:
      ValueType - Gson deserializable type
      Parameters:
      experiment - Experiment object
      Returns:
      ExperimentResult instance
    • setOwnStickyBucketService

      public void setOwnStickyBucketService(@Nullable StickyBucketService stickyBucketService)
      Setting your own implementation of StickyBucketService interface
      Parameters:
      stickyBucketService - StickyBucketService
    • setInMemoryStickyBucketService

      public void setInMemoryStickyBucketService()
      Setting default in memory implementation of StickyBucketService interface
    • isOn

      public Boolean isOn(String featureKey)
      Returns true if the value is a truthy value Only the following values are considered to be "falsy": 1) null 2) false 3) "" 4) 0 Everything else is considered "truthy", including empty arrays and objects. If the value is "truthy", then isOn() will return true and isOff() will return false. If the value is "false", then the opposite values will be returned.
      Parameters:
      featureKey - name of the feature
      Returns:
      true if the value is a truthy value
    • isOff

      public Boolean isOff(String featureKey)
      Returns true if the value is a falsy value Only the following values are considered to be "falsy": 1) null 2) false 3) "" 4) 0 Everything else is considered "truthy", including empty arrays and objects. If the value is "truthy", then isOn() will return true and isOff() will return false. If the value is "false", then the opposite values will be returned.
      Parameters:
      featureKey - name of the feature
      Returns:
      true if the value is a truthy value
    • getFeatureValue

      public Boolean getFeatureValue(String featureKey, Boolean defaultValue)
      Get the feature value as a boolean
      Parameters:
      featureKey - name of the feature
      defaultValue - boolean value to return
      Returns:
      the found value or defaultValue
    • getFeatureValue

      public String getFeatureValue(String featureKey, String defaultValue)
      Get the feature value as a string
      Parameters:
      featureKey - name of the feature
      defaultValue - string value to return
      Returns:
      the found value or defaultValue
    • getFeatureValue

      public Float getFeatureValue(String featureKey, Float defaultValue)
      Get the feature value as a float
      Parameters:
      featureKey - name of the feature
      defaultValue - float value to return
      Returns:
      the found value or defaultValue
    • getFeatureValue

      public Integer getFeatureValue(String featureKey, Integer defaultValue)
      Get the feature value as an integer
      Parameters:
      featureKey - name of the feature
      defaultValue - integer value to return
      Returns:
      the found value or defaultValue
    • getFeatureValue

      public Object getFeatureValue(String featureKey, Object defaultValue)
      Get the feature value as an Object. This may be useful for implementations that do not use Gson.
      Parameters:
      featureKey - feature identifier
      defaultValue - default object value
      Returns:
      Object
    • getFeatureValue

      public <ValueType> ValueType getFeatureValue(String featureKey, ValueType defaultValue, Class<ValueType> gsonDeserializableClass)
      Get the feature value as a Gson-deserializable. If your class requires a custom deserializer, use getFeatureValue(String, Object) instead and deserialize it with your own Gson instance.
      Type Parameters:
      ValueType - Gson deserializable type
      Parameters:
      featureKey - feature identifier
      defaultValue - default generic class
      gsonDeserializableClass - the class of the generic, e.g. MyFeature.class
      Returns:
      ValueType instance
    • evaluateCondition

      public Boolean evaluateCondition(String attributesJsonString, String conditionJsonString)
      Evaluate a condition for a set of user attributes based on the provided condition. The condition syntax closely resembles MongoDB's syntax. This is defined in the Feature's targeting conditions' Advanced settings

      This is the main function used to evaluate a condition. It loops through the condition key/value pairs and checks each entry:

      1. If condition key is $or, check if evalOr(attributes, condition["$or"]) is false. If so, break out of the loop and return false 2. If condition key is $nor, check if !evalOr(attributes, condition["$nor"]) is false. If so, break out of the loop and return false 3. If condition key is $and, check if evalAnd(attributes, condition["$and"]) is false. If so, break out of the loop and return false 4. If condition key is $not, check if !evalCondition(attributes, condition["$not"]) is false. If so, break out of the loop and return false 5. Otherwise, check if evalConditionValue(value, getPath(attributes, key)) is false. If so, break out of the loop and return false If none of the entries failed their checks, evalCondition returns true

      Parameters:
      attributesJsonString - A JsonObject of the user attributes to evaluate
      conditionJsonString - A JsonObject of the condition
      Returns:
      Whether the condition should be true for the user
    • getFeatureValue

      public Double getFeatureValue(String featureKey, Double defaultValue)
      Get the feature value as a double
      Parameters:
      featureKey - name of the feature
      defaultValue - integer value to return
      Returns:
      the found value or defaultValue
    • destroy

      public void destroy()
      Reinitialized the list of ExperimentRunCallbacks. This method clears the current list of callbacks by replacing it with a new empty ArrayList.
    • subscribe

      public void subscribe(ExperimentRunCallback callback)
      This method add new calback to list of ExperimentRunCallback
      Parameters:
      callback - ExperimentRunCallback interface
    • featuresAPIModelSuccessfully

      public void featuresAPIModelSuccessfully(String featuresDataModel)
      Update sticky bucketing configuration Method that get cached assignments and set it to Context's Sticky Bucket Assignments documents
      Parameters:
      featuresDataModel - Json in format of String. See info how it looks like here ...
    • isFeatureEnabled

      public Boolean isFeatureEnabled(String featureKey)
      This method return boolean result if feature enabled by environment it would be present in context
      Parameters:
      featureKey - Feature name
      Returns:
      Whether feature is present in GBContext
    • getAttributeOverrides

      public com.google.gson.JsonObject getAttributeOverrides()
    • setAttributeOverrides

      public void setAttributeOverrides(com.google.gson.JsonObject attributeOverrides)
    • getForcedFeatureValues

      public Map<String,Object> getForcedFeatureValues()
    • setForcedFeatureValues

      public void setForcedFeatureValues(Map<String,Object> forcedFeatureValues)