posthog-ruby 3.36.0 → 3.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa1af5a1007154eb69081c96f31c9b81b0070a6b3f1ff02075ef6f8f9ece512c
4
- data.tar.gz: 60f1f2b27c6f492d3241ef6942f5873305268f350072d1f93614d67dcbc3765a
3
+ metadata.gz: f74e09d6dabda8fafd30387e8eb0f6a9b427ffe64b901abde1a0512825b60cb0
4
+ data.tar.gz: 34f44648b0487c09ba9767514a03cc1d6ff5969e37cba73810a4adad5dbf9b48
5
5
  SHA512:
6
- metadata.gz: 3b9ccc3390cbbf623d7266ca537cfaeda7f14160fd5814ac6bd1b5613aa05558ce1a8dbdd150e732a7f42633d1018acbe75ebbb997c3821f5c758304b51c3dfe
7
- data.tar.gz: bdbd3873deedb2e1d674d9560c3915cffbc6df0d9ab3717a734aed12b46f78ace41beef3c84081e8adaa3071a35c838bd207bef4b97c3000cc8d13eda0cc8101
6
+ metadata.gz: cdd9aca3ec01c635049849fcb585350e9a90449e1e961a98281c71e6f27e8aefb3b5a8f5b68b0a8e0ae3388a3501c039c55e485f8c654c41f973183a3277e47d
7
+ data.tar.gz: ec42d3655f3b51edfde368b4fc3c2ab4de5511ebfc1cba8c8553e9ecb76881dedc4080b4864ca9e0323fd78fe7023f184283445f1a92f6a485157e86bf3dd103
@@ -81,7 +81,6 @@ module PostHog
81
81
  # @api private
82
82
  module EventType
83
83
  CUSTOM = 'posthog:custom'
84
- IDENTIFY = 'posthog:identify'
85
84
  MCP_INITIALIZE = 'mcp:initialize'
86
85
  MCP_MISSING_CAPABILITY = 'mcp:missing_capability'
87
86
  MCP_PROMPTS_GET = 'mcp:prompts/get'
@@ -94,7 +93,6 @@ module PostHog
94
93
 
