amplitude-experiment 1.7.1 → 1.8.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: 6562a2b13fb38d240e3d83762e442a558478db59f3fff36bdff10b6589cfab0d
4
- data.tar.gz: 432ea0ff64724c882ad97aae3ce95ae8d8a203f9c90bfe34afd5629e00dbba28
3
+ metadata.gz: f40750aa31cc9abd90da453ed66d9c8af7ab7732d4887b248e47b9a878a159a7
4
+ data.tar.gz: 87064809c68b48a71ec4769f36fb797fc6757b763626ceb9e6a5e5a1d5656049
5
5
  SHA512:
6
- metadata.gz: 507368b7f4db8a28dfc866e0df203f724a1d2911af19dab24fada0bb7327e362d185057c891d534421c7816ec12953ad6555a0252b9bfa7d4922ffaea16bc935
7
- data.tar.gz: d7ad16548702c918919a9d3b4afe874d396c8c5e98fb5010ebb3f8c0f0b476ad86d4568ed4660a74040a5ee85d4a9e01197560039b19d94659ec0e38d67b0868
6
+ metadata.gz: c235e7a780773a4ea5873a1139917fdb3929b4bb27139a02a960f040c863226cc01f0ac3188fc4628c45897d2823c41ba383dc1d9f517f15b69c671d971b6397
7
+ data.tar.gz: cd4d84c2d088dfc265910c7a57147f9d54ef34d747959d172ff1623b21f96b85f1ff409bdde06900aedec6a15941e19b2a0d83da5f0ff8d8e09b4dc1027f08d4
@@ -6,6 +6,7 @@ require 'experiment/user'
6
6
  require 'experiment/variant'
7
7
  require 'experiment/factory'
8
8
  require 'experiment/remote/client'
9
+ require 'experiment/remote/fetch_options'
9
10
  require 'experiment/local/client'
10
11
  require 'experiment/local/config'
11
12
  require 'experiment/local/assignment/assignment'
@@ -25,7 +25,7 @@ module AmplitudeExperiment
25
25
  # @param [User] user
26
26
  # @return [Hash] Variants Hash
27
27
  def fetch(user)
28
- AmplitudeExperiment.filter_default_variants(fetch_internal(user))
28
+ AmplitudeExperiment.filter_default_variants(fetch_internal(user, nil))
29
29
  rescue StandardError => e
30
30
  @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
31
31
  {}
@@ -36,9 +36,10 @@ module AmplitudeExperiment
36
36
  # This method will automatically retry if configured (default). This function differs from fetch as it will
37
37
  # return a default variant object if the flag was evaluated but the user was not assigned (i.e. off).
38
38
  # @param [User] user
39
+ # @param [FetchOptions] fetch_options
39
40
  # @return [Hash] Variants Hash
40
- def fetch_v2(user)
41
- fetch_internal(user)
41
+ def fetch_v2(user, fetch_options = nil)
42
+ fetch_internal(user, fetch_options)
42
43
  rescue StandardError => e
43
44
  @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
44
45
  {}
@@ -51,7 +52,7 @@ module AmplitudeExperiment
51
52
  # @yield [User, Hash] callback block takes user object and variants hash
52
53
  def fetch_async(user, &callback)
53
54
  Thread.new do
54
- variants = fetch_internal(user)
55
+ variants = AmplitudeExperiment.filter_default_variants(fetch_internal(user, nil))
55
56
  yield(user, variants) unless callback.nil?
56
57
  variants
57
58
  rescue StandardError => e
@@ -67,10 +68,10 @@ module AmplitudeExperiment
67
68
  # This method will automatically retry if configured (default).
68
69
  # @param [User] user
69
70
  # @yield [User, Hash] callback block takes user object and variants hash
70
- def fetch_async_v2(user, &callback)
71
+ def fetch_async_v2(user, fetch_options = nil, &callback)
71
72
  Thread.new do
72
- variants = fetch_internal(user)
73
- yield(user, filter_default_variants(variants)) unless callback.nil?
73
+ variants = fetch_internal(user, fetch_options)
74
+ yield(user, variants) unless callback.nil?
74
75
  variants
75
76
  rescue StandardError => e
76
77
  @logger.error("[Experiment] Failed to fetch variants: #{e.message}")
@@ -82,14 +83,15 @@ module AmplitudeExperiment
82
83
  private
83
84
 
84
85
  # @param [User] user
85
- def fetch_internal(user)
86
+ # @param [FetchOptions] fetch_options
87
+ def fetch_internal(user, fetch_options)
86
88
  @logger.debug("[Experiment] Fetching variants for user: #{user.as_json}")
87
- do_fetch(user, @config.connect_timeout_millis, @config.fetch_timeout_millis)
89
+ do_fetch(user, fetch_options, @config.connect_timeout_millis, @config.fetch_timeout_millis)
88
90
  rescue StandardError => e
89
91
  @logger.error("[Experiment] Fetch failed: #{e.message}")
90
92
  if should_retry_fetch?(e)
91
93
  begin
92
- retry_fetch(user)
94
+ retry_fetch(user, fetch_options)
93
95
  rescue StandardError => err
94
96
  @logger.error("[Experiment] Retry Fetch failed: #{err.message}")
95
97
  end
@@ -98,7 +100,8 @@ module AmplitudeExperiment
98
100
  end
99
101
 
100
102
  # @param [User] user
101
- def retry_fetch(user)
103
+ # @param [FetchOptions] fetch_options
104
+ def retry_fetch(user, fetch_options)
102
105
  return {} if @config.fetch_retries.zero?
103
106
 
104
107
  @logger.debug('[Experiment] Retrying fetch')
@@ -107,7 +110,7 @@ module AmplitudeExperiment
107
110
  @config.fetch_retries.times do
108
111
  sleep(delay_millis.to_f / 1000.0)
109
112
  begin
110
- return do_fetch(user, @config.connect_timeout_millis, @config.fetch_retry_timeout_millis)
113
+ return do_fetch(user, fetch_options, @config.connect_timeout_millis, @config.fetch_retry_timeout_millis)
111
114
  rescue StandardError => e
112
115
  @logger.error("[Experiment] Retry failed: #{e.message}")
113
116
  err = e
@@ -118,15 +121,24 @@ module AmplitudeExperiment
118
121
  end
119
122
 
120
123
  # @param [User] user
124
+ # @param [FetchOptions] fetch_options
121
125
  # @param [Integer] connect_timeout_millis
122
126
  # @param [Integer] fetch_timeout_millis
123
- def do_fetch(user, connect_timeout_millis, fetch_timeout_millis)
127
+ def do_fetch(user, fetch_options, connect_timeout_millis, fetch_timeout_millis)
124
128
  start_time = Time.now
125
129
  user_context = add_context(user)
126
130
  headers = {
127
131
  'Authorization' => "Api-Key #{@api_key}",
128
132
  'Content-Type' => 'application/json;charset=utf-8'
129
133
  }
134
+ unless fetch_options.nil?
135
+ unless fetch_options.tracks_assignment.nil?
136
+ headers['X-Amp-Exp-Track'] = fetch_options.tracks_assignment ? 'track' : 'no-track'
137
+ end
138
+ unless fetch_options.tracks_exposure.nil?
139
+ headers['X-Amp-Exp-Exposure-Track'] = fetch_options.tracks_exposure ? 'track' : 'no-track'
140
+ end
141
+ end
130
142
  connect_timeout = connect_timeout_millis.to_f / 1000 if (connect_timeout_millis.to_f / 1000) > 0
131
143
  read_timeout = fetch_timeout_millis.to_f / 1000 if (fetch_timeout_millis.to_f / 1000) > 0
132
144
  http = PersistentHttpClient.get(@uri, { open_timeout: connect_timeout, read_timeout: read_timeout }, @api_key)
@@ -0,0 +1,19 @@
1
+ module AmplitudeExperiment
2
+ # Fetch options
3
+ class FetchOptions
4
+ # Whether to track assignment events.
5
+ # If not provided, the default is null, which will use server default (to track assignment events).
6
+ # @return [Boolean, nil] the value of tracks_assignment
7
+ attr_accessor :tracks_assignment
8
+
9
+ # Whether to track exposure events.
10
+ # If not provided, the default is null, which will use server default (to not track exposure events).
11
+ # @return [Boolean, nil] the value of tracks_exposure
12
+ attr_accessor :tracks_exposure
13
+
14
+ def initialize(tracks_assignment: nil, tracks_exposure: nil)
15
+ @tracks_assignment = tracks_assignment
16
+ @tracks_exposure = tracks_exposure
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module AmplitudeExperiment
2
- VERSION = '1.7.1'.freeze
2
+ VERSION = '1.8.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amplitude-experiment
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amplitude
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-12-04 00:00:00.000000000 Z
11
+ date: 2025-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -216,6 +216,7 @@ files:
216
216
  - lib/experiment/persistent_http_client.rb
217
217
  - lib/experiment/remote/client.rb
218
218
  - lib/experiment/remote/config.rb
219
+ - lib/experiment/remote/fetch_options.rb
219
220
  - lib/experiment/user.rb
220
221
  - lib/experiment/util/flag_config.rb
221
222
  - lib/experiment/util/hash.rb