Module: Statsig

Defined in:
lib/memo.rb,
lib/id_list.rb,
lib/network.rb,
lib/statsig.rb,
lib/ttl_set.rb,
lib/constants.rb,
lib/evaluator.rb,
lib/hash_utils.rb,
lib/spec_store.rb,
lib/diagnostics.rb,
lib/sdk_configs.rb,
lib/config_result.rb,
lib/error_boundary.rb,
lib/statsig_errors.rb,
lib/statsig_logger.rb,
lib/evaluation_details.rb,
lib/interfaces/data_store.rb,
lib/client_initialize_helpers.rb,
lib/user_persistent_storage_utils.rb,
lib/interfaces/user_persistent_storage.rb

Defined Under Namespace

Modules: Const, EvaluationReason, Interfaces Classes: CheckGateOptions, ConfigResult, Diagnostics, ErrorBoundary, EvaluationDetails, Evaluator, GetConfigOptions, GetExperimentOptions, GetGateOptions, GetLayerOptions, HashUtils, IDList, InvalidSDKKeyResponse, Memo, Network, NetworkError, ResponseFormatter, SDKConfigs, SpecStore, StatsigLogger, TTLSet, UninitializedError, UserPersistentStorageUtils, ValueError

Class Method Summary collapse

Class Method Details

.check_gate(user, gate_name, options = nil) ⇒ Boolean

Gets the boolean result of a gate, evaluated against the given user. An exposure event will automatically be logged for the gate.

Parameters:

  • user (StatsigUser)

    A StatsigUser object used for the evaluation

  • gate_name (String)

    The name of the gate being checked

  • options (CheckGateOptions) (defaults to: nil)

    Additional options for evaluating the gate

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/statsig.rb', line 68

def self.check_gate(user, gate_name, options = nil)
  ensure_initialized
  @shared_instance&.check_gate(user, gate_name, options)
end

.check_gate_with_exposure_logging_disabled(user, gate_name) ⇒ Object

Deprecated.
  • use check_gate(user, gate, options) with CheckGateOptions.new(disable_log_exposure: true) as options

Gets the boolean result of a gate, evaluated against the given user.

Parameters:

  • user

    A StatsigUser object used for the evaluation

  • gate_name

    The name of the gate being checked



79
80
81
82
# File 'lib/statsig.rb', line 79

def self.check_gate_with_exposure_logging_disabled(user, gate_name)
  ensure_initialized
  @shared_instance&.check_gate(user, gate_name, CheckGateOptions.new(disable_log_exposure: true))
end

.clear_config_overridesObject



366
367
368
369
# File 'lib/statsig.rb', line 366

def self.clear_config_overrides
  ensure_initialized
  @shared_instance&.clear_config_overrides
end

.clear_experiment_overridesObject



371
372
373
374
# File 'lib/statsig.rb', line 371

def self.clear_experiment_overrides
  ensure_initialized
  @shared_instance&.clear_experiment_overrides
end

.clear_gate_overridesObject



335
336
337
338
# File 'lib/statsig.rb', line 335

def self.clear_gate_overrides
  ensure_initialized
  @shared_instance&.clear_gate_overrides
end

.ensure_initializedObject



424
425
426
427
428
# File 'lib/statsig.rb', line 424

def self.ensure_initialized
  if not defined? @shared_instance or @shared_instance.nil?
    raise Statsig::UninitializedError.new
  end
end

.get_client_initialize_response(user, hash: 'sha256', client_sdk_key: nil, include_local_overrides: false) ⇒ Object

Note:

See Ruby Documentation: docs.statsig.com/server/rubySDK)

Gets all evaluated values for the given user. These values can then be given to a Statsig Client SDK via bootstrapping.

Parameters:

  • user

    A StatsigUser object used for the evaluation

  • hash (defaults to: 'sha256')

    The type of hashing algorithm to use (‘sha256’, ‘djb2’, ‘none’)

  • client_sdk_key (defaults to: nil)

    A optional client sdk key to be used for the evaluation

  • include_local_overrides (defaults to: false)

    Option to include local overrides



398
399
400
401
402
403
404
405
406
# File 'lib/statsig.rb', line 398

def self.get_client_initialize_response(
  user,
  hash: 'sha256',
  client_sdk_key: nil,
  include_local_overrides: false
)
  ensure_initialized
  @shared_instance&.get_client_initialize_response(user, hash, client_sdk_key, include_local_overrides)
end

.get_config(user, dynamic_config_name, options = nil) ⇒ DynamicConfig

Get the values of a dynamic config, evaluated against the given user. An exposure event will automatically be logged for the dynamic config.

Parameters:

  • user (StatsigUser)

    A StatsigUser object used for the evaluation

  • dynamic_config_name (String)

    The name of the dynamic config

  • options (GetConfigOptions) (defaults to: nil)

    Additional options for evaluating the config

Returns:



116
117
118
119
# File 'lib/statsig.rb', line 116

def self.get_config(user, dynamic_config_name, options = nil)
  ensure_initialized
  @shared_instance&.get_config(user, dynamic_config_name, options)
end

.get_config_with_exposure_logging_disabled(user, dynamic_config_name) ⇒ DynamicConfig

Deprecated.
  • use get_config(user, config, options) with GetConfigOptions.new(disable_log_exposure: true) as options

Get the values of a dynamic config, evaluated against the given user.

Parameters:

  • user (StatsigUser)

    A StatsigUser object used for the evaluation

  • dynamic_config_name (String)

    The name of the dynamic config

Returns:



128
129
130
131
# File 'lib/statsig.rb', line 128

def self.get_config_with_exposure_logging_disabled(user, dynamic_config_name)
  ensure_initialized
  @shared_instance&.get_config(user, dynamic_config_name, GetConfigOptions.new(disable_log_exposure: true))
end

.get_experiment(user, experiment_name, options = nil) ⇒ Object

Get the values of an experiment, evaluated against the given user. An exposure event will automatically be logged for the experiment.

Parameters:

  • user (StatsigUser)

    A StatsigUser object used for the evaluation

  • experiment_name (String)

    The name of the experiment

  • options (GetExperimentOptions) (defaults to: nil)

    Additional options for evaluating the experiment



165
166
167
168
# File 'lib/statsig.rb', line 165

def self.get_experiment(user, experiment_name, options = nil)
  ensure_initialized
  @shared_instance&.get_experiment(user, experiment_name, options)
end

.get_experiment_with_exposure_logging_disabled(user, experiment_name) ⇒ Object

Deprecated.
  • use get_experiment(user, experiment, options) with GetExperimentOptions.new(disable_log_exposure: true) as options

Get the values of an experiment, evaluated against the given user.

Parameters:

  • user (StatsigUser)

    A StatsigUser object used for the evaluation

  • experiment_name (String)

    The name of the experiment



176
177
178
179
# File 'lib/statsig.rb', line 176

def self.get_experiment_with_exposure_logging_disabled(user, experiment_name)
  ensure_initialized
  @shared_instance&.get_experiment(user, experiment_name, GetExperimentOptions.new(disable_log_exposure: true))
end

.get_fields_used_for_config(config_name) ⇒ Object



143
144
145
146
# File 'lib/statsig.rb', line 143

def self.get_fields_used_for_config(config_name)
  ensure_initialized
  @shared_instance&.get_fields_used_for_config(config_name)
end

.get_fields_used_for_experiment(experiment_name) ⇒ Object



196
197
198
199
# File 'lib/statsig.rb', line 196

def self.get_fields_used_for_experiment(experiment_name)
  ensure_initialized
  @shared_instance&.get_fields_used_for_config(experiment_name)
end

.get_fields_used_for_gate(gate_name) ⇒ Object



94
95
96
97
# File 'lib/statsig.rb', line 94

def self.get_fields_used_for_gate(gate_name)
  ensure_initialized
  @shared_instance&.get_fields_used_for_gate(gate_name)
end

.get_fields_used_for_layer(layer_name) ⇒ Object



244
245
246
247
# File 'lib/statsig.rb', line 244

def self.get_fields_used_for_layer(layer_name)
  ensure_initialized
  @shared_instance&.get_fields_used_for_layer(layer_name)
end

.get_gate(user, gate_name, options) ⇒ FeatureGate

Gets the gate, evaluated against the given user. An exposure event will automatically be logged for the gate.

Parameters:

  • user (StatsigUser)

    A StatsigUser object used for the evaluation

  • gate_name (String)

    The name of the gate being checked

  • options (GetGateOptions)

    Additional options for evaluating the gate

Returns:



46
47
48
49
# File 'lib/statsig.rb', line 46

def self.get_gate(user, gate_name, options)
  ensure_initialized
  @shared_instance&.get_gate(user, gate_name, options)
end

.get_initialization_detailsObject



22
23
24
25
26
27
# File 'lib/statsig.rb', line 22

def self.get_initialization_details
  if not defined? @shared_instance or @shared_instance.nil?
    return {duration: 0, isSDKReady: false, configSpecReady: false, failure_details: {exception: Statsig::UninitializedError.new, reason: 'INTERNAL_ERROR'}}
  end
  @shared_instance.get_initialization_details
end

.get_layer(user, layer_name, options = nil) ⇒ Object

