Class: Growthbook::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/growthbook/context.rb

Overview

Context object passed into the GrowthBook constructor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Context

Returns a new instance of Context.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/growthbook/context.rb', line 61

def initialize(options = {})
  @features = {}
  @attributes = {}
  @forced_variations = {}
  @forced_features = {}
  @attributes = {}
  @enabled = true
  @impressions = {}
  @sticky_bucket_assignment_docs = {}
  @saved_groups = {}
  @remote_group_ids = nil

  features = {}
  attributes = {}
  remote_group_ids = nil

  options.transform_keys(&:to_sym).each do |key, value|
    case key
    when :enabled
      @enabled = value
    when :attributes
      attributes = value
    when :url
      @url = value
    when :decryption_key
      nil
    when :encrypted_features
      decrypted = decrypted_features_from_options(options)
      features = decrypted unless decrypted.nil?
    when :features
      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
    when :sticky_bucket_service
      @sticky_bucket_service = value
    when :sticky_bucket_identifier_attributes
      @sticky_bucket_identifier_attributes = value
    when :saved_groups, :savedGroups
      @saved_groups = value || {}
    when :remote_group_ids, :remoteGroupIds
      remote_group_ids = value
    else
      warn("Unknown context option: #{key}")
    end
  end

  @using_derived_sticky_bucket_attributes = !@sticky_bucket_identifier_attributes
  self.attributes = attributes
  self.remote_group_ids = remote_group_ids
  self.features = features
end

Instance Attribute Details

#attributes ⇒ Hash

Returns Map of user attributes that are used to assign variations.

Returns:

  • (Hash) —

    Map of user attributes that are used to assign variations



24
25
26
# File 'lib/growthbook/context.rb', line 24

def attributes
  @attributes
end

#enabled ⇒ true, false

Returns Switch to globally disable all experiments. Default true.

Returns:

  • (true, false) —

    Switch to globally disable all experiments. Default true.



9
10
11
# File 'lib/growthbook/context.rb', line 9

def enabled
  @enabled
end

#features ⇒ Hash

Returns Feature definitions (usually pulled from an API or cache).

Returns:

  • (Hash) —

    Feature definitions (usually pulled from an API or cache)



27
28
29
# File 'lib/growthbook/context.rb', line 27

def features
  @features
end

#forced_features ⇒ Hash[String, Any]

Returns Forced feature values.

Returns:

  • (Hash[String, Any]) —

    Forced feature values



36
37
38
# File 'lib/growthbook/context.rb', line 36

def forced_features
  @forced_features
end

#forced_variations ⇒ Hash

Returns Force specific experiments to always assign a specific variation (used for QA).

Returns:

  • (Hash) —

    Force specific experiments to always assign a specific variation (used for QA)



30
31
32
# File 'lib/growthbook/context.rb', line 30

def forced_variations
  @forced_variations
end

#impressions ⇒ Hash[String, Growthbook::InlineExperimentResult] (readonly)

Returns Tracked impressions.

Returns:



33
34
35
# File 'lib/growthbook/context.rb', line 33

def impressions
  @impressions
end

#listener ⇒ Growthbook::TrackingCallback

Returns An object that responds to on_experiment_viewed(GrowthBook::InlineExperiment, GrowthBook::InlineExperimentResult).

Returns:

  • (Growthbook::TrackingCallback) —

    An object that responds to on_experiment_viewed(GrowthBook::InlineExperiment, GrowthBook::InlineExperimentResult)



18
19
20
# File 'lib/growthbook/context.rb', line 18

def listener
  @listener
end

#on_feature_usage ⇒ Growthbook::FeatureUsageCallback

Returns An object that responds to on_feature_usage(String, Growthbook::FeatureResult).

Returns:



21
22
23
# File 'lib/growthbook/context.rb', line 21

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.

Returns:

  • (true, false, nil) —

    If true, random assignment is disabled and only explicitly forced variations are used.



15
16
17
# File 'lib/growthbook/context.rb', line 15

def qa_mode
  @qa_mode
end

#remote_group_ids ⇒ Array<String>?

Returns The remote saved groups the user is in, resolved by the app. nil means unresolved, which is not the same as no groups: rules using remote groups then fail, "not in group" included. See #add_remote_saved_groups.

Returns:

  • (Array<String>, nil) —

    The remote saved groups the user is in, resolved by the app. nil means unresolved, which is not the same as no groups: rules using remote groups then fail, "not in group" included. See #add_remote_saved_groups.



44
45
46
# File 'lib/growthbook/context.rb', line 44

def remote_group_ids
  @remote_group_ids
end

#saved_groups ⇒ Hash[String, Array]

Returns Saved groups, used by the $inGroup/$notInGroup operators.

Returns:

  • (Hash[String, Array]) —

    Saved groups, used by the $inGroup/$notInGroup operators



