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

OptimizelyConfig for the Ruby SDK

How to get access to project configuration data within the datafile using OptimizelyConfig for the Optimizely Feature Experimentation Ruby SDK.

Overview

Optimizely Feature Experimentation SDKs open a well-defined set of public APIs, hiding all implementation details. However, some clients may need access to project configuration data within the datafile.

In this document, we extend our public APIs to define data models and access methods, which clients can use to access project configuration data.

OptimizelyConfig API

A public configuration data model (OptimizelyConfig) is defined below as a structured format of static Optimizely Project data.

Get OptimizelyConfig

OptimizelyConfig can be accessed from OptimizelyClient (top-level) with this public API call:

def get_optimizely_config

getOptimizelyConfig returns an OptimizelyConfig instance, which includes

  • environment key
  • SDK key
  • the datafile revision number
  • all experiments mapped by their key values
  • all attributes
  • all audiences
  • all events
  • feature flags mapped by their key values
  • function to retrieve the project configuration (the datafile)

📘

Note

When the SDK datafile is updated (the client can add a notification listener for OPTIMIZELY_CONFIG_UPDATE to get notified), the client is expected to call the method to get the updated OptimizelyConfig data. See examples below.

Get datafile

To share the same datafile between multiple SDK instances (for example, in a client/server scenario), you can pass a JSON string representation of the config (the datafile) between the instances. To get the datafile, use the OptimizelyConfig object's getDatafile method. For more information, see Sharing the datafile with multiple SDK implementations.

Object model

The following shows the object model for OptimizelyConfig.

# [OptimizelyConfig] is a hash describing the current project configuration data
# being used by this SDK instance. The hash consists of:

revision - string
sdkKey - string
environmentKey - string

# This experimentsMap is for experiments of legacy projects only.
# For flag projects, experiment keys are not guaranteed to be unique 
# across multiple flags, so this map may not include all experiments 
# when keys conflict.

experimentsMap - hash of experiment key to OptimizelyExperiment 
featuresMap - hash of feature flag key to OptimizelyFeature
attributes - list of attributes
audiences - list of all audiences
events - list of events

# [OptimizelyFeature] is a hash describing a feature flag. The hash consists of:

id - string
key - string
experimentRules - list of optimizely feature experiments
deliveryRules - list of optimizely rollout experiments

# Deprecated. Use experimentRules and deliveryRules instead.

experimentsMap - hash of experiment key to OptimizelyExperiment 
variablesMap - hash of variable key to OptimizelyVariable 


# [OptimizelyExperiment] is a hash describing an experiment. The hash consists of:

id - string
key - string
audiences - list of serialized audiences
variationMap - hash of variation key to OptimizelyVariation 


# [OptimizelyVariation] is a hash describing a variation in an experiment. The hash consists of:

id - string
key - string
featureEnabled - bool
variablesMap - hash of variable key to OptimizelyVariable 


# [OptimizelyVariable] is a hash describing a feature flag variable. The hash consists of:

id - string
key - string
type - string
value - string


# [OptimizelyAttribute] is a hash describing an attribute. The hash consists of:

id - string
key - string


# [OptimizelyAudience] is a hash describing an audience. The hash consists of:

id - string
name - string
conditions - string


# [OptimizelyEvent] is a hash describing an event. The hash consists of:

id - string
key - string
experimentIds - list of experiemnt ids

Examples

OptimizelyConfig can be accessed from OptimizelyClient (top-level) like this:

config = optimizely_instance.get_optimizely_config

revision = config['revision']
sdk_key = config['sdkKey']
evironnment_key = config['environmentKey']

#all audiences
audiences = config['audiences']

#all attributes
attributes = config['attributes']

#all events
events = config['events']

# all feature flags

features_map = config['featuresMap']
features = features_map.values
feature_keys = features_map.keys

feature_keys.each do |flag_key|
  flag = features_map[flag_key]
  delivery_rules = flag['deliveryRules']
  experiment_rules = flag['experimentRules']
  
  # use experiment rules and delivery rules and other flag data here...

  experiment_rules.each do |experiment|
    experiment_key = experiment['key']
    audiences = experiment['audiences']
    variations_map = experiment['variationsMap']
    variations_keys = variations_map.keys

    variations_keys.each do |variation_key|
      variation = variations_map[variation_key]
      variables_map = variation['variablesMap']
      variables_map.each do |variable_key|
        variable = variables_map[variable_key]
      end
    end
  end

  delivery_rules.each do |delivery|
    delivery_key = delivery['key']
    audiences = delivery['audiences']
    # use delivery rule data here...
  end
end

# listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
optimizely_instance.notification_center.add_notification_listener( Optimizely::NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE]
) do |*args|
  config = optimizely_instance.get_optimizely_config
end