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.



51
52
53
54
55
56
57
58
59
60
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
# File 'lib/growthbook/context.rb', line 51

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

  features = {}
  attributes = {}

  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
    else
      warn("Unknown context option: #{key}")
    end
  end

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

Instance Attribute Details

#attributesHash

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

Returns:

  • (Hash)

    Map of user attributes that are used to assign variations



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

def attributes
  @attributes
end

#enabledtrue, false

Returns Switch to globally disable all experiments. Default true.

Returns:

  • (true, false)

    Switch to globally disable all experiments. Default true.



7
8
9
# File 'lib/growthbook/context.rb', line 7

def enabled
  @enabled
end

#featuresHash

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

Returns:

  • (Hash)

    Feature definitions (usually pulled from an API or cache)



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

def features
  @features
end

#forced_featuresHash[String, Any]

Returns Forced feature values.

Returns:

  • (Hash[String, Any])

    Forced feature values



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

def forced_features
  @forced_features
end

#forced_variationsHash

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)



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

def forced_variations
  @forced_variations
end

#impressionsHash[String, Growthbook::InlineExperimentResult] (readonly)

Returns Tracked impressions.

Returns:



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

def impressions
  @impressions
end

#listenerGrowthbook::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)`



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

def listener
  @listener
end

#on_feature_usageGrowthbook::FeatureUsageCallback

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

Returns:



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

def on_feature_usage
  @on_feature_usage
end

#qa_modetrue, ...

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.



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

def qa_mode
  @qa_mode
end

#sticky_bucket_assignment_docsString (readonly)

Returns The attributes that are used to assign sticky buckets.

Returns:

  • (String)

    The attributes that are used to assign sticky buckets



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

def sticky_bucket_assignment_docs
  @sticky_bucket_assignment_docs
end

#sticky_bucket_attributesHash[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



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

def sticky_bucket_attributes
  @sticky_bucket_attributes
end

#sticky_bucket_identifier_attributesString (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



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

def sticky_bucket_identifier_attributes
  @sticky_bucket_identifier_attributes
end

#sticky_bucket_serviceGrowthbook::StickyBucketService (readonly)

Returns Sticky bucket service for sticky bucketing.

Returns:



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

def sticky_bucket_service
  @sticky_bucket_service
end

#urlString

Returns The URL of the current page.

Returns:

  • (String)

    The URL of the current page



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

def url
  @url
end

#using_derived_sticky_bucket_attributesBoolean (readonly)

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

Returns:

  • (Boolean)

    If true, the context is using derived sticky bucket attributes



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

def using_derived_sticky_bucket_attributes
  @using_derived_sticky_bucket_attributes
end

Instance Method Details

#eval_feature(key) ⇒ Object



132
133
134
# File 'lib/growthbook/context.rb', line 132

def eval_feature(key)
  _eval_feature(key, Set.new)
end

#feature_value(key, fallback = nil) ⇒ Object



148
149
150
151
# File 'lib/growthbook/context.rb', line 148

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

#off?(key) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/growthbook/context.rb', line 144

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

#on?(key) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/growthbook/context.rb', line 140

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

#run(exp) ⇒ Object



136
137
138
# File 'lib/growthbook/context.rb', line 136

def run(exp)
  _run(exp)
end