mixpanel-ruby 3.4.0 → 3.5.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: c087578749eaaa76591b16c382aef85e38e276501c39c8a82e5ff155e2572747
4
- data.tar.gz: edc867253e2a32dafa92dc32cc318d6aab740de9a776955715eef42972810b90
3
+ metadata.gz: b34915624a379f24601a62eab94dc46c75ef297383157081a9e58e7368dbc237
4
+ data.tar.gz: 368e6ff3920627844c1d9df24efab69979444950f4b254f3c161d8123d9b7f91
5
5
  SHA512:
6
- metadata.gz: a635485dfad21d6c20c7beafb831bd27af2333ca59b2cf3e04e3be6fd09dbc506c4ac67b160415a6d70646e9068290ffdcd6160453dee0b88ff4a1ec799e7253
7
- data.tar.gz: 173b6b1e59c299c5ddbf3c36f02ef64893388d55ea94671fb4da15a8e636e7bc2c943cf4b5f9c81a941f20a73e3c97cd508fd681d21dca85b25b94b3f612514f
6
+ metadata.gz: 59cd675026bc735a00230cfbe42551ebafabfb603b31f2ee5464335a15bdab8710da8d10c9c47ad6fe6f1c522b4847fe8b1cb55b936c3c323ecab865cc248017
7
+ data.tar.gz: 8ff8e085d684765c4e76953663913f467bedf6cd2cba8b640c88860dbaaca7aa1e863720e8f19fa1fbc896fa8a4df45f0979a241e051f0e8941e49a9bef5193f
@@ -104,7 +104,7 @@ jobs:
104
104
  echo "Version confirmed: $FILE_VERSION"
105
105
 
106
106
  - name: Set up Ruby
107
- uses: ruby/setup-ruby@d697be2f83c6234b20877c3b5eac7a7f342f0d0c # v1.269.0
107
+ uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
108
108
  with:
109
109
  ruby-version: '3.3'
110
110
  bundler-cache: false
@@ -11,8 +11,11 @@ permissions:
11
11
  jobs:
12
12
  test:
13
13
 
14
- runs-on: ubuntu-latest
14
+ # Pinned to 24.04: ubuntu-latest (26.04) only has prebuilt CRuby >= 3.2,
15
+ # and this matrix still covers 3.0/3.1.
16
+ runs-on: ubuntu-24.04
15
17
  strategy:
18
+ fail-fast: false
16
19
  matrix:
17
20
  ruby-version:
18
21
  - '3.0'
@@ -24,7 +27,7 @@ jobs:
24
27
  steps:
25
28
  - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
26
29
  - name: Set up Ruby
27
- uses: ruby/setup-ruby@d697be2f83c6234b20877c3b5eac7a7f342f0d0c # v1.269.0
30
+ uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
28
31
  with:
29
32
  ruby-version: ${{ matrix.ruby-version }}
30
33
  bundler-cache: true # runs 'bundle install' and caches installed gems automatically
@@ -38,8 +41,11 @@ jobs:
38
41
 
39
42
  test-openfeature:
40
43
 
41
- runs-on: ubuntu-latest
44
+ # Pinned to 24.04: ubuntu-latest (26.04) only has prebuilt CRuby >= 3.2,
45
+ # and this matrix still covers 3.1.
46
+ runs-on: ubuntu-24.04
42
47
  strategy:
48
+ fail-fast: false
43
49
  matrix:
44
50
  ruby-version:
45
51
  - '3.1'
@@ -50,7 +56,7 @@ jobs:
50
56
  steps:
51
57
  - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
52
58
  - name: Set up Ruby
53
- uses: ruby/setup-ruby@d697be2f83c6234b20877c3b5eac7a7f342f0d0c # v1.269.0
59
+ uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
54
60
  with:
55
61
  ruby-version: ${{ matrix.ruby-version }}