95
94
  EVENT_NAME_BY_TYPE = {
96
95
  CUSTOM => Event::CUSTOM,
97
- IDENTIFY => Event::IDENTIFY,
98
96
  MCP_INITIALIZE => Event::INITIALIZE,
99
97
  MCP_MISSING_CAPABILITY => Event::MISSING_CAPABILITY,
100
98
  MCP_PROMPTS_GET => Event::PROMPT_GET,
@@ -106,6 +106,10 @@ module PostHog
106
106
  add_error_details(event, properties) if event['is_error']
107
107
  properties[Property::PARAMETERS] = event['parameters'] unless event['parameters'].nil?
108
108
  properties[Property::RESPONSE] = event['response'] unless event['response'].nil?
109
+ add_person_properties(event, properties)
110
+ end
111
+
112
+ def add_person_properties(event, properties)
109
113
  actor_data = event['identify_actor_data']
110
114
  properties['$set'] = actor_data.dup if actor_data.is_a?(Hash) && !actor_data.empty?
111
115
  end
@@ -132,6 +136,7 @@ module PostHog
132
136
  add_session_id(event, properties)
133
137
  add_conversation_id(event, properties)
134
138
  add_person_processing(event, properties)
139
+ add_person_properties(event, properties)
135
140
  add_groups(event, properties)
136
141
 
137
142
  error = event['error']
@@ -31,19 +31,16 @@ module PostHog
31
31
  end
32
32
  end
33
33
 
34
- # Atomically read the cached identity, merge the new one, store it, and
35
- # report whether it changed. Keeps concurrent requests for one session
36
- # from interleaving their read-merge-write.
34
+ # Atomically read the cached identity, merge the new one, and store it.
35
+ # Keeps concurrent requests for one session from interleaving their
36
+ # read-merge-write.
37
37
  #
38
- # @return [Array(UserIdentity, Boolean)] `[merged, changed]`
38
+ # @return [UserIdentity] the merged identity
39
39
  def merge!(session_id, identity)
40
40
  @mutex.synchronize do
41
- previous = @cache.delete(session_id)
42
- merged = Identity.merge_identities(previous, identity)
43
- changed = !(previous && Identity.identities_equal?(previous, merged))
41
+ merged = Identity.merge_identities(@cache.delete(session_id), identity)
44
42
  @cache.shift if @cache.length >= @max_size
45
43
  @cache[session_id] = merged
46
- [merged, changed]
47
44
  end
48
45
  end
49
46
 
@@ -56,31 +53,13 @@ module PostHog
56
53
  end
57
54
  end
58
55
 
59
- # Identity resolution: runs the `identify` option, dedupes against the
60
- # per-server cache, and decides when a standalone `$identify` event fires.
56
+ # Identity resolution: runs the `identify` option and merges the result into
57
+ # the per-server cache.
61
58
  #
62
59
  # @api private
63
60
  module Identity
64
61
  module_function
65
62
 
66
- def identities_equal?(first, second)
67
- return false if first.distinct_id != second.distinct_id
68
- return false if sorted_json(first.groups || {}) != sorted_json(second.groups || {})
69
-
70
- a_props = first.properties || {}
71
- b_props = second.properties || {}
72
- return false if a_props.keys.map(&:to_s).sort != b_props.keys.map(&:to_s).sort
73
-
74
- a_props.all? do |key, value|
75
- other = if b_props.key?(key)
76
- b_props[key]
77
- else
78
- b_props[key.is_a?(Symbol) ? key.to_s : key.to_s.to_sym]
79
- end
80
- sorted_json(value) == sorted_json(other)
81
- end
82
- end
83
-
84
63
  def merge_identities(previous, nxt)
85
64
  return nxt if previous.nil?
86
65
 
@@ -92,8 +71,7 @@ module PostHog
92
71
  end
93
72
 
94
73
  # Resolve the optional `identify` callback for one request: the identity the
95
- # request's events belong to, plus an `$identify` event to emit only when
96
- # that identity has materially changed.
74
+ # request's events belong to.
97
75
  #
98
76
  # The actor is handed back rather than left for the caller to read out of
99
77
  # {IdentityCache} later. The cache is keyed by session, and a request's
@@ -102,10 +80,10 @@ module PostHog
102
80
  # When resolution yields nothing the cache is read once, here, so whatever
103
81
  # a request is attributed to it is attributed to consistently.
104
82
  #
105
- # @return [Array(Hash, UserIdentity), Array(nil, UserIdentity), Array(nil, nil)] `[event, actor]`
106
- def identify_for_request(data, session_id, request, extra)
83
+ # @return [UserIdentity, nil]
84
+ def resolve(data, session_id, request, extra)
107
85
  identify = data.options.identify
108
- return [nil, nil] unless identify
86
+ return nil unless identify
109
87
 
110
88
  result = if identify.is_a?(UserIdentity) || identify.is_a?(Hash)
111
89
  identify
@@ -116,62 +94,13 @@ module PostHog
116
94
  identity = UserIdentity.coerce(result)
117
95
  unless identity
118
96
  Log.debug(data.options, "Warning: Supplied identify function returned null for session #{session_id}")
119
- return [nil, data.identified_sessions.get(session_id)]
97
+ return data.identified_sessions.get(session_id)
120
98
  end
121
99
 
122
- merged, changed = data.identified_sessions.merge!(session_id, identity)
123
- return [nil, merged] unless changed
124
-
125
- Log.debug(data.options, "Identified session #{session_id}")
126
- [{
127
- 'session_id' => session_id,
128
- 'resource_name' => request_resource_name(request),
129
- 'event_type' => EventType::IDENTIFY,
130
- 'parameters' => { 'request' => request, 'extra' => captured_extra(extra) },
131
- 'timestamp' => Time.now.utc
132
- }, merged]
100
+ data.identified_sessions.merge!(session_id, identity)
133
101
  rescue StandardError => e
134
102
  Log.debug(data.options, "Error: identify function threw while identifying session #{session_id} - #{e.message}")
135
- [nil, data.identified_sessions.get(session_id)]
136
- end
137
-
138
- # @return [Hash, nil] the `$identify` event alone; see {identify_for_request}.
139
- def handle_identify(data, session_id, request, extra)
140
- identify_for_request(data, session_id, request, extra).first
141
- end
142
-
143
- def request_resource_name(request)
144
- return 'Unknown' unless request.is_a?(Hash)
145
-
146
- params = request[:params] || request['params']
147
- return 'Unknown' unless params.is_a?(Hash)
148
-
149
- name = params[:name] || params['name']
150
- name.is_a?(String) ? name : 'Unknown'
151
- end
152
-
153
- # Only JSON scalars from `extra` are captured; never opaque transport objects.
154
- def captured_extra(extra)
155
- return nil unless extra.is_a?(Hash)
156
-
157
- extra.select do |_, value|
158
- value.nil? || value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false
159
- end
160
- end
161
-
162
- def sorted_json(value)
163
- JSON.generate(sort_deep(value))
164
- rescue StandardError
165
- value.to_s
166
- end
167
-
168
- def sort_deep(value)
169
- case value
170
- when Hash then value.map { |k, v| [k.to_s, sort_deep(v)] }.sort_by(&:first).to_h
171
- when Array then value.map { |v| sort_deep(v) }
172
- when String, Numeric, true, false, nil then value
173
- else value.to_s
174
- end
103
+ data.identified_sessions.get(session_id)
175
104
  end
176
105
  end
177
106
 
@@ -523,17 +523,13 @@ module PostHog
523
523
  # `identify` callback runs once per session per request, so preparing
524
524
  # twice never asks it the same question twice.
525
525
  unless @identified_in_request.key?(session_id)
526
- identify_event, actor = Identity.identify_for_request(@data, session_id, request, extra)
526
+ actor = Identity.resolve(@data, session_id, request, extra)
527
527
  # Pin what this request resolved. Every event it emits afterwards -
528
528
  # including one a tool body captures through {Analytics} - is attributed
529
529
  # to this, never to a re-read of the cache another request may have
530
530
  # overwritten while the handler ran.
531
531
  @identified_in_request[session_id] = actor
532
532
  @scope[:actor] = actor if @scope.is_a?(Hash)
533
- if identify_event
534
- self.class.capture_event(@data, identify_event.merge('session_source' => source),
535
- actor: actor)
536
- end
537
533
  end
538
534
  session_id
539
535
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PostHog
4
- VERSION = '3.36.0'
4
+ VERSION = '3.37.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posthog-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.36.0
4
+ version: 3.37.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PostHog