Get the values of a layer, evaluated against the given user. Exposure events will be fired when get or get_typed is called on the resulting Layer class.

Parameters:

  • user (StatsigUser)

    A StatsigUser object used for the evaluation

  • layer_name (String)

    The name of the layer

  • options (GetLayerOptions) (defaults to: nil)

    Configuration of how this method call should behave



217
218
219
220
# File 'lib/statsig.rb', line 217

def self.get_layer(user, layer_name, options = nil)
  ensure_initialized
  @shared_instance&.get_layer(user, layer_name, options)
end

.get_layer_with_exposure_logging_disabled(user, layer_name) ⇒ Object

Deprecated.
  • use get_layer(user, gate, options) with GetLayerOptions.new(disable_log_exposure: true) as options

Get the values of a layer, evaluated against the given user.

Parameters:

  • user

    A StatsigUser object used for the evaluation

  • layer_name

    The name of the layer



228
229
230
231
# File 'lib/statsig.rb', line 228

def self.get_layer_with_exposure_logging_disabled(user, layer_name)
  ensure_initialized
  @shared_instance&.get_layer(user, layer_name, GetLayerOptions.new(disable_log_exposure: true))
end

.get_optionsObject



418
419
420
# File 'lib/statsig.rb', line 418

def self.get_options
  @driver&.instance_variable_get(:@options)
end

.get_statsig_metadataObject

Internal Statsig metadata for this SDK



410
411
412
413
414
415
416
# File 'lib/statsig.rb', line 410

def self.
  {
    'sdkType' => 'ruby-server',
    'sdkVersion' => '2.8.2',
    'languageVersion' => RUBY_VERSION
  }
end

.get_user_persisted_values(user, id_type) ⇒ Object



191
192
193
194
# File 'lib/statsig.rb', line 191

def self.get_user_persisted_values(user, id_type)
  ensure_initialized
  @shared_instance&.get_user_persisted_values(user, id_type)
end

.initialize(secret_key, options = nil, error_callback = nil) ⇒ Object

Initializes the Statsig SDK.

Parameters:

  • secret_key

    The server SDK key copied from console.statsig.com

  • options (defaults to: nil)

    The StatsigOptions object used to configure the SDK

  • error_callback (defaults to: nil)

    A callback function, called if the initialize network call fails



12
13
14
15
16
17
18
19
20
# File 'lib/statsig.rb', line 12

def self.initialize(secret_key, options = nil, error_callback = nil)
  unless @shared_instance.nil?
    puts 'Statsig already initialized.'
    @shared_instance.maybe_restart_background_threads
    return @shared_instance
  end

  @shared_instance = StatsigDriver.new(secret_key, options, error_callback)
end

.list_autotunesObject

Returns a list of all autotune names



298
299
300
301
# File 'lib/statsig.rb', line 298

def self.list_autotunes
  ensure_initialized
  @shared_instance&.list_autotunes
end

.list_configsObject

Returns a list of all config names



282
283
284
285
# File 'lib/statsig.rb', line 282

def self.list_configs
  ensure_initialized
  @shared_instance&.list_configs
end

.list_experimentsObject

Returns a list of all experiment names



290
291
292
293
# File 'lib/statsig.rb', line 290

def self.list_experiments
  ensure_initialized
  @shared_instance&.list_experiments
end

.list_gatesObject

Returns a list of all gate names



274
275
276
277
# File 'lib/statsig.rb', line 274

def self.list_gates
  ensure_initialized
  @shared_instance&.list_gates
end

.list_layersObject

Returns a list of all layer names



306
307
308
309
# File 'lib/statsig.rb', line 306

def self.list_layers
  ensure_initialized
  @shared_instance&.list_layers
end

.log_event(user, event_name, value = nil, metadata = nil) ⇒ Object

Logs an event to Statsig with the provided values.

Parameters:

  • user

    A StatsigUser object to be included in the log

  • event_name

    The name given to the event

  • value (defaults to: nil)

    A top level value for the event

  • metadata (defaults to: nil)

    Any extra values to be logged



256
257
258
259
# File 'lib/statsig.rb', line 256

def self.log_event(user, event_name, value = nil,  = nil)
  ensure_initialized
  @shared_instance&.log_event(user, event_name, value, )
end

.manually_log_config_exposure(user, dynamic_config) ⇒ Object

Logs an exposure event for the dynamic config

Parameters:

  • user

    A StatsigUser object used for the evaluation

  • dynamic_config_name

    The name of the dynamic config



138
139
140
141
# File 'lib/statsig.rb', line 138

def self.manually_log_config_exposure(user, dynamic_config)
  ensure_initialized
  @shared_instance&.manually_log_config_exposure(user, dynamic_config)
