class PostHog::Client
Public Class Methods
@param [Hash] opts @option opts [String] :api_key Your project's api_key @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/posthog/client.rb, line 21 def initialize(opts = {}) symbolize_keys!(opts) @queue = Queue.new @api_key = opts[:api_key] @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE @worker_mutex = Mutex.new @worker = Worker.new(@queue, @api_key, opts) @worker_thread = nil @feature_flags_poller = nil @personal_api_key = nil check_api_key! if opts[:personal_api_key].present? @personal_api_key = opts[:personal_api_key] @feature_flags_poller = FeatureFlagsPoller.new(opts[:feature_flags_polling_interval], opts[:personal_api_key], @api_key, opts[:host]) end at_exit { @worker_thread && @worker_thread[:should_exit] = true } end
Public Instance Methods
Aliases a user from one id to another
@param [Hash] attrs
@option attrs [String] :alias The alias to give the distinct id @macro common_attrs
# File lib/posthog/client.rb, line 91 def alias(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_alias(attrs)) end
Captures an event
@param [Hash] attrs
@option attrs [String] :event Event name @option attrs [Hash] :properties Event properties (optional) @macro common_attrs
# File lib/posthog/client.rb, line 69 def capture(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_capture(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/posthog/client.rb, line 49 def flush while !@queue.empty? || @worker.is_requesting? ensure_worker_running sleep(0.1) end end
Identifies a user
@param [Hash] attrs
@option attrs [Hash] :properties User properties (optional) @macro common_attrs
# File lib/posthog/client.rb, line 80 def identify(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_identify(attrs)) end
# File lib/posthog/client.rb, line 101 def is_feature_enabled(flag_key, distinct_id, default_value=false) unless @personal_api_key logger.error('You need to specify a personal_api_key to use feature flags') return end is_enabled = @feature_flags_poller.is_feature_enabled(flag_key, distinct_id, default_value) capture({ 'distinct_id': distinct_id, 'event': '$feature_flag_called', 'properties': { '$feature_flag': flag_key, '$feature_flag_response': is_enabled } }) return is_enabled end
@return [Fixnum] number of messages in the queue
# File lib/posthog/client.rb, line 97 def queued_messages @queue.length end
# File lib/posthog/client.rb, line 118 def reload_feature_flags unless @personal_api_key logger.error('You need to specify a personal_api_key to use feature flags') return end @feature_flags_poller.load_feature_flags(true) end
# File lib/posthog/client.rb, line 126 def shutdown @feature_flags_poller.shutdown_poller flush end
Private Instance Methods
private: Checks that the api_key is properly initialized
# File lib/posthog/client.rb, line 156 def check_api_key! raise ArgumentError, 'API key must be initialized' if @api_key.nil? end
private: Enqueues the action.
returns Boolean of whether the item was added to the queue.
# File lib/posthog/client.rb, line 136 def enqueue(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/posthog/client.rb, line 160 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/posthog/client.rb, line 170 def worker_running? @worker_thread && @worker_thread.alive? end