Class: Growthbook::Context
- Inherits:
-
Object
- Object
- Growthbook::Context
- Defined in:
- lib/growthbook/context.rb
Overview
Context object passed into the GrowthBook constructor.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
Map of user attributes that are used to assign variations.
-
#enabled ⇒ true, false
Switch to globally disable all experiments.
-
#features ⇒ Hash
Feature definitions (usually pulled from an API or cache).
-
#forced_features ⇒ Hash[String, Any]
Forced feature values.
-
#forced_variations ⇒ Hash
Force specific experiments to always assign a specific variation (used for QA).
-
#impressions ⇒ Hash[String, Growthbook::InlineExperimentResult]
readonly
Tracked impressions.
-
#listener ⇒ Growthbook::TrackingCallback
An object that responds to ‘on_experiment_viewed(GrowthBook::InlineExperiment, GrowthBook::InlineExperimentResult)`.
-
#on_feature_usage ⇒ Growthbook::FeatureUsageCallback
An object that responds to ‘on_feature_usage(String, Growthbook::FeatureResult)`.
-
#qa_mode ⇒ true, ...
If true, random assignment is disabled and only explicitly forced variations are used.
-
#url ⇒ String
The URL of the current page.
Instance Method Summary collapse
- #eval_feature(key) ⇒ Object
- #feature_value(key, fallback = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Context
constructor
A new instance of Context.
- #off?(key) ⇒ Boolean
- #on?(key) ⇒ Boolean
- #run(exp) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Context
Returns a new instance of Context.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/growthbook/context.rb', line 36 def initialize( = {}) @features = {} @forced_variations = {} @forced_features = {} @attributes = {} @enabled = true @impressions = {} .transform_keys(&:to_sym).each do |key, value| case key when :enabled @enabled = value when :attributes self.attributes = value when :url @url = value when :decryption_key nil when :encrypted_features decrypted = () self.features = decrypted unless decrypted.nil? when :features self.features = value when :forced_variations, :forcedVariations self.forced_variations = value when :forced_features self.forced_features = value when :qa_mode, :qaMode @qa_mode = value when :listener @listener = value when :on_feature_usage @on_feature_usage = value else warn("Unknown context option: #{key}") end end end |
Instance Attribute Details
#attributes ⇒ Hash
Returns Map of user attributes that are used to assign variations.
22 23 24 |
# File 'lib/growthbook/context.rb', line 22 def attributes @attributes end |
#enabled ⇒ true, false
Returns Switch to globally disable all experiments. Default true.
7 8 9 |
# File 'lib/growthbook/context.rb', line 7 def enabled @enabled end |
#features ⇒ Hash
Returns Feature definitions (usually pulled from an API or cache).
25 26 27 |
# File 'lib/growthbook/context.rb', line 25 def features @features end |
#forced_features ⇒ Hash[String, Any]
Returns Forced feature values.
34 35 36 |
# File 'lib/growthbook/context.rb', line 34 def forced_features @forced_features end |
#forced_variations ⇒ Hash
Returns Force specific experiments to always assign a specific variation (used for QA).
28 29 30 |
# File 'lib/growthbook/context.rb', line 28 def forced_variations @forced_variations end |
#impressions ⇒ Hash[String, Growthbook::InlineExperimentResult] (readonly)
Returns Tracked impressions.
31 32 33 |
# File 'lib/growthbook/context.rb', line 31 def impressions @impressions end |
#listener ⇒ Growthbook::TrackingCallback
Returns An object that responds to ‘on_experiment_viewed(GrowthBook::InlineExperiment, GrowthBook::InlineExperimentResult)`.
16 17 18 |
# File 'lib/growthbook/context.rb', line 16 def listener @listener end |
#on_feature_usage ⇒ Growthbook::FeatureUsageCallback
Returns An object that responds to ‘on_feature_usage(String, Growthbook::FeatureResult)`.
19 20 21 |
# File 'lib/growthbook/context.rb', line 19 def on_feature_usage @on_feature_usage end |
#qa_mode ⇒ true, ...
Returns If true, random assignment is disabled and only explicitly forced variations are used.
13 14 15 |
# File 'lib/growthbook/context.rb', line 13 def qa_mode @qa_mode end |
#url ⇒ String
Returns The URL of the current page.
10 11 12 |
# File 'lib/growthbook/context.rb', line 10 def url @url end |
Instance Method Details
#eval_feature(key) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/growthbook/context.rb', line 100 def eval_feature(key) # Forced in the context return get_feature_result(key.to_s, @forced_features[key.to_s], 'override', nil, nil) if @forced_features.key?(key.to_s) # Return if we can't find the feature definition feature = get_feature(key) return get_feature_result(key.to_s, nil, 'unknownFeature', nil, nil) unless feature feature.rules.each do |rule| # Targeting condition next if rule.condition && !condition_passes?(rule.condition) # If there are filters for who is included (e.g. namespaces) next if rule.filters && filtered_out?(rule.filters || []) # If this is a percentage rollout, skip if not included if rule.force? seed = rule.seed || key hash_attribute = rule.hash_attribute || 'id' included_in_rollout = included_in_rollout?( seed: seed.to_s, hash_attribute: hash_attribute, range: rule.range, coverage: rule.coverage, hash_version: rule.hash_version ) next unless included_in_rollout return get_feature_result(key.to_s, rule.force, 'force', nil, nil) end # Experiment rule next unless rule.experiment? exp = rule.to_experiment(key) next if exp.nil? result = _run(exp, key) next unless result.in_experiment && !result.passthrough return get_feature_result(key.to_s, result.value, 'experiment', exp, result) end # Fallback get_feature_result(key.to_s, feature.default_value.nil? ? nil : feature.default_value, 'defaultValue', nil, nil) end |
#feature_value(key, fallback = nil) ⇒ Object
159 160 161 162 |
# File 'lib/growthbook/context.rb', line 159 def feature_value(key, fallback = nil) value = eval_feature(key).value value.nil? ? fallback : value end |
#off?(key) ⇒ Boolean
155 156 157 |
# File 'lib/growthbook/context.rb', line 155 def off?(key) eval_feature(key).off end |
#on?(key) ⇒ Boolean
151 152 153 |
# File 'lib/growthbook/context.rb', line 151 def on?(key) eval_feature(key).on end |
#run(exp) ⇒ Object
147 148 149 |
# File 'lib/growthbook/context.rb', line 147 def run(exp) _run(exp) end |