posthog-ruby 3.41.0 → 3.42.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: 53dc3d5bed1cb575616ac9a1c008327962694e5374e3c4b13cc85b3c89563640
4
- data.tar.gz: 1582ca6801fb82a6c107a91bfde35e2486ab894379fefda7743d6da331f5edb8
3
+ metadata.gz: 78dd9125dd36fbfc194e377fafe2541b38a9ab90a77da7e451a6cca521142b6a
4
+ data.tar.gz: 8e274d2e3f5088c5fa17f5edf9b3ecd3ba5889258092e45eede0f84533b3812f
5
5
  SHA512:
6
- metadata.gz: 148a0aa5c64dbf8d5adde86bda82841e19b91b963d4bab80508c8f685b77b6267c778812d109f2bec0bcd563388a58d7374d2e5d58c12f519570b128d09190b0
7
- data.tar.gz: d134bc4159fc1966ef591e0914809889a83ddd8afa7962597a63e3acd458545f62d635b4082fafcc7d47755a0f0ff02415cb22cb5f2d8b00eda8d13a9d2c7322
6
+ metadata.gz: 3c963f48ca74a2de5d79f2734a748fef8c1ec74b8e03fd06a2d61056626ce93dd77cdb339655986d5fbfbbebc355ad0c4ba0d12ee8d1070f081302b4abb8bc18
7
+ data.tar.gz: b90a93a80fbb4ba1a6f98b63607bee7f02bd897c2a8d81f5db757e61b0ad844ef6ae537478e4a96cedd7600ed03ef2df212c67f3a2ad460ee848cf57b1fc2834
@@ -1445,7 +1445,8 @@ module PostHog
1445
1445
  groups, person_properties, group_properties
1446
1446
  )
1447
1447
 
1448
- all_group_properties = {}
1448
+ # Group properties without a group key are kept: a filter on that group type still reads them.
1449
+ all_group_properties = group_properties.dup
1449
1450
  groups&.each do |group_name, group_key|