end

.manually_log_experiment_exposure(user, experiment_name) ⇒ Object

Logs an exposure event for the experiment

Parameters:

  • user

    A StatsigUser object used for the evaluation

  • experiment_name

    The name of the experiment



186
187
188
189
# File 'lib/statsig.rb', line 186

def self.manually_log_experiment_exposure(user, experiment_name)
  ensure_initialized
  @shared_instance&.manually_log_config_exposure(user, experiment_name)
end

.manually_log_gate_exposure(user, gate_name) ⇒ Object

Logs an exposure event for the gate

Parameters:

  • user

    A StatsigUser object used for the evaluation

  • gate_name

    The name of the gate being checked



89
90
91
92
# File 'lib/statsig.rb', line 89

def self.manually_log_gate_exposure(user, gate_name)
  ensure_initialized
  @shared_instance&.manually_log_gate_exposure(user, gate_name)
end

.manually_log_layer_parameter_exposure(user, layer_name, parameter_name) ⇒ Object

Logs an exposure event for the parameter in the given layer

Parameters:

  • user

    A StatsigUser object used for the evaluation

  • layer_name

    The name of the layer

  • parameter_name

    The name of the parameter in the layer



239
240
241
242
# File 'lib/statsig.rb', line 239

def self.manually_log_layer_parameter_exposure(user, layer_name, parameter_name)
  ensure_initialized
  @shared_instance&.manually_log_layer_parameter_exposure(user, layer_name, parameter_name)
end

.override_config(config_name, config_value) ⇒ Object

Sets a value to be returned for the given dynamic config/experiment instead of the actual evaluated value.

Parameters:

  • config_name

    The name of the dynamic config or experiment to be overridden

  • config_value

    The value that will be returned



345
346
347
348
# File 'lib/statsig.rb', line 345

def self.override_config(config_name, config_value)
  ensure_initialized
  @shared_instance&.override_config(config_name, config_value)
end

.override_experiment_by_group_name(experiment_name, group_name) ⇒ Object

Overrides an experiment to return the value for a specific group name.

Parameters:

  • experiment_name

    The name of the experiment to be overridden

  • group_name

    The name of the group whose value should be returned



356
357
358
359
# File 'lib/statsig.rb', line 356

def self.override_experiment_by_group_name(experiment_name, group_name)
  ensure_initialized
  @shared_instance&.override_experiment_by_group_name(experiment_name, group_name)
end

.override_gate(gate_name, gate_value) ⇒ Object

Sets a value to be returned for the given gate instead of the actual evaluated value.

Parameters:

  • gate_name

    The name of the gate to be overridden

  • gate_value

    The value that will be returned



325
326
327
328
# File 'lib/statsig.rb', line 325

def self.override_gate(gate_name, gate_value)
  ensure_initialized
  @shared_instance&.override_gate(gate_name, gate_value)
end

.remove_config_override(config_name) ⇒ Object



361
362
363
364
# File 'lib/statsig.rb', line 361

def self.remove_config_override(config_name)
  ensure_initialized
  @shared_instance&.remove_config_override(config_name)
end

.remove_experiment_override(experiment_name) ⇒ Object



376
377
378
379
# File 'lib/statsig.rb', line 376

def self.remove_experiment_override(experiment_name)
  ensure_initialized
  @shared_instance&.remove_experiment_override(experiment_name)
end

.remove_gate_override(gate_name) ⇒ Object



330
331
332
333
# File 'lib/statsig.rb', line 330

def self.remove_gate_override(gate_name)
  ensure_initialized
  @shared_instance&.remove_gate_override(gate_name)
end

.set_debug_info(debug_info) ⇒ Object

Parameters:

  • debug (HashTable)

    information log with exposure events



383
384
385
386
# File 'lib/statsig.rb', line 383

def self.set_debug_info(debug_info)
  ensure_initialized
  @shared_instance&.set_debug_info(debug_info)
end

.shutdownObject

Stops all Statsig activity and flushes any pending events.



313
314
315
316
317
318
# File 'lib/statsig.rb', line 313

def self.shutdown
  if defined? @shared_instance and !@shared_instance.nil?
    @shared_instance.shutdown
  end
  @shared_instance = nil
end

.sync_idlistsObject



266
267
268
269
# File 'lib/statsig.rb', line 266

def self.sync_idlists
  ensure_initialized
  @shared_instance&.manually_sync_idlists
end

.sync_rulesetsObject



261
262
263
264
# File 'lib/statsig.rb', line 261

def self.sync_rulesets
  ensure_initialized
  @shared_instance&.manually_sync_rulesets
end