posthog-ruby 3.40.0 → 3.41.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: 16949639bc7e275d0e914a05589108fac9d8c19a41b54e97e5aac196f6d75132
4
- data.tar.gz: f1e09622f31b1fa977dc2dcf8385125984fbbc3fcd218012c3cf41cbad4157eb
3
+ metadata.gz: 53dc3d5bed1cb575616ac9a1c008327962694e5374e3c4b13cc85b3c89563640
4
+ data.tar.gz: 1582ca6801fb82a6c107a91bfde35e2486ab894379fefda7743d6da331f5edb8
5
5
  SHA512:
6
- metadata.gz: 277c507553f1fa823ff53030faff2d235877289409d728e96357265d33a2165862d7e604188ab5b610e7dce0907c8998c0a0dd8422e464feb28b32930375d9da
7
- data.tar.gz: b6875faf06f12a2b1e012be6ad49ed185b7f5bcc78b2877d5d24c10e3831bdedbdd8d97a8dd728f6e6f103249a5d04560f07cb95574a67b00cfd339ea4c9ba28
6
+ metadata.gz: 148a0aa5c64dbf8d5adde86bda82841e19b91b963d4bab80508c8f685b77b6267c778812d109f2bec0bcd563388a58d7374d2e5d58c12f519570b128d09190b0
7
+ data.tar.gz: d134bc4159fc1966ef591e0914809889a83ddd8afa7962597a63e3acd458545f62d635b4082fafcc7d47755a0f0ff02415cb22cb5f2d8b00eda8d13a9d2c7322
@@ -379,35 +379,12 @@ module PostHog
379
379
  'avoids a second `/flags` request per capture and guarantees the event carries the ' \
380
380
  'exact flag values your code branched on.'
381
381
  )
382
- # Handle different types of send_feature_flags parameter
383
- case send_feature_flags_param
384
- when true
385
- # Backward compatibility: simple boolean
386
- feature_variants = @feature_flags_poller.get_feature_variants(attrs[:distinct_id], attrs[:groups] || {})
387
- when Hash
388
- # Hash with options
389
- options = SendFeatureFlagsOptions.from_hash(send_feature_flags_param)
390
- feature_variants = @feature_flags_poller.get_feature_variants(
391
- attrs[:distinct_id],
392
- attrs[:groups] || {},
393
- options ? options.person_properties : {},
394
- options ? options.group_properties : {},
395
- options ? options.only_evaluate_locally : false
396
- )
397
- when SendFeatureFlagsOptions
398
- # SendFeatureFlagsOptions object
399
- feature_variants = @feature_flags_poller.get_feature_variants(
400
- attrs[:distinct_id],
401
- attrs[:groups] || {},
402
- send_feature_flags_param.person_properties,
403
- send_feature_flags_param.group_properties,
404
- send_feature_flags_param.only_evaluate_locally || false
405
- )
406
- else
407
- # Invalid type, treat as false
408
- feature_variants = nil
409
- end
410
-
382
+ options = case send_feature_flags_param
383
+ when true then SendFeatureFlagsOptions.new
384
+ when Hash then SendFeatureFlagsOptions.from_hash(send_feature_flags_param)
385
+ when SendFeatureFlagsOptions then send_feature_flags_param
386
+ end
387
+ feature_variants = options && _capture_feature_variants(attrs[:distinct_id], attrs[:groups], options)
411
388
  attrs[:feature_variants] = feature_variants if feature_variants
412
389
  end
413
390
 
@@ -530,6 +507,10 @@ module PostHog
530
507
  # @param person_properties [Hash] Properties to use when evaluating the user locally or remotely.
531
508
  # @param group_properties [Hash] Properties to use when evaluating groups locally or remotely.
532
509
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
510
+ # @param person_property_keys_known_absent [Array<String, Symbol>, Set<String, Symbol>, nil] Person property
511
+ # keys known to be absent. See {#evaluate_flags}.
512
+ # @param group_property_keys_known_absent [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil]
513
+ # Group property keys known to be absent, by group type. See {#evaluate_flags}.
533
514
  # @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
534
515
  # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
535
516
  # person property. An explicit device id is sent to /flags.
@@ -543,7 +524,9 @@ module PostHog
543
524
  group_properties: {},
544
525
  only_evaluate_locally: false,
545
526
  send_feature_flag_events: true,
546
- device_id: nil
527
+ device_id: nil,
528
+ person_property_keys_known_absent: nil,
529
+ group_property_keys_known_absent: nil
547
530
  )
548
531
  _emit_deprecation(
549
532
  :is_feature_enabled,
@@ -557,7 +540,9 @@ module PostHog
557
540
  flag_key, distinct_id,
558
541
  groups: groups, person_properties: person_properties, group_properties: group_properties,
559
542
  only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events,
560
- device_id: device_id
543
+ device_id: device_id,
544
+ person_property_keys_known_absent: person_property_keys_known_absent,
545
+ group_property_keys_known_absent: group_property_keys_known_absent
561
546
  )
562
547
  response = result&.value
563
548
  return nil if response.nil?
@@ -581,6 +566,10 @@ module PostHog
581
566
  # @param [Hash] person_properties key-value pairs of properties to associate with the user.
582
567
  # @param [Hash] group_properties
583
568
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
569
+ # @param person_property_keys_known_absent [Array<String, Symbol>, Set<String, Symbol>, nil] Person property
570
+ # keys known to be absent. See {#evaluate_flags}.
571
+ # @param group_property_keys_known_absent [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil]
572
+ # Group property keys known to be absent, by group type. See {#evaluate_flags}.
584
573
  # @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
585
574
  # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
586
575
  # person property. An explicit device id is sent to /flags.
@@ -607,7 +596,9 @@ module PostHog
607
596
  group_properties: {},
608
597
  only_evaluate_locally: false,
609
598
  send_feature_flag_events: true,
610
- device_id: nil
599
+ device_id: nil,
600
+ person_property_keys_known_absent: nil,
601
+ group_property_keys_known_absent: nil
611
602
  )
612
603
  _emit_deprecation(
613
604
  :get_feature_flag,
@@ -620,7 +611,9 @@ module PostHog
620
611
  key, distinct_id,
621
612
  groups: groups, person_properties: person_properties, group_properties: group_properties,
622
613
  only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events,
623
- device_id: device_id
614
+ device_id: device_id,
615
+ person_property_keys_known_absent: person_property_keys_known_absent,
616
+ group_property_keys_known_absent: group_property_keys_known_absent
624
617
  )
625
618
  result&.value
626
619
  end
@@ -633,6 +626,10 @@ module PostHog
633
626
  # @param person_properties [Hash] Properties to use when evaluating the user locally or remotely.
634
627
  # @param group_properties [Hash] Properties to use when evaluating groups locally or remotely.
635
628
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
629
+ # @param person_property_keys_known_absent [Array<String, Symbol>, Set<String, Symbol>, nil] Person property
630
+ # keys known to be absent. See {#evaluate_flags}.
631
+ # @param group_property_keys_known_absent [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil]
632
+ # Group property keys known to be absent, by group type. See {#evaluate_flags}.
636
633
  # @param send_feature_flag_events [Boolean] Whether to capture `$feature_flag_called` for this access.
637
634
  # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
638
635
  # person property. An explicit device id is sent to /flags.
@@ -645,7 +642,9 @@ module PostHog
645
642
  group_properties: {},
646
643
  only_evaluate_locally: false,
647
644
  send_feature_flag_events: true,
648
- device_id: nil
645
+ device_id: nil,
646
+ person_property_keys_known_absent: nil,
647
+ group_property_keys_known_absent: nil
649
648
  )
650
649
  _emit_deprecation(
651
650
  :get_feature_flag_result,
@@ -660,7 +659,9 @@ module PostHog
660
659
  key, distinct_id,
661
660
  groups: groups, person_properties: person_properties, group_properties: group_properties,
662
661
  only_evaluate_locally: only_evaluate_locally, send_feature_flag_events: send_feature_flag_events,
663
- device_id: device_id
662
+ device_id: device_id,
663
+ person_property_keys_known_absent: person_property_keys_known_absent,
664
+ group_property_keys_known_absent: group_property_keys_known_absent
664
665
  )
665
666
  end
666
667
 
@@ -685,6 +686,17 @@ module PostHog
685
686
  # Distinct from {FeatureFlagEvaluations#only}, which filters the already-fetched snapshot in memory.
686
687
  # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
687
688
  # person property. An explicit device id is sent to /flags.
689
+ # @param [Array<String, Symbol>, Set<String, Symbol>, nil] person_property_keys_known_absent Person property keys
690
+ # the caller knows are absent for this evaluation. A listed key is absent even when +person_properties+
691
+ # has a value for it, so local evaluation resolves it definitively: +is_not_set+ and the negated operators
692
+ # (+is_not+, +not_icontains+, +not_starts_with+, +not_ends_with+, +not_regex+) match, and every other
693
+ # operator the SDK evaluates locally doesn't. An operator it can't evaluate locally, or a filter without a
694
+ # condition value on an operator that needs one, still falls back to +/flags+. Keys starting with
695
+ # +__posthog_person_metadata__+ are ignored. Sent to +/flags+ on remote evaluation. At most 1000 keys,
696
+ # counting duplicates; a longer list raises +ArgumentError+ and is never truncated.
697
+ # @param [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil] group_property_keys_known_absent
698
+ # Group property keys known to be absent, by group type name. Each group type's keys apply only to that
699
+ # group type and follow the same rules and 1000-key limit as +person_property_keys_known_absent+.
688
700
  # @return [PostHog::FeatureFlagEvaluations]
689
701
  def evaluate_flags(
690
702
  distinct_id,
@@ -694,7 +706,9 @@ module PostHog
694
706
  only_evaluate_locally: false,
695
707
  disable_geoip: nil,
696
708
  flag_keys: nil,
697
- device_id: nil
709
+ device_id: nil,
710
+ person_property_keys_known_absent: nil,
711
+ group_property_keys_known_absent: nil
698
712
  )
699
713
  host = _feature_flag_evaluations_host
700
714
 
@@ -704,6 +718,9 @@ module PostHog
704
718
 
705
719
  return FeatureFlagEvaluations.new(host: host, distinct_id: distinct_id, flags: {}, groups: groups) if @disabled
706
720
 
721
+ known_absent = KnownAbsentPropertyKeys.build(person_property_keys_known_absent,
722
+ group_property_keys_known_absent)
723
+
707
724
  if flag_keys && flag_keys.empty?
708
725
  return FeatureFlagEvaluations.new(
709
726
  host: host, distinct_id: distinct_id, flags: {}, groups: groups, disable_geoip: disable_geoip
@@ -730,7 +747,7 @@ module PostHog
730
747
  match = @feature_flags_poller.send(
731
748
  :_compute_flag_locally,
732
749
  definition, distinct_id, groups, person_properties, group_properties,
733
- snapshot: definition_snapshot, device_id: device_id
750
+ snapshot: definition_snapshot, known_absent: known_absent, device_id: device_id
734
751
  )
735
752
  rescue PostHog::RequiresServerEvaluation, PostHog::InconclusiveMatchError => e
736
753
  unresolved_flags[key.to_s] = e.reason
@@ -782,7 +799,7 @@ module PostHog
782
799
  begin
783
800
  flags_response = @feature_flags_poller.get_flags(
784
801
  distinct_id, groups, person_properties, group_properties, flag_keys, disable_geoip,
785
- device_id: device_id
802
+ known_absent: known_absent, device_id: device_id
786
803
  )
787
804
  minimal_flag_called_events = flags_response[:minimalFlagCalledEvents] == true
788
805
  request_id = flags_response[:requestId]
@@ -841,6 +858,10 @@ module PostHog
841
858
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
842
859
  # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
843
860
  # person property. An explicit device id is sent to /flags.
861
+ # @param person_property_keys_known_absent [Array<String, Symbol>, Set<String, Symbol>, nil] Person property
862
+ # keys known to be absent. See {#evaluate_flags}.
863
+ # @param group_property_keys_known_absent [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil]
864
+ # Group property keys known to be absent, by group type. See {#evaluate_flags}.
844
865
  #
845
866
  # @return [Hash] String (not symbol) key value pairs of flag and their values
846
867
  def get_all_flags(
@@ -849,15 +870,19 @@ module PostHog
849
870
  person_properties: {},
850
871
  group_properties: {},
851
872
  only_evaluate_locally: false,
852
- device_id: nil
873
+ device_id: nil,
874
+ person_property_keys_known_absent: nil,
875
+ group_property_keys_known_absent: nil
853
876
  )
854
877
  return {} if @disabled
855
878
 
879
+ known_absent = KnownAbsentPropertyKeys.build(person_property_keys_known_absent,
880
+ group_property_keys_known_absent)
856
881
  groups, person_properties, group_properties = add_local_person_and_group_properties(
857
882
  groups, person_properties, group_properties
858
883
  )
859
884
  @feature_flags_poller.get_all_flags(distinct_id, groups, person_properties, group_properties,
860
- only_evaluate_locally, device_id: device_id)
885
+ only_evaluate_locally, known_absent: known_absent, device_id: device_id)
861
886
  end
