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 ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev 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.

Optimizely Feature Experimentation SDKs open a well-defined set of public APIs, hiding implementation details. However, you may need access to project configuration data within the datafile. The following section extends the public APIs to define data models and access methods, which you can use to access project configuration data.

OptimizelyConfig API

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

Get OptimizelyConfig

You can access OptimizelyConfig through the OptimizelyClient (top-level) using this public API call.

def get_optimizely_config

getOptimizelyConfig returns an OptimizelyConfig instance, which includes the following:

  • Environment key
  • SDK key
  • Datafile revision number
  • Experiments mapped by key values
  • All attributes
  • All audiences
  • All events
  • Feature flags mapped by key values
  • Function to retrieve the project configuration (the datafile)

📘

Note

When the system updates the datafile, you can add a OptimizelyConfigUpdate notification listener to get notified. After receiving the notification, call the method to get the updated OptimizelyConfig data.

Get datafile

To share the same datafile between multiple SDK instances, such as in a client and server scenario, pass a JSON string representation of the config (the datafile) between 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 sample 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

You can access OptimizelyConfig from the OptimizelyClient (top-level) using the following code example:

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