Class: SessionVision::Client
- Inherits:
-
Object
- Object
- SessionVision::Client
- Defined in:
- lib/sessionvision/client.rb
Constant Summary collapse
- MAX_GROUPS =
8- MAX_GROUP_KEY_LENGTH =
200- CONTROL_CHARS =
/[\x00-\x1f\x7f]/
Instance Method Summary collapse
- #capture(event_name, user_id: nil, anonymous_id: nil, session_id: nil, properties: {}, groups: nil) ⇒ Object
- #flush ⇒ Object
- #identify(user_id, groups: nil, **traits) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #register(**properties) ⇒ Object
- #register_once(**properties) ⇒ Object
- #reset ⇒ Object
- #set_groups(groups) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sessionvision/client.rb', line 12 def initialize(config) @config = config @lock = Mutex.new @registered = {} @groups = [] @shutdown_called = false @logger = config.debug ? Logger.new($stdout, progname: "sessionvision") : nil @transport = Transport.new( ingest_host: config.ingest_host, max_retries: config.max_retries, retry_delays: config.retry_delays, gzip_threshold: config.gzip_threshold, on_error: config.on_error, logger: @logger ) @buffer = EventBuffer.new( flush_size: config.flush_size, flush_interval: config.flush_interval, send_fn: method(:send_batch), logger: @logger ) @buffer.start at_exit { shutdown } @logger&.debug("Client initialized (host=#{config.ingest_host})") end |
Instance Method Details
#capture(event_name, user_id: nil, anonymous_id: nil, session_id: nil, properties: {}, groups: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sessionvision/client.rb', line 43 def capture(event_name, user_id: nil, anonymous_id: nil, session_id: nil, properties: {}, groups: nil) raise ArgumentError, "event_name must be a non-empty string" unless event_name.is_a?(String) && !event_name.empty? raise ArgumentError, "Either user_id or anonymous_id is required" if user_id.nil? && anonymous_id.nil? merged_props, merged_groups = @lock.synchronize do [@registered.merge(properties || {}), merge_groups(groups)] end event = { "event" => event_name, "timestamp" => (Time.now.to_f * 1000).to_i, "properties" => merged_props, "sessionId" => session_id || SecureRandom.uuid } event["userId"] = user_id if user_id event["anonymousId"] = anonymous_id if anonymous_id event["groups"] = merged_groups unless merged_groups.empty? @logger&.debug("Event captured: #{event_name}") @buffer.push(event) end |
#flush ⇒ Object
93 94 95 |
# File 'lib/sessionvision/client.rb', line 93 def flush @buffer.flush end |
#identify(user_id, groups: nil, **traits) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/sessionvision/client.rb', line 65 def identify(user_id, groups: nil, **traits) raise ArgumentError, "user_id must be a non-empty string" unless user_id.is_a?(String) && !user_id.empty? set_groups(groups) unless groups.nil? capture("$identify", user_id: user_id, properties: traits.transform_keys(&:to_s), groups: groups) end |
#register(**properties) ⇒ Object
78 79 80 81 82 |
# File 'lib/sessionvision/client.rb', line 78 def register(**properties) @lock.synchronize do properties.each { |k, v| @registered[k.to_s] = v } end end |
#register_once(**properties) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/sessionvision/client.rb', line 84 def register_once(**properties) @lock.synchronize do properties.each do |k, v| key = k.to_s @registered[key] = v unless @registered.key?(key) end end end |
#reset ⇒ Object
108 109 110 |
# File 'lib/sessionvision/client.rb', line 108 def reset @lock.synchronize { @registered.clear } end |
#set_groups(groups) ⇒ Object
72 73 74 75 76 |
# File 'lib/sessionvision/client.rb', line 72 def set_groups(groups) @lock.synchronize do @groups = normalize_groups(groups) end end |
#shutdown ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sessionvision/client.rb', line 97 def shutdown @lock.synchronize do return if @shutdown_called @shutdown_called = true end @logger&.debug("Shutting down") @buffer.shutdown end |