1450
1451
  all_group_properties[group_name] = {
1451
1452
  '$group_key': group_key
@@ -44,6 +44,16 @@ module PostHog
44
44
  # html: rendered_html,
45
45
  # distinct_id: 'user_123'
46
46
  # )
47
+ #
48
+ # @example Email headers and event tags on a code send
49
+ # posthog.emails.send(
50
+ # to: 'user@example.com',
51
+ # template_key: 'order-shipped',
52
+ # subject: 'Your order has shipped',
53
+ # html: rendered_html,
54
+ # headers: { 'X-Entity-Ref-ID' => 'order-1234' },
55
+ # tags: { 'plan' => 'pro', 'flow' => 'order-shipped' }
56
+ # )
47
57
  class Emails
48
58
  include PostHog::Logging
49
59
  include PostHog::Utils
@@ -97,7 +107,9 @@ module PostHog
97
107
  "project's engagement setting."
98
108
  CODE_FORM_WHY = 'A send with template_key takes its content from your code.'
99
109
  TEMPLATE_FIELDS = %i[template_data].freeze
100
- CODE_FIELDS = %i[subject html text from reply_to cc bcc attachments sensitive tracking capture_engagement].freeze
110
+ CODE_FIELDS = %i[
111
+ subject html text from reply_to cc bcc attachments sensitive tracking capture_engagement headers tags
112
+ ].freeze
101
113
  NETWORK_ERRORS = [
102
114
  Timeout::Error, SocketError, SystemCallError, IOError, EOFError, OpenSSL::SSL::SSLError,
103
115
  Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, Zlib::Error
@@ -173,6 +185,23 @@ module PostHog
173
185
  # @param capture_engagement [Boolean] Deprecated: use +tracking: { engagement: false }+ instead. Code form:
174
186
  # whether PostHog captures the email's engagement events. It still works, but a send can't set both. Sent
175
187
  # only when you set it.
188
+ # @param headers [Hash{String => String}] Code form: email headers that PostHog puts on the email itself, not on
189
+ # the HTTP request, such as +{ 'X-Entity-Ref-ID' => 'order-1234' }+. At most 8. PostHog takes `In-Reply-To`,
190
+ # `References`, `List-Unsubscribe`, `List-Unsubscribe-Post`, `List-Id` and any `X-` header, but not `X-PostHog-`
191
+ # or `X-SES-` headers. A value is at most 870 ASCII characters without line breaks. Only an `X-` header takes
192
+ # non-ASCII text, up to 200 UTF-8 bytes. A header PostHog sets itself, such as `List-Unsubscribe` on marketing
193
+ # email, keeps PostHog's value. The client checks only that it is a Hash and sends it as given: PostHog refuses
194
+ # other names and values with {EmailError#code} `invalid_header`. An empty Hash sends nothing. A template send
195
+ # can't take it, and PostHog refuses it from the PostHog test sender with `sandbox_fields_unsupported`, and with
196
+ # `field_not_accepted` where PostHog doesn't take it yet. Added in posthog-ruby 3.42.0.
197
+ # @param tags [Hash{String => String}] Code form: names and values PostHog adds to the email's events, such as
198
+ # +{ 'plan' => 'pro' }+. They come back as `$email_tags` on the `$workflows_email_sent`, delivered, opened,
199
+ # clicked and bounced events, so you can break those down by a property such as `$email_tags.plan`. At most 10.
200
+ # A name is 1 to 64 letters, digits, `_` and `-`, and starts with a letter or digit. A value is 1 to 256 of the
201
+ # same characters. The client checks only that it is a Hash and sends it as given: PostHog refuses other names
202
+ # and values with {EmailError#code} `invalid_tag`. An empty Hash sends nothing. A template send can't take it,
203
+ # and PostHog refuses it from the PostHog test sender with `sandbox_fields_unsupported`, and with
204
+ # `field_not_accepted` where PostHog doesn't take it yet. Added in posthog-ruby 3.42.0.
176
205
  # @param distinct_id [String] The recipient's distinct ID in your product. The email's events go to this
177
206
  # person. Without it, they go to a person whose distinct ID is the email address.
178
207
  # @param idempotency_key [String] 1 to 255 visible ASCII characters. A send with a key PostHog has already
@@ -193,12 +222,12 @@ module PostHog
193
222
  def send(
194
223
  to:, template_id: nil, template_data: nil, template_key: nil, subject: nil, html: nil, text: nil, from: nil,
195
224
  reply_to: nil, cc: nil, bcc: nil, attachments: nil, sensitive: nil, tracking: nil, capture_engagement: nil,
196
- distinct_id: nil, idempotency_key: nil, max_retries: nil
225
+ headers: nil, tags: nil, distinct_id: nil, idempotency_key: nil, max_retries: nil
197
226
  )
198
227
  fields = {
199
228
  template_data: template_data, subject: subject, html: html, text: text, from: from, reply_to: reply_to,
200
229
  cc: cc, bcc: bcc, attachments: attachments, sensitive: sensitive, tracking: tracking,
201
- capture_engagement: capture_engagement
230
+ capture_engagement: capture_engagement, headers: headers, tags: tags
202
231
  }.compact
203
232
  check_recipient(to, distinct_id, idempotency_key, max_retries)
204
233
  payload = content(template_id, template_key, fields).merge(to: to)
@@ -206,13 +235,14 @@ module PostHog
206
235
  payload[:attachments] = files.references unless files.empty?
207
236
  payload[:distinct_id] = distinct_id unless distinct_id.nil?
208
237
  body = JSON.generate(payload)
209
- headers = { 'Content-Type' => 'application/json', 'Idempotency-Key' => idempotency_key || SecureRandom.uuid }
238
+ request_headers = { 'Content-Type' => 'application/json',
239
+ 'Idempotency-Key' => idempotency_key || SecureRandom.uuid }
210
240
 
211
241
  result = call('no email was sent', SEND_KEY_NEEDED) do |secret_key|
212
242
  upload(files, secret_key, max_retries || @max_retries) unless files.empty?
213
243
  uri = URI(@app_host + SEND_PATH)
214
244
  with_retries(max_retries || @max_retries) do
215
- parse_send_reply(request(Net::HTTP::Post.new(uri), uri, secret_key, headers, body))
245
+ parse_send_reply(request(Net::HTTP::Post.new(uri), uri, secret_key, request_headers, body))
216
246
  end
217
247
  end
218
248
  result.warnings.each { |warning| logger.warn("PostHog email warning #{warning.code}: #{warning.message}") }
@@ -355,6 +385,9 @@ module PostHog
355
385
  %i[capture_engagement sensitive].each do |name|
356
386
  raise ArgumentError, "#{name} must be true or false." unless [nil, true, false].include?(fields[name])
357
387
  end
388
+ %i[headers tags].each do |name|
389
+ raise ArgumentError, "#{name} must be a Hash." if fields.key?(name) && !fields[name].is_a?(Hash)
390
+ end
358
391
 
359
392
  code = fields.slice(:subject, :html, :text, :from).merge(template_key: template_key)
360
393
  code[:reply_to] = addresses(:reply_to, fields[:reply_to]) if fields.key?(:reply_to)
@@ -365,6 +398,7 @@ module PostHog
365
398
  code[:capture_engagement] = fields[:capture_engagement] if fields.key?(:capture_engagement)
366
399
  code[:sensitive] = fields[:sensitive] if fields.key?(:sensitive)
367
400
  code[:tracking] = tracking(fields[:tracking]) if fields.key?(:tracking)
401
+ %i[headers tags].each { |name| code[name] = fields[name] unless fields.fetch(name, {}).empty? }
368
402
  code
369
403
  end
370
404
 
@@ -1651,8 +1651,10 @@ module PostHog
1651
1651
  end
1652
1652
 
1653
1653
  unsupported = (flag_filters[:groups] || []).any? do |condition|
1654
+ person_condition = condition.fetch(:aggregation_group_type_index, aggregation).nil?
1654
1655
  (condition[:properties] || []).any? do |property|
1655
- unsupported_property?(property, flags_by_key, cohorts, Set.new)
1656
+ (person_condition && index_less_group_filter?(property)) ||
1657
+ unsupported_property?(property, flags_by_key, cohorts, Set.new)
1656
1658
  end
1657
1659
  end
1658
1660
  unsupported ? UnresolvedFlagReason::UNSUPPORTED_DEFINITION : nil
@@ -1684,6 +1686,11 @@ module PostHog
1684
1686
  "Cannot evaluate flag dependency '#{missing_dependency}' - flag not found in local flags"
1685
1687
  end
1686
1688
 
1689
+ # In a person condition an index-less group filter has no group type to read.
1690
+ def self.index_less_group_filter?(property)
1691
+ property[:type] == 'group' && property[:group_type_index].nil?
1692
+ end
1693
+
1687
1694
  def self.unsupported_property?(property, flags_by_key, cohorts, visited_cohort_ids)
1688
1695
  case property[:type]
1689
1696
  when 'flag'
@@ -1735,6 +1742,7 @@ module PostHog
1735
1742
  end
1736
1743
 
1737
1744
  private_class_method :extract_value, :cohort_flag_dependency?, :unparsable_cohort_flag_leaf?,
1745
+ :index_less_group_filter?,
1738
1746
  :assert_cohort_flag_leaves_parse,
1739
1747
  :find_cohort_property, :nested_property_group?,
1740
1748
  :property_group_items_match?, :assert_property_filter_type_supported,
@@ -1935,14 +1943,16 @@ module PostHog
1935
1943
  known_absent_scope: effective_known_absent_scope,
1936
1944
  bucketing_value: effective_bucketing,
1937
1945
  missing_device_id: missing_device_id,
1938
- dependency_evaluation: dependency_evaluation)
1946
+ dependency_evaluation: dependency_evaluation,
1947
+ condition_aggregation: condition_aggregation)
1939
1948
  else
