Class: PostHog::Client
- Inherits:
-
Object
- Object
- PostHog::Client
- 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
-
#alias(attrs) ⇒ Object
Aliases a user from one id to another.
-
#capture(attrs) ⇒ Object
Captures an event.
-
#clear ⇒ Object
Clears the queue without waiting.
-
#dequeue_last_message ⇒ Hash
Pops the last message from the queue.
-
#flush ⇒ Object
Synchronously waits until the worker has cleared the queue.
-
#get_all_flags(distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) ⇒ Hash
Returns all flags for a given user.
-
#get_all_flags_and_payloads(distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) ⇒ Object
Returns all flags and payloads for a given user.
-
#get_feature_flag(key, distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false, send_feature_flag_events: true) ⇒ String?
Returns whether the given feature flag is enabled for the given user or not.
-
#get_feature_flag_payload(key, distinct_id, match_value: nil, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) ⇒ Object
Returns payload for a given feature flag.
-
#group_identify(attrs) ⇒ Object
Identifies a group.
-
#identify(attrs) ⇒ Object
Identifies a user.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #is_feature_enabled(flag_key, distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false, send_feature_flag_events: true) ⇒ Object
-
#queued_messages ⇒ Fixnum
Number of messages in the queue.
- #reload_feature_flags ⇒ Object
- #shutdown ⇒ Object
Methods included from Logging
Methods included from Utils
#convert_to_datetime, #date_in_iso8601, #datetime_in_iso8601, #formatted_offset, #is_valid_regex, #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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/posthog/client.rb', line 27 def initialize(opts = {}) symbolize_keys!(opts) opts[:host] ||= 'https://app.posthog.com' @queue = Queue.new @api_key = opts[:api_key] @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE @worker_mutex = Mutex.new @worker = if opts[:test_mode] NoopWorker.new(@queue) else SendWorker.new(@queue, @api_key, opts) end @worker_thread = nil @feature_flags_poller = nil @personal_api_key = opts[:personal_api_key] check_api_key! @feature_flags_poller = FeatureFlagsPoller.new( opts[:feature_flags_polling_interval], opts[:personal_api_key], @api_key, opts[:host], opts[:feature_flag_request_timeout_seconds] || Defaults::FeatureFlags::FLAG_REQUEST_TIMEOUT_SECONDS, opts[:on_error] ) @distinct_id_has_sent_flag_calls = SizeLimitedHash.new(Defaults::MAX_HASH_SIZE) { |hash, key| hash[key] = Array.new } end |
Instance Method Details
#alias(attrs) ⇒ Object
Aliases a user from one id to another
135 136 137 138 |
# File 'lib/posthog/client.rb', line 135 def alias(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_alias(attrs)) end |
#capture(attrs) ⇒ Object
Captures an event
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/posthog/client.rb', line 92 def capture(attrs) symbolize_keys! attrs if attrs[:send_feature_flags] feature_variants = @feature_flags_poller.get_feature_variants(attrs[:distinct_id], attrs[:groups] || {}) attrs[:feature_variants] = feature_variants end enqueue(FieldParser.parse_for_capture(attrs)) end |
#clear ⇒ Object
Clears the queue without waiting.
Use only in test mode
74 75 76 |
# File 'lib/posthog/client.rb', line 74 def clear @queue.clear end |
#dequeue_last_message ⇒ Hash
Returns pops the last message from the queue.
141 142 143 |
# File 'lib/posthog/client.rb', line 141 def @queue.pop end |
#flush ⇒ Object
Synchronously waits until the worker has cleared the queue.
Use only for scripts which are not long-running, and will specifically exit
64 65 66 67 68 69 |
# File 'lib/posthog/client.rb', line 64 def flush while !@queue.empty? || @worker.is_requesting? ensure_worker_running sleep(0.1) end end |
#get_all_flags(distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) ⇒ Hash
Returns all flags for a given user
209 210 211 212 |
# File 'lib/posthog/client.rb', line 209 def get_all_flags(distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) person_properties, group_properties = add_local_person_and_group_properties(distinct_id, groups, person_properties, group_properties) return @feature_flags_poller.get_all_flags(distinct_id, groups, person_properties, group_properties, only_evaluate_locally) end |
#get_all_flags_and_payloads(distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) ⇒ Object
Returns all flags and payloads for a given user
237 238 239 240 |
# File 'lib/posthog/client.rb', line 237 def get_all_flags_and_payloads(distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) person_properties, group_properties = add_local_person_and_group_properties(distinct_id, groups, person_properties, group_properties) @feature_flags_poller.get_all_flags_and_payloads(distinct_id, groups, person_properties, group_properties, only_evaluate_locally) end |
#get_feature_flag(key, distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false, send_feature_flag_events: true) ⇒ String?
Returns whether the given feature flag is enabled for the given user or not
The provided properties are used to calculate feature flags locally, if possible.
‘groups` are a mapping from group type to group key. So, if you have a group type of “organization” and a group key of “5”, you would pass groups=“5”. `group_properties` take the format: { group_type_name: { group_properties } } So, for example, if you have the group type “organization” and the group key “5”, with the properties name, and employee count, you’ll send these as: “‘ruby
group_properties: {"organization": {"name": "PostHog", "employees": 11}}
“‘
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/posthog/client.rb', line 178 def get_feature_flag(key, distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false, send_feature_flag_events: true) person_properties, group_properties = add_local_person_and_group_properties(distinct_id, groups, person_properties, group_properties) feature_flag_response, flag_was_locally_evaluated = @feature_flags_poller.get_feature_flag(key, distinct_id, groups, person_properties, group_properties, only_evaluate_locally) feature_flag_reported_key = "#{key}_#{feature_flag_response}" if !@distinct_id_has_sent_flag_calls[distinct_id].include?(feature_flag_reported_key) && send_feature_flag_events capture( { 'distinct_id': distinct_id, 'event': '$feature_flag_called', 'properties': { '$feature_flag' => key, '$feature_flag_response' => feature_flag_response, 'locally_evaluated' => flag_was_locally_evaluated }, 'groups': groups, } ) @distinct_id_has_sent_flag_calls[distinct_id] << feature_flag_reported_key end feature_flag_response end |
#get_feature_flag_payload(key, distinct_id, match_value: nil, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) ⇒ Object
Returns payload for a given feature flag
224 225 226 227 |
# File 'lib/posthog/client.rb', line 224 def get_feature_flag_payload(key, distinct_id, match_value: nil, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false) person_properties, group_properties = add_local_person_and_group_properties(distinct_id, groups, person_properties, group_properties) @feature_flags_poller.get_feature_flag_payload(key, distinct_id, match_value, groups, person_properties, group_properties, only_evaluate_locally) end |
#group_identify(attrs) ⇒ Object
Identifies a group
124 125 126 127 |
# File 'lib/posthog/client.rb', line 124 def group_identify(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_group_identify(attrs)) end |
#identify(attrs) ⇒ Object
Identifies a user
110 111 112 113 |
# File 'lib/posthog/client.rb', line 110 def identify(attrs) symbolize_keys! attrs enqueue(FieldParser.parse_for_identify(attrs)) end |
#is_feature_enabled(flag_key, distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false, send_feature_flag_events: true) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/posthog/client.rb', line 150 def is_feature_enabled(flag_key, distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false, send_feature_flag_events: true) response = get_feature_flag(flag_key, distinct_id, groups: groups, person_properties: person_properties, group_properties: group_properties, only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events) if response.nil? return nil end !!response end |
#queued_messages ⇒ Fixnum
Returns number of messages in the queue.
146 147 148 |
# File 'lib/posthog/client.rb', line 146 def @queue.length end |
#reload_feature_flags ⇒ Object
242 243 244 245 246 247 248 249 250 |
# File 'lib/posthog/client.rb', line 242 def reload_feature_flags unless @personal_api_key logger.error( 'You need to specify a personal_api_key to locally evaluate feature flags' ) return end @feature_flags_poller.load_feature_flags(true) end |
#shutdown ⇒ Object
252 253 254 255 |
# File 'lib/posthog/client.rb', line 252 def shutdown @feature_flags_poller.shutdown_poller flush end |