growthbook 1.2.2 → 2.0.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.
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
4
+
3
5
  module Growthbook
4
6
  # Context object passed into the GrowthBook constructor.
5
- class Context
7
+ class Context # rubocop:disable Metrics/ClassLength
6
8
  # @return [true, false] Switch to globally disable all experiments. Default true.
7
9
  attr_accessor :enabled
8
10
 
@@ -33,29 +35,53 @@ module Growthbook
33
35
  # @return [Hash[String, Any]] Forced feature values
34
36
  attr_reader :forced_features
35
37
 
38
+ # @return [Hash[String, Array]] Saved groups, used by the $inGroup/$notInGroup operators
39
+ attr_reader :saved_groups
40
+
41
+ # @return [Growthbook::StickyBucketService] Sticky bucket service for sticky bucketing
42
+ attr_reader :sticky_bucket_service
43
+
44
+ # @return [String] The attributes that identify users. If omitted, this will be inferred from the feature definitions
45
+ attr_reader :sticky_bucket_identifier_attributes
46
+
47
+ # @return [String] The attributes that are used to assign sticky buckets
48
+ attr_reader :sticky_bucket_assignment_docs
49
+
50
+ # @return [Boolean] If true, the context is using derived sticky bucket attributes
51
+ attr_reader :using_derived_sticky_bucket_attributes
52
+
53
+ # @return [Hash[String, String]] The attributes that are used to assign sticky buckets
54
+ attr_reader :sticky_bucket_attributes
55
+
36
56
  def initialize(options = {})
37
57
  @features = {}
58
+ @attributes = {}
38
59
  @forced_variations = {}
39
60
  @forced_features = {}
40
61
  @attributes = {}
41
62
  @enabled = true
42
63
  @impressions = {}
64
+ @sticky_bucket_assignment_docs = {}
65
+ @saved_groups = {}
66
+
67
+ features = {}
68
+ attributes = {}
43
69
 
44
70
  options.transform_keys(&:to_sym).each do |key, value|
45
71
  case key
46
72
  when :enabled
47
73
  @enabled = value
48
74
  when :attributes
49
- self.attributes = value
75
+ attributes = value
50
76
  when :url
51
77
  @url = value
52
78
  when :decryption_key
53
79
  nil
54
80
  when :encrypted_features
55
81
  decrypted = decrypted_features_from_options(options)
56
- self.features = decrypted unless decrypted.nil?
82
+ features = decrypted unless decrypted.nil?
57
83
  when :features
58
- self.features = value
84
+ features = value
59
85
  when :forced_variations, :forcedVariations
60
86
  self.forced_variations = value
61
87
  when :forced_features
@@ -66,10 +92,20 @@ module Growthbook
66
92
  @listener = value
67
93
  when :on_feature_usage
68
94
  @on_feature_usage = value
95
+ when :sticky_bucket_service
96
+ @sticky_bucket_service = value
97
+ when :sticky_bucket_identifier_attributes
98
+ @sticky_bucket_identifier_attributes = value
99
+ when :saved_groups, :savedGroups
100
+ @saved_groups = value || {}
69
101
  else
70
102
  warn("Unknown context option: #{key}")
71
103
  end
72
104
  end
105
+
106
+ @using_derived_sticky_bucket_attributes = !@sticky_bucket_identifier_attributes
107
+ self.attributes = attributes
108
+ self.features = features
73
109
  end
74
110
 
75
111
  def features=(features)
@@ -83,10 +119,14 @@ module Growthbook
83
119
 
84
120
  @features[k.to_s] = v
85
121
  end
122
+
123
+ refresh_sticky_buckets
86
124
  end
87
125
 
88
126
  def attributes=(attrs)
89
127
  @attributes = stringify_keys(attrs || {})
128
+
129
+ refresh_sticky_buckets
90
130
  end
91
131
 
92
132
  def forced_variations=(forced_variations)
@@ -98,6 +138,43 @@ module Growthbook
98
138
  end
99
139
 
100
140
  def eval_feature(key)
141
+ _eval_feature(key, Set.new)
142
+ end
143
+
144
+ def run(exp)
145
+ _run(exp)
146
+ end
147
+
148
+ def on?(key)
149
+ eval_feature(key).on
150
+ end
151
+
152
+ def off?(key)
153
+ eval_feature(key).off
154
+ end
155
+
156
+ def feature_value(key, fallback = nil)
157
+ value = eval_feature(key).value
158
+ value.nil? ? fallback : value
159
+ end
160
+
161
+ private
162
+
163
+ def _eval_prereqs(parent_conditions, stack)
164
+ parent_conditions.each do |parent_condition|
165
+ parent_res = _eval_feature(parent_condition['id'], stack)
166
+
167
+ return 'cyclic' if parent_res.source == 'cyclicPrerequisite'
168
+
169
+ next if parent_condition_passes?(parent_res.value, parent_condition['condition'])
170
+ return 'gate' if parent_condition['gate']
171
+
172
+ return 'fail'
173
+ end
174
+ 'pass'
175
+ end
176
+
177
+ def _eval_feature(key, stack)
101
178
  # Forced in the context
102
179
  return get_feature_result(key.to_s, @forced_features[key.to_s], 'override', nil, nil) if @forced_features.key?(key.to_s)
103
180
 
@@ -105,7 +182,27 @@ module Growthbook
105
182
  feature = get_feature(key)
106
183
  return get_feature_result(key.to_s, nil, 'unknownFeature', nil, nil) unless feature
107
184
 
185
+ return get_feature_result(key.to_s, nil, 'cyclicPrerequisite', nil, nil) if stack.include?(key.to_s)
186
+
187
+ # Work on a copy so sibling prerequisite evaluations don't pollute each
188
+ # other's path (matches the by-value stack semantics of the other SDKs)
189
+ stack = stack.dup
190
+ stack.add(key.to_s)
191
+
108
192
  feature.rules.each do |rule|
193
+ if rule.parent_conditions&.length&.positive?
194
+ prereq_res = _eval_prereqs(rule.parent_conditions, stack)
195
+ case prereq_res
196
+ when 'gate'
197
+ return get_feature_result(key.to_s, nil, 'prerequisite', nil, nil)
198
+ when 'cyclic'
199
+ # Warning already logged in this case
200
+ return get_feature_result(key.to_s, nil, 'cyclicPrerequisite', nil, nil)
201
+ when 'fail'
202
+ next
203
+ end
204
+ end
205
+
109
206
  # Targeting condition
110
207
  next if rule.condition && !condition_passes?(rule.condition)
111
208
 
@@ -119,13 +216,14 @@ module Growthbook
119
216
  included_in_rollout = included_in_rollout?(
120
217
  seed: seed.to_s,
121
218
  hash_attribute: hash_attribute,
219
+ fallback_attribute: rule.fallback_attribute,
122
220
  range: rule.range,
123
221
  coverage: rule.coverage,
124
222
  hash_version: rule.hash_version
125
223
  )
126
224
  next unless included_in_rollout
127
225
 
128
- return get_feature_result(key.to_s, rule.force, 'force', nil, nil)
226
+ return get_feature_result(key.to_s, rule.force, 'force', nil, nil, rule.id)
129
227
  end
130
228
  # Experiment rule
131
229
  next unless rule.experiment?
@@ -137,33 +235,14 @@ module Growthbook
137
235
 
138
236
  next unless result.in_experiment && !result.passthrough
139
237
 
140
- return get_feature_result(key.to_s, result.value, 'experiment', exp, result)
238
+ return get_feature_result(key.to_s, result.value, 'experiment', exp, result, rule.id)
141
239
  end
142
240
 
143
241
  # Fallback
144
242
  get_feature_result(key.to_s, feature.default_value.nil? ? nil : feature.default_value, 'defaultValue', nil, nil)
145
243
  end
146
244
 
147
- def run(exp)
148
- _run(exp)
149
- end
150
-
151
- def on?(key)
152
- eval_feature(key).on
153
- end
154
-
155
- def off?(key)
156
- eval_feature(key).off
157
- end
158
-
159
- def feature_value(key, fallback = nil)
160
- value = eval_feature(key).value
161
- value.nil? ? fallback : value
162
- end
163
-
164
- private
165
-
166
- def _run(exp, feature_id = '')
245
+ def _run(exp, feature_id = '') # rubocop:disable Metrics/AbcSize
167
246
  key = exp.key
168
247
 
169
248
  # 1. If experiment doesn't have enough variations, return immediately
@@ -193,38 +272,70 @@ module Growthbook
193
272
  return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) unless exp.active
194
273
 
195
274
  # 6. Get hash_attribute/value and return if empty
196
- hash_attribute = exp.hash_attribute || 'id'
197
- hash_value = get_attribute(hash_attribute).to_s
275
+ hash_attribute, hash_value_raw = get_hash_attribute(exp.hash_attribute, experiment_fallback_attribute(exp))
276
+ hash_value = hash_string(hash_value_raw)
198
277
  return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) if hash_value.empty?
199
278
 
200
- # 7. Exclude if user is filtered out (used to be called "namespace")
201
- if exp.filters
202
- return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) if filtered_out?(exp.filters || [])
203
- elsif exp.namespace && !Growthbook::Util.in_namespace?(hash_value, exp.namespace)
204
- return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id)
279
+ assigned = -1
280
+
281
+ found_sticky_bucket = false
282
+ sticky_bucket_version_is_blocked = false
283
+ if sticky_bucket_service && !exp.disable_sticky_bucketing
284
+ sticky_bucket = _get_sticky_bucket_variation(
285
+ exp.key,
286
+ exp.bucket_version,
287
+ exp.min_bucket_version,
288
+ exp.meta,
289
+ exp.hash_attribute,
290
+ exp.fallback_attribute
291
+ )
292
+ found_sticky_bucket = sticky_bucket['variation'].to_i >= 0
293
+ assigned = sticky_bucket['variation'].to_i
294
+ sticky_bucket_version_is_blocked = sticky_bucket['versionIsBlocked']
205
295
  end
206
296
 
207
- # 8. Exclude if condition is false
208
- if exp.condition && !condition_passes?(exp.condition)
209
- return get_experiment_result(
210
- exp,
211
- -1,
212
- hash_used: false,
213
- feature_id: feature_id
214
- )
297
+ # Some checks are not needed if we already have a sticky bucket
298
+ unless found_sticky_bucket
299
+ # 7. Exclude if user is filtered out (used to be called "namespace")
300
+ if exp.filters
301
+ return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) if filtered_out?(exp.filters || [])
302
+ elsif exp.namespace && !Growthbook::Util.in_namespace?(hash_value, exp.namespace)
303
+ return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id)
304
+ end
305
+
306
+ # 8. Exclude if condition is false
307
+ if exp.condition && !condition_passes?(exp.condition)
308
+ return get_experiment_result(
309
+ exp,
310
+ -1,
311
+ hash_used: false,
312
+ feature_id: feature_id
313
+ )
314
+ end
315
+
316
+ # 8.01 Exclude if parent conditions are not met
317
+ if exp.parent_conditions
318
+ prereq_res = _eval_prereqs(exp.parent_conditions, Set.new)
319
+ return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) if %w[gate fail cyclic].include?(prereq_res)
320
+ end
215
321
  end
216
322
 
217
323
  # 9. Get bucket ranges and choose variation
218
- ranges = exp.ranges || Growthbook::Util.get_bucket_ranges(
219
- exp.variations.length,
220
- exp.coverage,
221
- exp.weights
222
- )
223
324
  seed = exp.seed || key || ''
224
325
  n = Growthbook::Util.get_hash(seed: seed, value: hash_value, version: exp.hash_version || 1)
