Class: StatsigUser

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/statsig_user.rb

Overview

The user object to be evaluated against your Statsig configurations (gates/experiments/dynamic configs).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_hash) ⇒ StatsigUser

Returns a new instance of StatsigUser.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/statsig_user.rb', line 65

def initialize(user_hash)
  the_hash = user_hash
  begin
    the_hash = JSON.parse(user_hash&.to_json || "")
  rescue
    puts 'Failed to clone user hash'
  end

  @user_id = from_hash(the_hash, [:user_id, :userID], String)
  @email = from_hash(the_hash, [:email], String)
  @ip = from_hash(the_hash, [:ip], String)
  @user_agent = from_hash(the_hash, [:user_agent, :userAgent], String)
  @country = from_hash(the_hash, [:country], String)
  @locale = from_hash(the_hash, [:locale], String)
  @app_version = from_hash(the_hash, [:app_version, :appVersion], String)
  @custom = from_hash(the_hash, [:custom], Hash)
  @private_attributes = from_hash(the_hash, [:private_attributes, :privateAttributes], Hash)
  @custom_ids = from_hash(the_hash, [:custom_ids, :customIDs], Hash)
  @statsig_environment = from_hash(the_hash, [:statsig_environment, :statsigEnvironment], Hash)
end

Instance Attribute Details

#app_versionObject

The current app version the user is interacting with. Evaluated against the App Version criteria. (docs.statsig.com/feature-gates/conditions#app-version)



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

def app_version
  @app_version
end

#countryObject

The country code associated with this user (e.g New Zealand => NZ). Evaluated against the Country criteria. (docs.statsig.com/feature-gates/conditions#country)



29
30
31
# File 'lib/statsig_user.rb', line 29

def country
  @country
end

#custom_idsObject

Any Custom IDs to associated with the user. (See docs.statsig.com/guides/experiment-on-custom-id-types)



45
46
47
# File 'lib/statsig_user.rb', line 45

def custom_ids
  @custom_ids
end

#emailObject

An identifier for this user. Evaluated against the Email criteria. (docs.statsig.com/feature-gates/conditions#email)



17
18
19
# File 'lib/statsig_user.rb', line 17

def email
  @email
end

#ipObject

An IP address associated with this user. Evaluated against the IP Address criteria. (docs.statsig.com/feature-gates/conditions#ip)



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

def ip
  @ip
end

#localeObject

An locale for this user.



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

def locale
  @locale
end

#private_attributesObject

Any value you wish to use in evaluation, but do not want logged with events, can be stored in this field.



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

def private_attributes
  @private_attributes
end

#statsig_environmentObject

A Hash you can use to set environment variables that apply to this user. e.g. { “tier” => “development” }



41
42
43
# File 'lib/statsig_user.rb', line 41

def statsig_environment
  @statsig_environment
end

#user_agentObject

A user agent string associated with this user. Evaluated against Browser Version and Name (docs.statsig.com/feature-gates/conditions#browser-version)



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

def user_agent
  @user_agent
end

#user_idObject

An identifier for this user. Evaluated against the User ID criteria. (docs.statsig.com/feature-gates/conditions#userid)



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

def user_id
  @user_id
end

Instance Method Details

#customObject



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

def custom
  @custom
end

#custom=(value) ⇒ Object

Any custom fields for this user. Evaluated against the Custom criteria. (docs.statsig.com/feature-gates/conditions#custom)



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

def custom=(value)
  @custom = value.is_a?(Hash) ? value : Hash.new
end

#serialize(for_logging) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/statsig_user.rb', line 86

def serialize(for_logging)
  hash = {
    'userID' => @user_id,
    'email' => @email,
    'ip' => @ip,
    'userAgent' => @user_agent,
    'country' => @country,
    'locale' => @locale,
    'appVersion' => @app_version,
    'custom' => @custom,
    'statsigEnvironment' => @statsig_environment,
    'privateAttributes' => @private_attributes,
    'customIDs' => @custom_ids,
  }
  if for_logging
    hash.delete('privateAttributes')
  end
  hash.compact
end

#value_lookupObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/statsig_user.rb', line 106

def value_lookup
  {
    'userID' => @user_id,
    'userid' => @user_id,
    'user_id' => @user_id,
    'email' => @email,
    'ip' => @ip,
    'userAgent' => @user_agent,
    'useragent' => @user_agent,
    'user_agent' => @user_agent,
    'country' => @country,
    'locale' => @locale,
    'appVersion' => @app_version,
    'appversion' => @app_version,
    'app_version' => @app_version,
    'custom' => @custom,
    'privateAttributes' => @private_attributes,
  }
end