Class: PostHog::Client

Inherits:
Object
  • Object
show all
Includes:
Logging, Utils
Defined in:
lib/posthog/client.rb

Constant Summary

Constants included from Utils

Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON

Instance Method Summary collapse

Methods included from Logging

included, #logger

Methods included from Utils

#date_in_iso8601, #datetime_in_iso8601, #formatted_offset, #isoify_dates, #isoify_dates!, #seconds_to_utc_offset, #stringify_keys, #symbolize_keys, #symbolize_keys!, #time_in_iso8601, #uid

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :api_key (String)

    Your project’s api_key

  • :max_queue_size (FixNum)

    Maximum number of calls to be remain queued.

  • :on_error (Proc)

    Handles error calls from the API.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/posthog/client.rb', line 20

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

Instance Method Details

#alias(attrs) ⇒ Object

Aliases a user from one id to another

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :alias (String)

    The alias to give the distinct id

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :distinct_id (String)

    The ID for this user in your database



94
95
96
97
# File 'lib/posthog/client.rb', line 94

def alias(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_alias(attrs))
end

#capture(attrs) ⇒ Object

Captures an event

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :event (String)

    Event name

  • :properties (Hash)

    Event properties (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :distinct_id (String)

    The ID for this user in your database



72
73
74
75
# File 'lib/posthog/client.rb', line 72

def capture(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_capture(attrs))
end

#flushObject

Synchronously waits until the worker has flushed the queue.

Use only for scripts which are not long-running, and will specifically exit



52
53
54
55
56
57
# File 'lib/posthog/client.rb', line 52

def flush
  while !@queue.empty? || @worker.is_requesting?
    ensure_worker_running
    sleep(0.1)
  end
end

#identify(attrs) ⇒ Object

Identifies a user

Parameters:

  • attrs (Hash)

Options Hash (attrs):

  • :properties (Hash)

    User properties (optional)

  • :message_id (String)

    ID that uniquely identifies a message across the API. (optional)

  • :timestamp (Time)

    When the event occurred (optional)

  • :distinct_id (String)

    The ID for this user in your database



83
84
85
86
# File 'lib/posthog/client.rb', line 83

def identify(attrs)
  symbolize_keys! attrs
  enqueue(FieldParser.parse_for_identify(attrs))
end

#is_feature_enabled(flag_key, distinct_id, default_value = false) ⇒ Object



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
# File 'lib/posthog/client.rb', line 104

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

#queued_messagesFixnum

Returns number of messages in the queue.

Returns:

  • (Fixnum)

    number of messages in the queue



100
101
102
# File 'lib/posthog/client.rb', line 100

def queued_messages
  @queue.length
end

#reload_feature_flagsObject



130
131
132
133
134
135
136
137
138
# File 'lib/posthog/client.rb', line 130

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

#shutdownObject



140
141
142
143
# File 'lib/posthog/client.rb', line 140

def shutdown
  @feature_flags_poller.shutdown_poller
  flush
end