56
62
  - name: Install dependencies
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [v3.5.0](https://github.com/mixpanel/mixpanel-ruby/tree/v3.5.0) (2026-11-23)
4
+
5
+ ### Features
6
+ - tag OpenFeature exposures with $openfeature_provider (SDK-448) ([#181](https://github.com/mixpanel/mixpanel-ruby/pull/181))
7
+
8
+ ### Fixes
9
+ - surface failed imports instead of failing silently (SDK-395) ([#177](https://github.com/mixpanel/mixpanel-ruby/pull/177))
10
+ - bump json-logic-rb minimum to 0.3 ([#175](https://github.com/mixpanel/mixpanel-ruby/pull/175))
11
+
12
+ ### Chores
13
+ - explain which credential import needs (SDK-388) ([#179](https://github.com/mixpanel/mixpanel-ruby/pull/179))
14
+
15
+ [Full Changelog](https://github.com/mixpanel/mixpanel-ruby/compare/v3.4.0...v3.5.0)
16
+
3
17
  ## [v3.4.0](https://github.com/mixpanel/mixpanel-ruby/tree/v3.4.0) (2026-09-01)
4
18
 
5
19
  ### Features
data/Readme.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = mixpanel-ruby: The official Mixpanel Ruby library
2
2
 
3
- ##### _September 01, 2026_ - [v3.4.0](https://github.com/mixpanel/mixpanel-ruby/releases/tag/v3.4.0)
3
+ ##### _November 23, 2026_ - [v3.5.0](https://github.com/mixpanel/mixpanel-ruby/releases/tag/v3.5.0)
4
4
 
5
5
  mixpanel-ruby is a library for tracking events and sending \Mixpanel profile
6
6
  updates to \Mixpanel from your ruby applications.
@@ -34,6 +34,23 @@ right away.
34
34
  Service accounts provide secure server-to-server authentication and are recommended over
35
35
  API keys for import operations and feature flags.
36
36
 
37
+ === Which credential do I need?
38
+
39
+ \Mixpanel uses different credentials for different calls, and they are not interchangeable:
40
+
41
+ [Project token]
42
+ Passed to Mixpanel::Tracker.new and used for track, people and groups calls. It is
43
+ public (it ships in client-side code) and does not authenticate import.
44
+ [Service account username and secret, plus project ID]
45
+ Required for import_events (and import without an API key). Create a service account in your \Mixpanel
46
+ organization settings
47
+ (see https://docs.mixpanel.com/docs/orgs-and-projects/service-accounts), then pass it
48
+ to Mixpanel::Tracker.new as a Mixpanel::ServiceAccountCredentials. The project ID is
49
+ shown in your project settings.
50
+ [API key]
51
+ The legacy credential taken by <tt>import("API_KEY", ...)</tt>. It is not your project
52
+ token. See "Import with API Key (deprecated)" below.
53
+
37
54
  === Import with Service Account (Recommended)
38
55
 
39
56
  require 'mixpanel-ruby'
@@ -61,14 +78,28 @@ API keys for import operations and feature flags.
61
78
  'Source' => 'Import'
62
79
  })
63
80
 
64
- === Import with API Key (Legacy)
81
+ import_events returns true when the request completed without a \Mixpanel error, and false
82
+ when a Mixpanel::MixpanelError was raised. With a custom consumer block, such as a queue, true
83
+ only means the handoff to that block succeeded. A false result does not always mean the event
84
+ was lost: after a timeout, \Mixpanel may still have stored it, so set a unique
85
+ <tt>$insert_id</tt> property if you retry, and \Mixpanel will drop the duplicate.
65
86
 
66
- # Legacy approach with API key string
67
- tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
68
- tracker.import("API_KEY", 'User1', 'Past Event', {
69
- 'time' => 1369353600,
70
- 'Source' => 'Import'
71
- })
87
+ With the default error handler, a failed import also prints a warning to stderr. To handle
88
+ failures yourself, for example to retry or alert, pass a Mixpanel::ErrorHandler subclass:
89
+
90
+ class ImportErrorHandler < Mixpanel::ErrorHandler
91
+ def handle(error)
92
+ Rails.logger.error("Mixpanel import failed: #{error.message}")
93
+ end
94
+ end
95
+
96
+ tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN, ImportErrorHandler.new, credentials: credentials)
97
+
98
+ === Import with API Key (deprecated)
99
+
100
+ Passing an API key as the first argument to import, as in
101
+ <tt>tracker.import("API_KEY", ...)</tt>, is deprecated, and \Mixpanel's /import endpoint
102
+ may reject these requests. Use service account credentials with import_events, as shown above.
72
103
 
73
104
  === Feature Flags with Service Account
74
105
 
@@ -107,7 +107,7 @@ module Mixpanel
107
107
  def import(api_key, distinct_id, event, properties={}, ip=nil)
108
108
  # Warn about deprecated API key usage
109
109
  if api_key && !api_key.to_s.empty?
110
- warn '[DEPRECATION] Passing api_key to import() is deprecated. Use ServiceAccountCredentials in the constructor instead. See https://docs.mixpanel.com/docs/tracking-methods/sdks/ruby#service-account-authentication'
110
+ warn '[DEPRECATION] Passing api_key to import() is deprecated and may be rejected by Mixpanel\'s /import endpoint. Use ServiceAccountCredentials in the constructor with import_events() instead. See https://docs.mixpanel.com/docs/tracking-methods/sdks/ruby#service-account-authentication'
111
111
  end
112
112
 
113
113
  # Warn when using import(nil, ...) - recommend import_events instead
@@ -181,6 +181,21 @@ module Mixpanel
181
181
  begin
182
182
  @sink.call(:import, message.to_json)
183
183
  rescue MixpanelError => e
184
+ # The default ErrorHandler silences all errors, which makes a rejected
185
+ # import (for example a 401 from bad credentials) indistinguishable from
186
+ # a successful one unless the caller checks the return value. Print the
187
+ # first failure per tracker to stderr when no custom handler is configured
188
+ # (once only, so a failing backfill doesn't flood stderr).
189
+ if @error_handler.instance_of?(ErrorHandler) && !@import_failure_warned
190
+ @import_failure_warned = true
191
+ # The outcome is not always known: a read timeout, a 5xx, or an unreadable
192
+ # 200 response can all follow a successful write. Don't claim the event was
193
+ # rejected; the message carries the status and server reply when there is one.
194
+ detail = e.message.to_s.gsub(/\s+/, ' ').strip[0, 200].chomp('.')
195
+ warn "[Mixpanel] import failed; the event may not have been stored: #{detail}. " \
196
+ 'Further import failures will not be printed. ' \
197
+ 'Pass a Mixpanel::ErrorHandler to Tracker.new to handle import errors yourself.'
198
+ end
184
199
  @error_handler.handle(e)
185
200
  ret = false
186
201
  end
@@ -126,6 +126,11 @@ module Mixpanel
126
126
  properties['$experiment_id'] = selected_variant.experiment_id if selected_variant.experiment_id
127
127
  properties['$is_experiment_active'] = selected_variant.is_experiment_active unless selected_variant.is_experiment_active.nil?
128
128
  properties['$is_qa_tester'] = selected_variant.is_qa_tester unless selected_variant.is_qa_tester.nil?
129
+ # SDK-448: the OpenFeature provider marks its evaluation context so
130
+ # exposures it triggers can be told apart from direct SDK calls.
131
+ if context['$openfeature_provider'] == true || context[:'$openfeature_provider'] == true
132
+ properties['$openfeature_provider'] = true
133
+ end
129
134
 
130
135
  dispatch_exposure(distinct_id, properties)
131
136
  end
@@ -127,21 +127,25 @@ module Mixpanel
127
127
  # as a Hash with string keys and strings, numbers or booleans as
128
128
  # values.
129
129
  #
130
- # tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
131
- #
132
- # # Using deprecated API key (still supported)
133
- # tracker.import("API_KEY", "12345", "Credit Card Declined", {
134
- # 'time' => 1310111365
135
- # })
130
+ # Prefer #import_events, which takes the same arguments without the
131
+ # leading api_key. Import is authenticated with service account
132
+ # credentials passed to the constructor; the project token alone is
133
+ # not enough.
136
134
  #
137
135
  # # Using service account credentials (recommended)
138
136
  # credentials = Mixpanel::ServiceAccountCredentials.new(username, secret, project_id)
139
137
  # tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN, credentials: credentials)
140
- # tracker.import(nil, "12345", "Welcome Email Sent", {
138
+ # tracker.import_events("12345", "Welcome Email Sent", {
141
139
  # 'Email Template' => 'Pretty Pink Welcome',
142
140
  # 'User Sign-up Cohort' => 'July 2013',
143
141
  # 'time' => 1310111365
144
142
  # })
143
+ #
144
+ # # Deprecated: legacy API key (not your project token). Avoid in new code.
145
+ # tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
146
+ # tracker.import("API_KEY", "12345", "Credit Card Declined", {
147
+ # 'time' => 1310111365
148
+ # })
145
149
  def import(api_key, distinct_id, event, properties={}, ip=nil)
146
150
  # This is here strictly to allow rdoc to include the relevant
147
151
  # documentation
@@ -1,3 +1,3 @@
1
1
  module Mixpanel
2
- VERSION = '3.4.0'
2
+ VERSION = '3.5.0'
3
3
  end
@@ -15,7 +15,7 @@ spec = Gem::Specification.new do |spec|
15
15
  spec.required_ruby_version = '>= 3.0.0'
16
16
  spec.add_runtime_dependency 'mutex_m'
17
17
  spec.add_runtime_dependency "base64"
18
- spec.add_runtime_dependency 'json-logic-rb', '~> 0.2'
18
+ spec.add_runtime_dependency 'json-logic-rb', '~> 0.3'
19
19
 
20
20
  spec.add_development_dependency 'activesupport', '~> 4.0'
21
21
  spec.add_development_dependency 'rake', '~> 13'
@@ -150,15 +150,19 @@ module Mixpanel
150
150
  end
151
151
 
152
152
  def build_context(evaluation_context)
153
- return {} if evaluation_context.nil?
154
-
155
153
  ctx = {}
156
- if evaluation_context.respond_to?(:fields)
157
- evaluation_context.fields.each { |k, v| ctx[k] = unwrap_value(v) }
158
- end
159
- if evaluation_context.respond_to?(:targeting_key) && evaluation_context.targeting_key
160
- ctx['targetingKey'] = unwrap_value(evaluation_context.targeting_key)
154
+ unless evaluation_context.nil?
155
+ if evaluation_context.respond_to?(:fields)
156
+ evaluation_context.fields.each { |k, v| ctx[k] = unwrap_value(v) }
157
+ end
158
+ if evaluation_context.respond_to?(:targeting_key) && evaluation_context.targeting_key
159
+ ctx['targetingKey'] = unwrap_value(evaluation_context.targeting_key)
160
+ end
161
161
  end
162
+ # SDK-448: marks exposures as coming from the OpenFeature provider. The
163
+ # SDK copies this onto `$experiment_started`; older SDKs ignore the key.
164
+ # Set last so a customer attribute of the same name can't override it.
165
+ ctx['$openfeature_provider'] = true
162
166
  ctx
163
167
  end
164
168
 
@@ -467,7 +467,7 @@ RSpec.describe Mixpanel::OpenFeature::Provider do
467
467
  targeting_key: nil
468
468
  )
469
469
  allow(mock_flags).to receive(:get_variant) do |_key, fallback, ctx|
470
- expect(ctx).to eq({ 'distinct_id' => 'user-1', 'plan' => 'premium' })
470
+ expect(ctx).to eq({ 'distinct_id' => 'user-1', 'plan' => 'premium', '$openfeature_provider' => true })
471
471
  Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
472
472
  end
473
473
 
@@ -480,7 +480,7 @@ RSpec.describe Mixpanel::OpenFeature::Provider do
480
480
  targeting_key: 'tk-123'
481
481
  )
482
482
  allow(mock_flags).to receive(:get_variant) do |_key, fallback, ctx|
483
- expect(ctx).to eq({ 'distinct_id' => 'user-1', 'targetingKey' => 'tk-123' })
483
+ expect(ctx).to eq({ 'distinct_id' => 'user-1', 'targetingKey' => 'tk-123', '$openfeature_provider' => true })
484
484
  Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
485
485
  end
486
486
 
@@ -493,7 +493,7 @@ RSpec.describe Mixpanel::OpenFeature::Provider do
493
493
  targeting_key: nil
494
494
  )
495
495
  allow(mock_flags).to receive(:get_variant) do |_key, fallback, ctx|
496
- expect(ctx).to eq({ 'distinct_id' => 'user-1', 'age' => 30 })
496
+ expect(ctx).to eq({ 'distinct_id' => 'user-1', 'age' => 30, '$openfeature_provider' => true })
497
497
  Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
498
498
  end
499
499
 
@@ -506,7 +506,7 @@ RSpec.describe Mixpanel::OpenFeature::Provider do
506
506
  targeting_key: nil
507
507
  )
508
508
  allow(mock_flags).to receive(:get_variant) do |_key, fallback, ctx|
509
- expect(ctx).to eq({ 'score' => 3.14 })
509
+ expect(ctx).to eq({ 'score' => 3.14, '$openfeature_provider' => true })
510
510
  Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
511
511
  end
512
512
 
@@ -519,7 +519,7 @@ RSpec.describe Mixpanel::OpenFeature::Provider do
519
519
  targeting_key: nil
520
520
  )
521
521
  allow(mock_flags).to receive(:get_variant) do |_key, fallback, ctx|
522
- expect(ctx).to eq({ 'tags' => ['a', 'b', 'c'] })
522
+ expect(ctx).to eq({ 'tags' => ['a', 'b', 'c'], '$openfeature_provider' => true })
523
523
  Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
524
524
  end
525
525
 
@@ -532,16 +532,16 @@ RSpec.describe Mixpanel::OpenFeature::Provider do
532
532
  targeting_key: nil
533
533
  )
534
534
  allow(mock_flags).to receive(:get_variant) do |_key, fallback, ctx|
535
- expect(ctx).to eq({ 'meta' => { 'nested_float' => 5, 'name' => 'test' } })
535
+ expect(ctx).to eq({ 'meta' => { 'nested_float' => 5, 'name' => 'test' }, '$openfeature_provider' => true })
536
536
  Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
537
537
  end
538
538
 
539
539
  provider.fetch_boolean_value(flag_key: 'flag', default_value: false, evaluation_context: eval_context)
540
540
  end
541
541
 
542
- it 'passes empty hash when evaluation_context is nil' do
542
+ it 'passes only the provider marker when evaluation_context is nil' do
543
543
  allow(mock_flags).to receive(:get_variant) do |_key, fallback, ctx|
544
- expect(ctx).to eq({})
544
+ expect(ctx).to eq({ '$openfeature_provider' => true })
545
545
  Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
546
546
  end
547
547
 
@@ -665,6 +665,43 @@ RSpec.describe Mixpanel::OpenFeature::Provider do
665
665
  end
666
666
  end
667
667
 
668
+ # --- OpenFeature provider marker (SDK-448) ---
669
+
670
+ describe 'openfeature provider marker' do
671
+ it 'adds $openfeature_provider => true to the context passed to get_variant' do
672
+ eval_context = double('EvaluationContext', fields: { 'distinct_id' => 'user-1' }, targeting_key: nil)
673
+ received_ctx = nil
674
+ expect(mock_flags).to receive(:get_variant).once do |_key, _fallback, ctx, **_kwargs|
675
+ received_ctx = ctx
676
+ Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
677
+ end
678
+
679
+ result = provider.fetch_boolean_value(flag_key: 'flag', default_value: false, evaluation_context: eval_context)
680
+
681
+ # Guard against the provider's rescue swallowing a stub error.
682
+ expect(result.error_code).to be_nil
683
+ expect(result.value).to be true
684
+ expect(received_ctx).to eq({ 'distinct_id' => 'user-1', '$openfeature_provider' => true })
685
+ end
686
+
687
+ it 'does not let a customer attribute override the marker' do
688
+ eval_context = double('EvaluationContext',
689
+ fields: { 'distinct_id' => 'user-1', '$openfeature_provider' => false },
690
+ targeting_key: nil
691
+ )
692
+ received_ctx = nil
693
+ expect(mock_flags).to receive(:get_variant).once do |_key, _fallback, ctx, **_kwargs|
694
+ received_ctx = ctx
695
+ Mixpanel::Flags::SelectedVariant.new(variant_key: 'v1', variant_value: true)
696
+ end
697
+
698
+ result = provider.fetch_boolean_value(flag_key: 'flag', default_value: false, evaluation_context: eval_context)
699
+
700
+ expect(result.error_code).to be_nil
701
+ expect(received_ctx['$openfeature_provider']).to be true
702
+ end
703
+ end
704
+
668
705
  # --- Lifecycle ---
669
706
 
670
707
  describe '#shutdown' do
@@ -225,4 +225,107 @@ describe Mixpanel::Events do
225
225
  }.not_to output(/DEPRECATION/).to_stderr
226
226
  end
227
227
  end
228
+
229
+ describe 'import failures' do
230
+ # These examples use a custom sink block to simulate consumer errors. Passing
231
+ # credentials to the constructor together with a block prints an unrelated
232
+ # "credentials are ignored" warning, which would pollute the stderr assertions,
233
+ # so credentials are set directly on the instance instead.
234
+ let(:credentials) { Mixpanel::ServiceAccountCredentials.new('user', 'secret', 'project') }
235
+ let(:failing_sink) do
236
+ proc { |_type, _message| raise Mixpanel::ServerError, "Could not write to Mixpanel, server responded with 401" }
237
+ end
238
+
239
+ it 'returns false and warns on stderr with the default error handler' do
240
+ events = Mixpanel::Events.new('TEST TOKEN', nil, credentials: nil, &failing_sink)
241
+ events.instance_variable_set(:@credentials, credentials)
242
+
243
+ result = nil
244
+ expect {
245
+ result = events.import_events('TEST ID', 'Test Event', {})
246
+ }.to output(/\[Mixpanel\] import failed; the event may not have been stored: .*401/).to_stderr
247
+ expect(result).to be false
248
+ end
249
+
250
+ it 'warns only on the first failure but returns false every time' do
251
+ events = Mixpanel::Events.new('TEST TOKEN', &failing_sink)
252
+ events.instance_variable_set(:@credentials, credentials)
253
+
254
+ results = []
255
+ expect {
256
+ 3.times { results << events.import_events('TEST ID', 'Test Event', {}) }
257
+ }.to output(/\A\[Mixpanel\] import failed[^\n]*\n\z/).to_stderr
258
+ expect(results).to eq([false, false, false])
259
+ end
260
+
261
+ it 'truncates a long server reply in the warning' do
262
+ events = Mixpanel::Events.new('TEST TOKEN') { |*| raise Mixpanel::ServerError, 'x' * 5000 }
263
+ events.instance_variable_set(:@credentials, credentials)
264
+
265
+ expect {
266
+ events.import_events('TEST ID', 'Test Event', {})
267
+ }.to output(/stored: x{200}\. Further/).to_stderr
268
+ end
269
+
270
+ it 'says the outcome is unknown when the import fails with a connection error' do
271
+ events = Mixpanel::Events.new('TEST TOKEN') do |_type, _message|
272
+ raise Mixpanel::ConnectionError, 'Could not connect to Mixpanel, with error "Net::ReadTimeout".'
273
+ end
274
+ events.instance_variable_set(:@credentials, credentials)
275
+
276
+ result = nil
277
+ expect {
278
+ result = events.import_events('TEST ID', 'Test Event', {})
279
+ }.to output(/\[Mixpanel\] import failed; the event may not have been stored: .*ReadTimeout/).to_stderr
280
+ expect(result).to be false
281
+ end
282
+
283
+ it 'prints the failure on a single line even when the server body has newlines' do
284
+ events = Mixpanel::Events.new('TEST TOKEN') do |_type, _message|
285
+ raise Mixpanel::ServerError, "Could not write to Mixpanel, server responded with 401 returning: '{\"status\":0}\n'"
286
+ end
287
+ events.instance_variable_set(:@credentials, credentials)
288
+
289
+ expect {
290
+ events.import_events('TEST ID', 'Test Event', {})
291
+ }.to output(/\A\[Mixpanel\] import failed[^\n]*\n\z/).to_stderr
292
+ end
293
+
294
+ it 'passes the error to a custom handler without printing the import warning' do
295
+ handler = Class.new(Mixpanel::ErrorHandler) do
296
+ attr_reader :errors
297
+ def handle(error)
298
+ (@errors ||= []) << error
299
+ end
300
+ end.new
301
+ events = Mixpanel::Events.new('TEST TOKEN', handler, &failing_sink)
302
+ events.instance_variable_set(:@credentials, credentials)
303
+
304
+ result = nil
305
+ expect {
306
+ result = events.import_events('TEST ID', 'Test Event', {})
307
+ }.not_to output(/import failed/).to_stderr
308
+ expect(result).to be false
309
+ expect(handler.errors.map(&:class)).to eq([Mixpanel::ServerError])
310
+ end
311
+
312
+ it 'keeps track failures silent with the default error handler' do
313
+ events = Mixpanel::Events.new('TEST TOKEN', &failing_sink)
314
+
315
+ result = nil
316
+ expect {
317
+ result = events.track('TEST ID', 'Test Event', {})
318
+ }.not_to output.to_stderr
319
+ expect(result).to be false
320
+ end
321
+
322
+ it 'does not warn when the import succeeds' do
323
+ events = Mixpanel::Events.new('TEST TOKEN') { |_type, _message| true }
324
+ events.instance_variable_set(:@credentials, credentials)
325
+
326
+ expect {
327
+ expect(events.import_events('TEST ID', 'Test Event', {})).to be true
328
+ }.not_to output.to_stderr
329
+ end
330
+ end
228
331
  end
@@ -654,6 +654,53 @@ describe Mixpanel::Flags::LocalFlagsProvider do
654
654
  provider.get_variant_value('test_flag', 'fallback', { 'distinct_id' => 'qa_user' })
655
655
  end
656
656
 
657
+ it 'marks exposure with $openfeature_provider when context carries the marker (SDK-448)' do
658
+ flag = create_test_flag
659
+ stub_flag_definitions([flag])
660
+ provider.start_polling_for_definitions!
661
+
662
+ captured = nil
663
+ expect(mock_tracker).to receive(:call).once do |_distinct_id, _event_name, properties|
664
+ captured = properties
665
+ end
666
+
667
+ result = provider.get_variant_value('test_flag', 'fallback', test_context.merge('$openfeature_provider' => true))
668
+
669
+ # The marker must not change variant assignment.
670
+ expect(result).to eq(provider.get_variant_value('test_flag', 'fallback', test_context, report_exposure: false))
671
+ expect(captured['$openfeature_provider']).to eq(true)
672
+ end
673
+
674
+ it 'accepts a symbol-keyed $openfeature_provider marker (SDK-448)' do
675
+ flag = create_test_flag
676
+ stub_flag_definitions([flag])
677
+ provider.start_polling_for_definitions!
678
+
679
+ captured = nil
680
+ expect(mock_tracker).to receive(:call).once do |_distinct_id, _event_name, properties|
681
+ captured = properties
682
+ end
683
+
684
+ provider.get_variant_value('test_flag', 'fallback', test_context.merge('$openfeature_provider': true))
685
+
686
+ expect(captured['$openfeature_provider']).to eq(true)
687
+ end
688
+
689
+ it 'omits $openfeature_provider from exposure for direct SDK calls (SDK-448)' do
690
+ flag = create_test_flag
691
+ stub_flag_definitions([flag])
692
+ provider.start_polling_for_definitions!
693
+
694
+ captured = nil
695
+ expect(mock_tracker).to receive(:call).once do |_distinct_id, _event_name, properties|
696
+ captured = properties
697
+ end
698
+
699
+ provider.get_variant_value('test_flag', 'fallback', test_context)
700
+
701
+ expect(captured).not_to have_key('$openfeature_provider')
702
+ end
703
+
657
704
  it 'does not track exposure on fallback' do
658
705
  stub_flag_definitions([])
659
706
  provider.start_polling_for_definitions!
@@ -303,6 +303,40 @@ describe Mixpanel::Flags::RemoteFlagsProvider do
303
303
  provider.get_variant('test-flag', fallback_variant, test_context)
304
304
  end
305
305
 
306
+ it 'marks exposure with $openfeature_provider when context carries the marker (SDK-448)' do
307
+ stub_flags_request(create_success_response({
308
+ 'test-flag' => { 'variant_key' => 'treatment', 'variant_value' => true }
309
+ }))
310
+ fallback_variant = Mixpanel::Flags::SelectedVariant.new(variant_key: 'control', variant_value: false)
311
+
312
+ captured = nil
313
+ expect(mock_tracker).to receive(:call).once do |_distinct_id, _event_name, properties|
314
+ captured = properties
315
+ end
316
+
317
+ result = provider.get_variant('test-flag', fallback_variant, test_context.merge('$openfeature_provider' => true))
318
+
319
+ expect(result.variant_key).to eq('treatment')
320
+ expect(captured['Flag evaluation mode']).to eq('remote')
321
+ expect(captured['$openfeature_provider']).to eq(true)
322
+ end
323
+
324
+ it 'omits $openfeature_provider from exposure for direct SDK calls (SDK-448)' do
325
+ stub_flags_request(create_success_response({
326
+ 'test-flag' => { 'variant_key' => 'treatment', 'variant_value' => true }
327
+ }))
328
+ fallback_variant = Mixpanel::Flags::SelectedVariant.new(variant_key: 'control', variant_value: false)
329
+
330
+ captured = nil
331
+ expect(mock_tracker).to receive(:call).once do |_distinct_id, _event_name, properties|
332
+ captured = properties
333
+ end
334
+
335
+ provider.get_variant('test-flag', fallback_variant, test_context)
336
+
337
+ expect(captured).not_to have_key('$openfeature_provider')
338
+ end
339
+
306
340
  it 'does not track exposure event when fallback variant is selected' do
307
341
  stub_flags_request(create_success_response({}))
308
342
 
@@ -10,6 +10,69 @@ describe Mixpanel::Tracker do
10
10
  allow(Time).to receive(:now).and_return(@time_now)
11
11
  end
12
12
 
13
+ describe 'import rejected by the server (GitHub #106)' do
14
+ before(:each) { WebMock.reset! }
15
+
16
+ it 'returns true, sends Basic Auth and project_id, and prints nothing when /import accepts the event' do
17
+ stub_request(:post, 'https://api.mixpanel.com/import?project_id=123').
18
+ to_return(status: 200, body: '{"code": 200, "num_records_imported": 1, "status": 1}')
19
+ credentials = Mixpanel::ServiceAccountCredentials.new('sa-user', 'sa-secret', 123)
20
+ tracker = Mixpanel::Tracker.new('TEST TOKEN', credentials: credentials)
21
+
22
+ result = nil
23
+ expect {
24
+ result = tracker.import_events('ID', 'Import', { 'time' => 1369353600 })
25
+ }.not_to output.to_stderr
26
+ expect(result).to be true
27
+
28
+ expect(WebMock).to have_requested(:post, 'https://api.mixpanel.com/import?project_id=123').
29
+ with(basic_auth: ['sa-user', 'sa-secret']) { |req|
30
+ form = URI.decode_www_form(req.body).to_h
31
+ payload = JSON.parse(Base64.decode64(form['data']))
32
+ !form.key?('api_key') && payload['event'] == 'Import' &&
33
+ payload['properties']['distinct_id'] == 'ID' && payload['properties']['time'] == 1369353600
34
+ }
35
+ end
36
+
37
+ it 'does not claim rejection when /import returns 200 with an unreadable body' do
38
+ stub_request(:post, 'https://api.mixpanel.com/import?project_id=123').
39
+ to_return(status: 200, body: '<html>bad gateway</html>')
40
+ credentials = Mixpanel::ServiceAccountCredentials.new('user', 'secret', 123)
41
+ tracker = Mixpanel::Tracker.new('TEST TOKEN', credentials: credentials)
42
+
43
+ result = nil
44
+ expect {
45
+ result = tracker.import_events('ID', 'Import', { 'time' => 1369353600 })
46
+ }.to output(/\[Mixpanel\] import failed; the event may not have been stored: Could not interpret/).to_stderr
47
+ expect(result).to be false
48
+ end
49
+
50
+ it 'returns false and warns when /import rejects service account credentials' do
51
+ stub_request(:post, 'https://api.mixpanel.com/import?project_id=123').
52
+ to_return(status: 401, body: '{"error": "Invalid credentials", "status": 0}')
53
+ credentials = Mixpanel::ServiceAccountCredentials.new('user', 'bad-secret', 123)
54
+ tracker = Mixpanel::Tracker.new('TEST TOKEN', credentials: credentials)
55
+
56
+ result = nil
57
+ expect {
58
+ result = tracker.import_events('ID', 'Import', { 'time' => 1369353600 })
59
+ }.to output(/\[Mixpanel\] import failed; the event may not have been stored: .*401/).to_stderr
60
+ expect(result).to be false
61
+ end
62
+
63
+ it 'returns false and warns when /import rejects a legacy api_key' do
64
+ stub_request(:post, 'https://api.mixpanel.com/import').
65
+ to_return(status: 401, body: '{"error": "Unauthorized", "status": 0}')
66
+ tracker = Mixpanel::Tracker.new('TEST TOKEN')
67
+
68
+ result = nil
69
+ expect {
70
+ result = tracker.import('API_KEY', 'ID', 'Import', { 'time' => 1369353600 })
71
+ }.to output(/may be rejected by Mixpanel's \/import endpoint.*\[Mixpanel\] import failed; the event may not have been stored/m).to_stderr
72
+ expect(result).to be false
73
+ end
74
+ end
75
+
13
76
  it 'should send an alias message to mixpanel no matter what the consumer is' do
14
77
  WebMock.reset!
15
78
  stub_request(:any, 'https://api.mixpanel.com/track').to_return({:body => '{"status": 1, "error": null}'})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixpanel-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mixpanel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-09-02 00:00:00.000000000 Z
11
+ date: 2026-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mutex_m
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.2'
47
+ version: '0.3'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.2'
54
+ version: '0.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement