class Rudder::Analytics::Client
Public Class Methods
@param [Hash] opts @option opts [String] :write_key Your project's write_key @option opts [String] :data_plane_url Your data plane URL @option opts [FixNum] :max_queue_size Maximum number of calls to be
remain queued.
@option opts [Proc] :on_error Handles error calls from the API.
# File lib/rudder/analytics/client.rb, line 24 def initialize(opts = {}) symbolize_keys!(opts) @queue = Queue.new @write_key = opts[:write_key] @data_plane_url = opts[:data_plane_url] @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE @worker_mutex = Mutex.new @worker = Worker.new(@queue, @data_plane_url, @write_key, opts) @worker_thread = nil uri = URI(opts[:data_plane_url]) @host = uri.host @port = uri.port check_write_key! at_exit { @worker_thread && @worker_thread[:should_exit] = true } end
Public Instance Methods
Aliases a user from one id to another
@see segment.com/docs/sources/server/ruby/#alias
@param [Hash] attrs
@option attrs [String] :previous_id The ID to alias from @macro common_attrs
# File lib/rudder/analytics/client.rb, line 106 def alias(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_alias(attrs)) end
Synchronously waits until the worker has flushed the queue.
Use only for scripts which are not long-running, and will specifically exit
# File lib/rudder/analytics/client.rb, line 49 def flush while !@queue.empty? || @worker.is_requesting? ensure_worker_running sleep(0.1) end end
Associates a user identity with a group.
@see segment.com/docs/sources/server/ruby/#group
@param [Hash] attrs
@option attrs [String] :group_id The ID of the group @option attrs [Hash] :traits User traits (optional) @macro common_attrs
# File lib/rudder/analytics/client.rb, line 120 def group(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_group(attrs)) end
Identifies a user
@see segment.com/docs/sources/server/ruby/#identify
@param [Hash] attrs
@option attrs [Hash] :traits User traits (optional) @macro common_attrs
# File lib/rudder/analytics/client.rb, line 92 def identify(attrs) printf("\nInside Identifyu \n") symbolize_keys! attrs enqueue(FieldParser.parse_for_identify(attrs)) end
Records a page view
@see segment.com/docs/sources/server/ruby/#page
@param [Hash] attrs
@option attrs [String] :name Name of the page @option attrs [Hash] :properties Page properties (optional) @macro common_attrs
# File lib/rudder/analytics/client.rb, line 134 def page(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_page(attrs)) end
@return [Fixnum] number of messages in the queue
# File lib/rudder/analytics/client.rb, line 153 def queued_messages @queue.length end
Records a screen view (for a mobile app)
@param [Hash] attrs
@option attrs [String] :name Name of the screen @option attrs [Hash] :properties Screen properties (optional) @option attrs [String] :category The screen category (optional) @macro common_attrs
# File lib/rudder/analytics/client.rb, line 147 def screen(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_screen(attrs)) end
Tracks an event
@see segment.com/docs/sources/server/ruby/#track
@param [Hash] attrs
@option attrs [String] :event Event name @option attrs [Hash] :properties Event properties (optional) @macro common_attrs
# File lib/rudder/analytics/client.rb, line 79 def track(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_track(attrs)) end
Private Instance Methods
private: Checks that the write_key is properly initialized
# File lib/rudder/analytics/client.rb, line 183 def check_write_key! raise ArgumentError, 'Write key must be initialized' if @write_key.nil? end
private: Enqueues the action.
returns Boolean of whether the item was added to the queue.
# File lib/rudder/analytics/client.rb, line 162 def enqueue(action) puts action # add our request id for tracing purposes action[:messageId] ||= uid if @queue.length < @max_queue_size @queue << action ensure_worker_running true else logger.warn( 'Queue is full, dropping events. The :max_queue_size ' \ 'configuration parameter can be increased to prevent this from ' \ 'happening.' ) false end end
# File lib/rudder/analytics/client.rb, line 187 def ensure_worker_running return if worker_running? @worker_mutex.synchronize do return if worker_running? @worker_thread = Thread.new do @worker.run end end end
# File lib/rudder/analytics/client.rb, line 197 def worker_running? @worker_thread && @worker_thread.alive? end