225
326
  return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) if n.nil?
226
327
 
227
- assigned = Growthbook::Util.choose_variation(n, ranges)
328
+ unless found_sticky_bucket
329
+ ranges = exp.ranges || Growthbook::Util.get_bucket_ranges(
330
+ exp.variations.length,
331
+ exp.coverage,
332
+ exp.weights
333
+ )
334
+ assigned = Growthbook::Util.choose_variation(n, ranges)
335
+ end
336
+
337
+ # Unenroll if any prior sticky buckets are blocked by version
338
+ return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id, sticky_bucket_used: true) if sticky_bucket_version_is_blocked
228
339
 
229
340
  # 10. Return if not in experiment
230
341
  return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) if assigned.negative?
@@ -236,7 +347,22 @@ module Growthbook
236
347
  return get_experiment_result(exp, -1, hash_used: false, feature_id: feature_id) if @qa_mode
237
348
 
238
349
  # 13. Build the result object
239
- result = get_experiment_result(exp, assigned, hash_used: true, feature_id: feature_id, bucket: n)
350
+ result = get_experiment_result(exp, assigned, hash_used: true, feature_id: feature_id, bucket: n, sticky_bucket_used: found_sticky_bucket)
351
+
352
+ # 13.5 Persist sticky bucket
353
+ if sticky_bucket_service && !exp.disable_sticky_bucketing
354
+ assignment = {
355
+ _get_sticky_bucket_experiment_key(exp.key, exp.bucket_version) => result.key.to_s
356
+ }
357
+
358
+ data = _generate_sticky_bucket_assignment_doc(hash_attribute, hash_value, assignment)
359
+ doc = data['doc']
360
+ if doc && data['changed']
361
+ @sticky_bucket_assignment_docs ||= {}
362
+ @sticky_bucket_assignment_docs[data['key']] = doc
363
+ sticky_bucket_service.save_assignments(doc)
364
+ end
365
+ end
240
366
 
241
367
  # 14. Fire tracking callback
242
368
  track_experiment(exp, result)
@@ -253,21 +379,30 @@ module Growthbook
253
379
  new_hash
254
380
  end
255
381
 
382
+ # A malformed prerequisite condition never matches, rather than raising into the app
383
+ def parent_condition_passes?(parent_value, condition)
384
+ Growthbook::Conditions.eval_condition({ 'value' => parent_value }, condition || {}, @saved_groups)
385
+ rescue StandardError
386
+ false
387
+ end
388
+
389
+ # A condition that raises, such as a malformed one, fails rather than breaking evaluation
256
390
  def condition_passes?(condition)
257
391
  return false if condition.nil?
258
392
 
259
- Growthbook::Conditions.eval_condition(@attributes, condition)
393
+ Growthbook::Conditions.eval_condition(@attributes, condition, @saved_groups)
394
+ rescue StandardError
395
+ false
260
396
  end
261
397
 
262
- def get_experiment_result(experiment, variation_index = -1, hash_used: false, feature_id: '', bucket: nil)
398
+ def get_experiment_result(experiment, variation_index = -1, hash_used: false, feature_id: '', bucket: nil, sticky_bucket_used: false)
263
399
  in_experiment = true
264
400
  if variation_index.negative? || variation_index >= experiment.variations.length
265
401
  variation_index = 0
266
402
  in_experiment = false
267
403
  end
268
404
 
269
- hash_attribute = experiment.hash_attribute || 'id'
270
- hash_value = get_attribute(hash_attribute)
405
+ hash_attribute, hash_value = get_hash_attribute(experiment.hash_attribute, experiment_fallback_attribute(experiment))
271
406
  meta = experiment.meta ? experiment.meta[variation_index] : {}
272
407
 
273
408
  result = Growthbook::InlineExperimentResult.new(
@@ -281,7 +416,8 @@ module Growthbook
281
416
  hash_value: hash_value,
282
417
  feature_id: feature_id,
283
418
  bucket: bucket,
284
- name: meta['name']
419
+ name: meta['name'],
420
+ sticky_bucket_used: sticky_bucket_used
285
421
  }
286
422
  )
287
423
 
@@ -290,8 +426,8 @@ module Growthbook
290
426
  result
291
427
  end
292
428
 
293
- def get_feature_result(key, value, source, experiment, experiment_result)
294
- res = Growthbook::FeatureResult.new(value, source, experiment, experiment_result)
429
+ def get_feature_result(key, value, source, experiment, experiment_result, rule_id = '')
430
+ res = Growthbook::FeatureResult.new(value, source, experiment, experiment_result, rule_id)
295
431
 
296
432
  track_feature_usage(key, res)
297
433
 
@@ -312,7 +448,48 @@ module Growthbook
312
448
  nil
313
449
  end
314
450
 
451
+ # Returns the hash attribute and its raw value ('' when missing). Like JS,
452
+ # only a non-empty string or a finite, non-zero number identifies a user;
453
+ # anything else counts as missing, and then the fallback attribute is tried
454
+ # with the same rule.
455
+ def get_hash_attribute(attr, fallback_attr)
456
+ attr = 'id' if attr.nil? || attr == ''
457
+
458
+ val = get_attribute(attr)
459
+ val = '' unless hashable?(val)
460
+
461
+ # If no match, try fallback
462
+ if val == '' && fallback_attr && hashable?(get_attribute(fallback_attr))
463
+ val = get_attribute(fallback_attr)
464
+ attr = fallback_attr
465
+ end
466
+
467
+ [attr, val]
468
+ end
469
+
470
+ def hashable?(value)
471
+ return value != '' if value.is_a?(String)
472
+ return value != 0 && value.to_f.finite? if value.is_a?(Integer) || value.is_a?(Float)
473
+
474
+ false
475
+ end
476
+
477
+ # Formats a hash value like JS String(): 1.0 as "1"
478
+ def hash_string(value)
479
+ return value if value.is_a?(String)
480
+ return Growthbook::Util.js_number_to_s(value) if value.is_a?(Integer) || value.is_a?(Float)
481
+
482
+ ''
483
+ end
484
+
485
+ # Experiments only use their fallback attribute with sticky bucketing, like JS
486
+ def experiment_fallback_attribute(experiment)
487
+ experiment.fallback_attribute if @sticky_bucket_service && !experiment.disable_sticky_bucketing
488
+ end
489
+
315
490
  def get_attribute(key)
491
+ return '' if key.nil?
492
+
316
493
  return @attributes[key.to_sym] if @attributes.key?(key.to_sym)
317
494
  return @attributes[key.to_s] if @attributes.key?(key.to_s)
318
495
 
@@ -326,10 +503,13 @@ module Growthbook
326
503
  @impressions[experiment.key] = result unless experiment.key.nil?
327
504
  end
328
505
 
329
- def included_in_rollout?(seed:, hash_attribute:, hash_version:, range:, coverage:)
506
+ def included_in_rollout?(seed:, hash_attribute:, fallback_attribute:, hash_version:, range:, coverage:)
330
507
  return true if range.nil? && coverage.nil?
508
+ return false if range.nil? && coverage&.zero?
509
+
510
+ _, hash_value_raw = get_hash_attribute(hash_attribute, fallback_attribute)
331
511
 
332
- hash_value = get_attribute(hash_attribute).to_s
512
+ hash_value = hash_string(hash_value_raw)
333
513
 
334
514
  return false if hash_value.empty?
335
515
 
@@ -337,17 +517,20 @@ module Growthbook
337
517
  return false if n.nil?
338
518
 
339
519
  return Growthbook::Util.in_range?(n, range) if range
340
- return n <= coverage if coverage
520
+ # Half-open, like in_range?, so a coverage of k/10000 includes exactly k hash buckets
521
+ return n < coverage if coverage
341
522
 
342
523
  true
343
524
  end
344
525
 
345
526
  def filtered_out?(filters)
346
527
  filters.any? do |filter|
347
- hash_value = get_attribute(filter['attribute'] || 'id').to_s
528
+ _, hash_value_raw = get_hash_attribute(filter['attribute'], nil)
529
+ hash_value = hash_string(hash_value_raw)
348
530
 
531
+ # Like JS, a user without a hash value is filtered out
349
532
  if hash_value.empty?
350
- false
533
+ true
351
534
  else
352
535
  n = Growthbook::Util.get_hash(seed: filter['seed'] || '', value: hash_value, version: filter['hashVersion'] || 2)
353
536
 
@@ -367,5 +550,124 @@ module Growthbook
367
550
  rescue StandardError
368
551
  nil
369
552
  end
553
+
554
+ def _derive_sticky_bucket_identifier_attributes
555
+ attributes = Set.new
556
+ @features.each do |_key, feature|
557
+ feature.rules.each do |rule|
558
+ if rule.variations
559
+ attributes.add(rule.hash_attribute || 'id')
560
+ attributes.add(rule.fallback_attribute) if rule.fallback_attribute
561
+ end
562
+ end
563
+ end
564
+ attributes.to_a
565
+ end
566
+
567
+ def _get_sticky_bucket_attributes
568
+ attributes = {}
569
+ @sticky_bucket_identifier_attributes = _derive_sticky_bucket_identifier_attributes if @using_derived_sticky_bucket_attributes
570
+
571
+ return attributes unless @sticky_bucket_identifier_attributes
572
+
573
+ @sticky_bucket_identifier_attributes.each do |attr|
574
+ _, hash_value = get_hash_attribute(attr, nil)
575
+ attributes[attr] = hash_string(hash_value)
576
+ end
577
+ attributes
578
+ end
579
+
580
+ def _get_sticky_bucket_assignments(attr = nil, fallback = nil)
581
+ merged = {}
582
+
583
+ attr, hash_value = get_hash_attribute(attr, nil)
584
+ key = "#{attr}||#{hash_string(hash_value)}"
585
+ merged = @sticky_bucket_assignment_docs[key]['assignments'] if @sticky_bucket_assignment_docs.key?(key)
586
+
587
+ if fallback
588
+ _, hash_value = get_hash_attribute(fallback, nil)
589
+ key = "#{fallback}||#{hash_string(hash_value)}"
590
+ if @sticky_bucket_assignment_docs.key?(key)
591
+ @sticky_bucket_assignment_docs[key]['assignments'].each do |k, v|
592
+ merged[k] = v unless merged.key?(k)
593
+ end
594
+ end
595
+ end
596
+
597
+ merged
598
+ end
599
+
600
+ def _is_blocked(assignments, experiment_key, min_bucket_version)
601
+ return false if min_bucket_version.zero?
602
+
603
+ (0...min_bucket_version).each do |i|
604
+ blocked_key = _get_sticky_bucket_experiment_key(experiment_key, i)
605
+ return true if assignments.key?(blocked_key)
606
+ end
607
+ false
608
+ end
609
+
610
+ def _get_sticky_bucket_variation(experiment_key, bucket_version = nil, min_bucket_version = nil, meta = nil, hash_attribute = nil, fallback_attribute = nil)
611
+ bucket_version ||= 0
612
+ min_bucket_version ||= 0
613
+ meta ||= []
614
+
615
+ id = _get_sticky_bucket_experiment_key(experiment_key, bucket_version)
616
+
617
+ assignments = _get_sticky_bucket_assignments(hash_attribute, fallback_attribute)
618
+ if _is_blocked(assignments, experiment_key, min_bucket_version)
619
+ return {
620
+ 'variation' => -1,
621
+ 'versionIsBlocked' => true
622
+ }
623
+ end
624
+
625
+ variation_key = assignments[id]
626
+ return { 'variation' => -1 } unless variation_key
627
+
628
+ variation = meta.find_index { |v| v['key'] == variation_key } || -1
629
+ return { 'variation' => -1 } if variation.negative?
630
+
631
+ { 'variation' => variation }
632
+ end
633
+
634
+ def _get_sticky_bucket_experiment_key(experiment_key, bucket_version = 0)
635
+ "#{experiment_key}__#{bucket_version}"
636
+ end
637
+
638
+ def refresh_sticky_buckets(force: false)
639
+ return unless @sticky_bucket_service
640
+
641
+ attributes = _get_sticky_bucket_attributes
642
+ return if !force && attributes == @sticky_bucket_attributes
643
+
644
+ @sticky_bucket_attributes = attributes
645
+ @sticky_bucket_assignment_docs = @sticky_bucket_service.get_all_assignments(attributes)
646
+ end
647
+
648
+ def _generate_sticky_bucket_assignment_doc(attribute_name, attribute_value, assignments)
649
+ key = "#{attribute_name}||#{attribute_value}"
650
+ existing_assignments = @sticky_bucket_assignment_docs[key]&.fetch('assignments', {})
651
+
652
+ if existing_assignments
653
+ new_assignments = existing_assignments.merge(assignments)
654
+ existing_json = existing_assignments.to_json
655
+ new_json = new_assignments.to_json
656
+ changed = existing_json != new_json
657
+ else
658
+ changed = true
659
+ new_assignments = assignments
660
+ end
661
+
662
+ {
663
+ 'key' => key,
664
+ 'doc' => {
665
+ 'attributeName' => attribute_name,
666
+ 'attributeValue' => attribute_value,
667
+ 'assignments' => new_assignments
668
+ },
669
+ 'changed' => changed
670
+ }
671
+ end
370
672
  end
371
673
  end
@@ -15,10 +15,14 @@ module Growthbook
15
15
  # Parsed features JSON
16
16
  attr_reader :features_json
17
17
 
18
+ # Parsed saved groups JSON (used by the $inGroup/$notInGroup operators)
19
+ attr_reader :saved_groups_json
20
+
18
21
  def initialize(endpoint:, decryption_key:)
19
22
  @endpoint = endpoint
20
23
  @decryption_key = decryption_key
21
24
  @features_json = {}
25
+ @saved_groups_json = {}
22
26
  end
23
27
 
24
28
  def fetch
@@ -56,7 +60,11 @@ module Growthbook
56
60
  end
57
61
 
58
62
  def parsed_plain_text_response
59
- @features_json = parsed_response['features'] unless parsed_response.nil?
63
+ parsed = parsed_response
64
+ return nil if parsed.nil?
65
+
66
+ @saved_groups_json = parsed['savedGroups'] || {}
67
+ @features_json = parsed['features']
60
68
  rescue StandardError
61
69
  nil
62
70
  end
@@ -65,12 +73,26 @@ module Growthbook
65
73
  k = decryption_key
66
74
  return nil if k.nil?
67
75
 
68
- decrypted_str = Growthbook::DecryptionUtil.decrypt(parsed_response['encryptedFeatures'], key: k)
76
+ parsed = parsed_response
77
+ return nil if parsed.nil?
78
+
79
+ @saved_groups_json = decrypted_saved_groups(parsed, k)
80
+
81
+ decrypted_str = Growthbook::DecryptionUtil.decrypt(parsed['encryptedFeatures'], key: k)
69
82
  @features_json = JSON.parse(decrypted_str) unless decrypted_str.nil?
70
83
  rescue StandardError
71
84
  nil
72
85
  end
73
86
 
87
+ def decrypted_saved_groups(parsed, key)
88
+ if parsed['encryptedSavedGroups']
89
+ decrypted = Growthbook::DecryptionUtil.decrypt(parsed['encryptedSavedGroups'], key: key)
90
+ return JSON.parse(decrypted) unless decrypted.nil?
91
+ end
92
+
93
+ parsed['savedGroups'] || {}
94
+ end
95
+
74
96
  def parsed_response
75
97
  res = response
76
98
  return {} if res.nil?