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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/statsig_user.rb', line 47

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)



27
28
29
# File 'lib/statsig_user.rb', line 27

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)



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

def country
  @country
end

#custom_idsObject

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



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

def custom_ids
  @custom_ids
end

#emailObject

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



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

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)



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

def ip
  @ip
end

#localeObject

An locale for this user.



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

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.



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

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



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

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)



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

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)



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

def user_id
  @user_id
end

Instance Method Details

#customObject



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

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)



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

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

#get_unit_id(id_type) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/statsig_user.rb', line 132

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



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

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
# File 'lib/statsig_user.rb', line 88

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