Class: StatsigUser

Inherits:
Object
  • Object
show all
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.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/statsig_user.rb', line 89

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)
  @memo = {}
  @dirty = true
  @memo_timeout = 2
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)



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

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)



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

def country
  @country
end

#custom_idsObject

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



64
65
66
# File 'lib/statsig_user.rb', line 64

def custom_ids
  @custom_ids
end

#emailObject

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



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

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)



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

def ip
  @ip
end

#localeObject

An locale for this user.



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

def locale
  @locale
end

#memo_timeoutObject

Returns the value of attribute memo_timeout.



87
88
89
# File 'lib/statsig_user.rb', line 87

def memo_timeout
  @memo_timeout
end

#private_attributesObject

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



71
72
73
# File 'lib/statsig_user.rb', line 71

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” }



57
58
59
# File 'lib/statsig_user.rb', line 57

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)



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

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)



8
9
10
# File 'lib/statsig_user.rb', line 8

def user_id
  @user_id
end

Instance Method Details

#clear_memoObject



200
201
202
203
204
# File 'lib/statsig_user.rb', line 200

def clear_memo
  @memo.clear
  @dirty = false
  @memo_access_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#customObject



77
78
79
# File 'lib/statsig_user.rb', line 77

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)



82
83
84
85
# File 'lib/statsig_user.rb', line 82

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

#get_memoObject



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/statsig_user.rb', line 186

def get_memo
  current_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  if @dirty || current_time - (@memo_access_time ||= current_time) > @memo_timeout
    if @memo.size() > 0
      @memo.clear
    end
    @dirty = false
    @memo_access_time = current_time
  end

  @memo
end

#get_unit_id(id_type) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/statsig_user.rb', line 177

def get_unit_id(id_type)
  if id_type.is_a?(String) && id_type != Statsig::Const::CML_USER_ID
    return nil unless @custom_ids.is_a? Hash

    return @custom_ids[id_type] || @custom_ids[id_type.downcase]
  end
  @user_id
end

#serialize(for_logging) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/statsig_user.rb', line 113

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/statsig_user.rb', line 133

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

#user_keyObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/statsig_user.rb', line 206

def user_key
  unless !@dirty && defined? @_user_key
    custom_id_key = ''
    if self.custom_ids.is_a?(Hash)
      custom_id_key = self.custom_ids.values.join(',')
    end
    user_id_key = ''
    unless self.user_id.nil?
      user_id_key = self.user_id.to_s
    end
    @_user_key = user_id_key + ',' + custom_id_key.to_s
  end
  @_user_key
end