1940
1949
  # Without device bucketing the condition hashes its distinct id; skipping the extra keywords
1941
1950
  # keeps the common path as cheap as before.
1942
1951
  condition_match_outcome(flag, condition_distinct_id, condition, effective_properties, evaluation_cache,
1943
1952
  cohort_properties, snapshot: snapshot, context: context,
1944
1953
  known_absent_scope: effective_known_absent_scope,
1945
- dependency_evaluation: dependency_evaluation)
1954
+ dependency_evaluation: dependency_evaluation,
1955
+ condition_aggregation: condition_aggregation)
1946
1956
  end
1947
1957
  case outcome
1948
1958
  when :match
@@ -2018,7 +2028,7 @@ module PostHog
2018
2028
  def condition_match_outcome(flag, distinct_id, condition, properties, evaluation_cache, cohort_properties = {},
2019
2029
  snapshot: _evaluation_snapshot, known_absent_scope: nil, context: nil,
2020
2030
  bucketing_value: distinct_id, missing_device_id: false,
2021
- dependency_evaluation: nil)
2031
+ dependency_evaluation: nil, condition_aggregation: nil)
2022
2032
  rollout_percentage = condition[:rollout_percentage]
2023
2033
 
2024
2034
  unless (condition[:properties] || []).empty?
@@ -2032,10 +2042,17 @@ module PostHog
2032
2042
  evaluate_flag_dependency(prop, evaluation_cache, distinct_id, properties, cohort_properties,
2033
2043
  snapshot: snapshot, context: context,
2034
2044
  dependency_evaluation: dependency_evaluation)
2035
- else
2045
+ elsif reads_condition_properties?(prop, condition_aggregation, context)
2036
2046
  FeatureFlagsPoller.match_property(prop, properties, cohort_properties,
2037
2047
  property_matching_version: snapshot[:property_matching_version],
2038
2048
  known_absent_scope: known_absent_scope)
2049
+ else
2050
+ filter_properties, filter_scope = filter_property_source(prop, properties, known_absent_scope,
2051
+ condition_aggregation, context, snapshot)
2052
+ !filter_properties.nil? &&
2053
+ FeatureFlagsPoller.match_property(prop, filter_properties, cohort_properties,
2054
+ property_matching_version: snapshot[:property_matching_version],
2055
+ known_absent_scope: filter_scope)
2039
2056
  end