39
40
41
# File 'lib/growthbook/context.rb', line 39

def saved_groups
  @saved_groups
end

#sticky_bucket_assignment_docs ⇒ String (readonly)

Returns The attributes that are used to assign sticky buckets.

Returns:

  • (String) —

    The attributes that are used to assign sticky buckets



53
54
55
# File 'lib/growthbook/context.rb', line 53

def sticky_bucket_assignment_docs
  @sticky_bucket_assignment_docs
end

#sticky_bucket_attributes ⇒ Hash[String, String] (readonly)

Returns The attributes that are used to assign sticky buckets.

Returns:

  • (Hash[String, String]) —

    The attributes that are used to assign sticky buckets



59
60
61
# File 'lib/growthbook/context.rb', line 59

def sticky_bucket_attributes
  @sticky_bucket_attributes
end

#sticky_bucket_identifier_attributes ⇒ String (readonly)

Returns The attributes that identify users. If omitted, this will be inferred from the feature definitions.

Returns:

  • (String) —

    The attributes that identify users. If omitted, this will be inferred from the feature definitions



50
51
52
# File 'lib/growthbook/context.rb', line 50

def sticky_bucket_identifier_attributes
  @sticky_bucket_identifier_attributes
end

#sticky_bucket_service ⇒ Growthbook::StickyBucketService (readonly)

Returns Sticky bucket service for sticky bucketing.

Returns:



47
48
49
# File 'lib/growthbook/context.rb', line 47

def sticky_bucket_service
  @sticky_bucket_service
end

#url ⇒ String

Returns The URL of the current page.

Returns:

  • (String) —

    The URL of the current page



12
13
14
# File 'lib/growthbook/context.rb', line 12

def url
  @url
end

#using_derived_sticky_bucket_attributes ⇒ Boolean (readonly)

Returns If true, the context is using derived sticky bucket attributes.

Returns:

  • (Boolean) —

    If true, the context is using derived sticky bucket attributes



56
57
58
# File 'lib/growthbook/context.rb', line 56

def using_derived_sticky_bucket_attributes
  @using_derived_sticky_bucket_attributes
end

Instance Method Details

#add_remote_saved_groups(resolver) ⇒ Growthbook::Context

Looks up the user's remote saved groups with resolver and sets them as #remote_group_ids. Leaves the context unchanged when the payload has no remote groups or the resolver raises.

Requires a saved group references v2 payload for the group metadata. With inline/v1 payloads, resolve membership in the app and set #remote_group_ids directly instead.

Parameters:

  • resolver (#call) —

    Called with the attributes the payload's remote groups use (keyed by attributeKey) and the groups (hashes with 'id', 'attributeKey' and, for a derived group, 'valuesFrom'). Returns the ids of the groups the user is in. See Growthbook::RedisResolver.

Returns:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/growthbook/context.rb', line 175

def add_remote_saved_groups(resolver)
  saved_groups = @saved_groups
  # No remote entries doesn't mean no remote rules: payloads for SDKs without
  # remote support check __gb_remoteGroupIds directly. Leaving it unset makes
  # those rules fail closed; [] would let every "not in group" rule match.
  return self if RemoteSavedGroups.groups(saved_groups).empty?

  ids = RemoteSavedGroups.resolve(@attributes, saved_groups, resolver)
  # A new payload set during the lookup can add groups the result doesn't
  # cover, and a missing group would pass "not in group" rules. Look up once
  # more for the new payload.
  unless @saved_groups.equal?(saved_groups)
    return self if RemoteSavedGroups.groups(@saved_groups).empty?

    ids = RemoteSavedGroups.resolve(@attributes, @saved_groups, resolver)
  end
  self.remote_group_ids = ids
  self
rescue StandardError => e
  # The class only: messages can hold attribute values, such as a Redis key
  warn("GrowthBook: could not resolve remote saved groups (#{e.class})")
  self
end

#eval_feature(key) ⇒ Object



207
208
209
210
# File 'lib/growthbook/context.rb', line 207

def eval_feature(key)
  RemoteSavedGroups.warn_if_missing_ids(@attributes, @remote_group_ids, @saved_groups)
  _eval_feature(key, Set.new)
end

#feature_value(key, fallback = nil) ⇒ Object



225
226
227
228
# File 'lib/growthbook/context.rb', line 225

def feature_value(key, fallback = nil)
  value = eval_feature(key).value
  value.nil? ? fallback : value
end

#off?(key) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/growthbook/context.rb', line 221

def off?(key)
  eval_feature(key).off
end

#on?(key) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/growthbook/context.rb', line 217

def on?(key)
  eval_feature(key).on
end

#run(exp) ⇒ Object



212
213
214
215
# File 'lib/growthbook/context.rb', line 212

def run(exp)
  RemoteSavedGroups.warn_if_missing_ids(@attributes, @remote_group_ids, @saved_groups)
  _run(exp)
end