862
887
 
863
888
  # Returns payload for a given feature flag
@@ -874,6 +899,10 @@ module PostHog
874
899
  # @param only_evaluate_locally [Boolean]
875
900
  # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
876
901
  # person property. An explicit device id is sent to /flags.
902
+ # @param person_property_keys_known_absent [Array<String, Symbol>, Set<String, Symbol>, nil] Person property
903
+ # keys known to be absent. See {#evaluate_flags}.
904
+ # @param group_property_keys_known_absent [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil]
905
+ # Group property keys known to be absent, by group type. See {#evaluate_flags}.
877
906
  # @return [Object, nil] The parsed payload for the matched flag value.
878
907
  #
879
908
  # @deprecated Use {#evaluate_flags} and {FeatureFlagEvaluations#get_flag_payload} instead.
@@ -885,7 +914,9 @@ module PostHog
885
914
  person_properties: {},
886
915
  group_properties: {},
887
916
  only_evaluate_locally: false,
888
- device_id: nil
917
+ device_id: nil,
918
+ person_property_keys_known_absent: nil,
919
+ group_property_keys_known_absent: nil
889
920
  )
890
921
  _emit_deprecation(
891
922
  :get_feature_flag_payload,
@@ -897,11 +928,14 @@ module PostHog
897
928
  return nil if @disabled
898
929
 
899
930
  key = key.to_s
931
+ known_absent = KnownAbsentPropertyKeys.build(person_property_keys_known_absent,
932
+ group_property_keys_known_absent)
900
933
  groups, person_properties, group_properties = add_local_person_and_group_properties(
901
934
  groups, person_properties, group_properties
902
935
  )
903
936
  @feature_flags_poller.get_feature_flag_payload(key, distinct_id, match_value, groups, person_properties,
904
- group_properties, only_evaluate_locally, device_id: device_id)
937
+ group_properties, only_evaluate_locally,
938
+ known_absent: known_absent, device_id: device_id)
905
939
  end
906
940
 
907
941
  # Returns all flags and payloads for a given user
@@ -917,6 +951,10 @@ module PostHog
917
951
  # @param only_evaluate_locally [Boolean] Skip the remote /flags call.
918
952
  # @param device_id [String, nil] Device id for flags that bucket on device id. Defaults to the `$device_id`
919
953
  # person property. An explicit device id is sent to /flags.
954
+ # @param person_property_keys_known_absent [Array<String, Symbol>, Set<String, Symbol>, nil] Person property
955
+ # keys known to be absent. See {#evaluate_flags}.
956
+ # @param group_property_keys_known_absent [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil]
957
+ # Group property keys known to be absent, by group type. See {#evaluate_flags}.
920
958
  #
921
959
  def get_all_flags_and_payloads(
922
960
  distinct_id,
@@ -924,15 +962,20 @@ module PostHog
924
962
  person_properties: {},
925
963
  group_properties: {},
926
964
  only_evaluate_locally: false,
927
- device_id: nil
965
+ device_id: nil,
966
+ person_property_keys_known_absent: nil,
967
+ group_property_keys_known_absent: nil
928
968
  )
929
969
  return { featureFlags: {}, featureFlagPayloads: {} } if @disabled
930
970
 
971
+ known_absent = KnownAbsentPropertyKeys.build(person_property_keys_known_absent,
972
+ group_property_keys_known_absent)
931
973
  groups, person_properties, group_properties = add_local_person_and_group_properties(
932
974
  groups, person_properties, group_properties
933
975
  )
934
976
  response = @feature_flags_poller.get_all_flags_and_payloads(
935
- distinct_id, groups, person_properties, group_properties, only_evaluate_locally, device_id: device_id
977
+ distinct_id, groups, person_properties, group_properties, only_evaluate_locally,
978
+ known_absent: known_absent, device_id: device_id
936
979
  )
937
980
 
938
981
  # Remove internal information
@@ -1179,11 +1222,15 @@ module PostHog
1179
1222
  group_properties: {},
1180
1223
  only_evaluate_locally: false,
1181
1224
  send_feature_flag_events: true,
1182
- device_id: nil
1225
+ device_id: nil,
1226
+ person_property_keys_known_absent: nil,
1227
+ group_property_keys_known_absent: nil
1183
1228
  )
1184
1229
  return nil if @disabled
1185
1230
 
1186
1231
  key = key.to_s
