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

#to_hash_without_stable_idObject



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
146
147
# File 'lib/statsig_user.rb', line 106

def to_hash_without_stable_id()
  hash = {}
  if @user_id != nil
    hash['userID'] = @user_id
  end
  if @email != nil
    hash['email'] = @email
  end
  if @ip != nil
    hash['ip'] = @ip
  end
  if @user_agent != nil
    hash['userAgent'] = @user_agent
  end
  if @country != nil
    hash['country'] = @country
  end
  if @locale != nil
    hash['locale'] = @locale
  end
  if @app_version != nil
    hash['appVersion'] = @app_version
  end
  if @custom != nil
    hash['custom'] = Statsig::HashUtils.sortHash(@custom)
  end
  if @statsig_environment != nil
    hash['statsigEnvironment'] = @statsig_environment.clone.sort_by { |key| key }.to_h
  end
  if @private_attributes != nil
    hash['privateAttributes'] = Statsig::HashUtils.sortHash(@private_attributes)
  end
  custom_ids = {}
  if @custom_ids != nil
    custom_ids = @custom_ids.clone
    if custom_ids.key?("stableID")
      custom_ids.delete("stableID")
    end
  end
  hash['customIDs'] = custom_ids.sort_by { |key| key }.to_h
  return Statsig::HashUtils.djb2ForHash(hash.sort_by { |key| key }.to_h)
end

#value_lookupObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/statsig_user.rb', line 149

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