2040
2057
  return :no_match unless matches
2041
2058
  rescue RequiresServerEvaluation => e
@@ -2077,6 +2094,82 @@ module PostHog
2077
2094
  :match
2078
2095
  end
2079
2096
 
2097
+ # The common case, checked without allocating on the per-filter hot path.
2098
+ def reads_condition_properties?(property, condition_aggregation, context)
2099
+ return true if context.nil?
2100
+
2101
+ case property[:type]
2102
+ when 'person' then condition_aggregation.nil?
2103
+ when 'group'
2104
+ filter_index = property[:group_type_index]
2105
+ filter_index.nil? ? !condition_aggregation.nil? : filter_index.to_s == condition_aggregation.to_s
2106
+ else true
2107
+ end
2108
+ end
2109
+
2110
+ # Like the flags service, a filter reads the properties of its own type, not those of the condition's
2111
+ # aggregation: a person filter reads the person, and a group filter reads the group type it names.
2112
+ # Returns the properties and known-absent scope for the filter, or nil properties for a definitive no-match.
2113
+ def filter_property_source(property, properties, known_absent_scope, condition_aggregation, context, snapshot)
2114
+ return [properties, known_absent_scope] if context.nil?
2115
+
2116
+ type = property[:type]
2117
+ known_absent = context[:known_absent]
2118
+ # Like posthog-rs, a filter without a type keeps reading the condition's properties, as older definitions expect.
2119
+ if type == 'person'
2120
+ return [properties, known_absent_scope] if condition_aggregation.nil?
2121
+
2122
+ return [person_properties_with_distinct_id(context), known_absent&.person_scope]
2123
+ end
2124
+ return [properties, known_absent_scope] unless type == 'group'
2125
+
2126
+ filter_index = property[:group_type_index]
2127
+ if filter_index.nil?
2128
+ return [properties, known_absent_scope] unless condition_aggregation.nil?
2129
+
2130
+ raise InconclusiveMatchError.new(
2131
+ "Can't evaluate group filter '#{property[:key]}' without a group type index in a person condition",
2132
+ reason: UnresolvedFlagReason::UNSUPPORTED_DEFINITION
2133
+ )
2134
+ end
2135
+ return [properties, known_absent_scope] if filter_index.to_s == condition_aggregation.to_s
2136
+
2137
+ other_group_type_source(property, filter_index, context, snapshot)
2138
+ end
2139
+
2140
+ def other_group_type_source(property, filter_index, context, snapshot)
2141
+ key = property[:key].to_s
2142
+ group_name = snapshot[:group_type_mapping][filter_index.to_s.to_sym]
2143
+ if group_name.nil?
2144
+ # The filter has no group type to read. Without group context the flags service reads every key absent.
2145
+ return [nil, nil] if group_context?(context)
2146
+
2147
+ return [{}, KnownAbsentPropertyKeys::Scope.new(Set[key], filter_index)]
2148
+ end
2149
+
2150
+ group_properties = context[:group_properties][group_name.to_sym] || {}
2151
+ listed = context[:known_absent]&.group_keys(group_name) || Set.new
2152
+ if FeatureFlagsPoller.usable_group_key?(context[:groups][group_name.to_sym])
2153
+ return [group_properties, KnownAbsentPropertyKeys::Scope.new(listed, filter_index)]
2154
+ end
2155
+
2156
+ # Without a key for that group type, the supplied properties are all the flags service has.
2157
+ supplied = group_properties.key?(key.to_sym) || group_properties.key?(key)
2158
+ listed |= [key] unless supplied
2159
+ [group_properties, KnownAbsentPropertyKeys::Scope.new(listed, filter_index)]
2160
+ end
2161
+
2162
+ # The flags service adds a supplied group key as `$group_key`, so an unusable injected key isn't context.
2163
+ def group_context?(context)
2164
+ return true if context[:groups].values.any? { |group_key| FeatureFlagsPoller.usable_group_key?(group_key) }
2165
+
2166
+ context[:group_properties].values.any? do |group_properties|
2167
+ group_properties.any? do |name, value|
2168
+ name.to_s != '$group_key' || FeatureFlagsPoller.usable_group_key?(value)
2169
+ end
2170
+ end
2171
+ end
2172
+
2080
2173
  # This function takes a distinct_id and a feature flag key and returns a float between 0 and 1.
2081
2174
  # Given the same distinct_id and key, it'll always return the same float. These floats are
2082
2175
  # uniformly distributed between 0 and 1, so if we want to show this feature to 20% of traffic
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PostHog
4
- VERSION = '3.41.0'
4
+ VERSION = '3.42.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.41.0
4
+ version: 3.42.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PostHog