1232
+ known_absent = KnownAbsentPropertyKeys.build(person_property_keys_known_absent,
1233
+ group_property_keys_known_absent)
1187
1234
  groups, person_properties, group_properties = add_local_person_and_group_properties(
1188
1235
  groups, person_properties, group_properties
1189
1236
  )
@@ -1191,7 +1238,7 @@ module PostHog
1191
1238
  has_experiment, minimal_flag_called_events =
1192
1239
  @feature_flags_poller.get_feature_flag(
1193
1240
  key, distinct_id, groups, person_properties, group_properties, only_evaluate_locally,
1194
- device_id: device_id
1241
+ known_absent: known_absent, device_id: device_id
1195
1242
  )
1196
1243
  if send_feature_flag_events
1197
1244
  properties = {
@@ -1374,6 +1421,25 @@ module PostHog
1374
1421
  @shutdown_mutex.synchronize { @shutdown }
1375
1422
  end
1376
1423
 
1424
+ def _capture_feature_variants(distinct_id, groups, options)
1425
+ begin
1426
+ known_absent = KnownAbsentPropertyKeys.build(options.person_property_keys_known_absent,
1427
+ options.group_property_keys_known_absent)
1428
+ rescue ArgumentError => e
1429
+ # Capture is fire-and-forget, so an invalid listing drops the flags rather than the event.
1430
+ logger.warn("[FEATURE FLAGS] Ignoring send_feature_flags on capture(): #{e.message}")
1431
+ return nil
1432
+ end
1433
+
1434
+ groups, person_properties, group_properties = add_local_person_and_group_properties(
1435
+ groups, options.person_properties, options.group_properties
1436
+ )
1437
+ @feature_flags_poller.get_feature_variants(
1438
+ distinct_id, groups, person_properties, group_properties, options.only_evaluate_locally || false,
1439
+ known_absent: known_absent
1440
+ )
1441
+ end
1442
+
1377
1443
  def add_local_person_and_group_properties(groups, person_properties, group_properties)
1378
1444
  groups, person_properties, group_properties = normalize_flag_evaluation_inputs(
1379
1445
  groups, person_properties, group_properties
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
+ require 'posthog/logging'
4
5
 
5
6
  module PostHog
6
7
  # Represents the result of a feature flag evaluation
@@ -59,23 +60,39 @@ module PostHog
59
60
  end
60
61
  end
61
62
 
62
- # Deserialize a flag payload. Strings are JSON-parsed (with the raw string
63
- # returned when the body is not valid JSON); already-deserialized values
64
- # pass through. Public so {FeatureFlagEvaluations} can normalize payloads
65
- # the same way {FeatureFlagResult} does.
63
+ # Deserialize a flag payload. Strings are JSON-parsed; a string that is not
64
+ # valid JSON (including an empty or whitespace-only one) reads as no payload
65
+ # and is logged, rather than being handed back raw. Already-deserialized
66
+ # values pass through. Public so {FeatureFlagEvaluations} can normalize
67
+ # payloads the same way {FeatureFlagResult} does.
66
68
  #
67
69
  # @param payload [Object, String, nil] The raw payload value.
68
- # @return [Object, nil] The parsed payload.
70
+ # @return [Object, nil] The parsed payload, or nil when it cannot be decoded.
69
71
  def self.parse_payload(payload)
70
72
  return nil if payload.nil?
71
73
  return payload unless payload.is_a?(String)
72
- return nil if payload.empty?
73
74
 
74
75
  begin
75
76
  JSON.parse(payload)
76
- rescue JSON::ParserError
77
- payload
77
+ rescue JSON::ParserError => e
78
+ Logging.logger.warn("[FEATURE FLAGS] Discarding a payload that is not valid JSON: #{e.message}")
79
+ nil
78
80
  end
79
81
  end
82
+
83
+ # Validate a payload that stays in its serialized form. Returns the string
84
+ # unchanged when it decodes, and nil when it does not, so accessors that
85
+ # hand back serialized JSON agree with the decoded accessors about which
86
+ # payloads exist.
87
+ #
88
+ # @param payload [Object, String, nil] The raw payload value.
89
+ # @return [Object, nil] The payload as given, or nil when it cannot be decoded.
90
+ # @api private
91
+ def self.validate_serialized_payload(payload)
92
+ return nil if payload.nil?
93
+ return payload unless payload.is_a?(String)
94
+
95
+ parse_payload(payload).nil? ? nil : payload
96
+ end
80
97
  end
81
98
  end