Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Add `mode: :audience` to collect events without identity fields. Product mode remains the default.

### Changed

* Deprecate the `environment` option: it is ignored and no longer sent. Use an environment-bound Product Analytics API key.


## [1.3.0](https://github.com/altertable-ai/altertable-ruby/compare/altertable/v1.2.0...altertable/v1.3.0) (2026-09-04)

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Initialize the client and track your first event. Call `track()` to record an ac
require 'altertable'

# Initialize the Altertable client
Altertable.init('pk_live_abc123', environment: 'production')
Altertable.init('pk_live_abc123')

# Track an event
Altertable.track('button_clicked', 'user_123', properties: {
Expand All @@ -40,7 +40,7 @@ Initialize the global Altertable client instance.
`Altertable.init(api_key, options = {})`

```ruby
Altertable.init('pk_live_abc123', environment: 'production', debug: true)
Altertable.init('pk_live_abc123', debug: true)
```

### Tracking Events
Expand Down Expand Up @@ -79,7 +79,7 @@ Altertable.track('Docs Opened', properties: { section: 'installation' })
```

Audience events send only `event`, `timestamp`, and `properties`; distinct, anonymous,
device, session, and environment fields are dropped. `identify` and `alias` are ignored.
device, and session fields are dropped. `identify` and `alias` are ignored.
Requests come from your server, so they cannot distinguish browser visitors.

### Identifying Users
Expand Down Expand Up @@ -128,7 +128,7 @@ You can configure the client by passing options during initialization.
| Option | Type | Default | Description |
|---|---|---|---|
| `mode` | Symbol, String | `:product` | Collection mode: `:product` or `:audience`. Fixed for the client lifetime. |
| `environment` | String | `"production"` | Product environment name (e.g., `production`, `development`). Omitted in audience mode; the API key selects its environment. |
| `environment` | String | — | Deprecated and ignored. It is accepted for compatibility but never sent; use an environment-bound Product Analytics API key. |
| `base_url` | String | `"https://api.altertable.ai"` | Base URL for API requests. |
| `request_timeout` | Integer | `5` | Request timeout in seconds. |
| `release` | String | `nil` | Application release version (e.g., commit hash). |
Expand Down
6 changes: 2 additions & 4 deletions lib/altertable/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Altertable
class Client
DEFAULT_BASE_URL = "https://api.altertable.ai"
DEFAULT_TIMEOUT = 5
# @deprecated Environments are selected by the Product Analytics API key.
# Retained so applications that reference the public constant continue to load.
DEFAULT_ENVIRONMENT = "production"

def initialize(api_key, options = {})
Expand All @@ -22,7 +24,6 @@ def initialize(api_key, options = {})
end
@api_key = api_key
@base_url = options[:base_url] || DEFAULT_BASE_URL
@environment = options[:environment] || DEFAULT_ENVIRONMENT
@timeout = options[:request_timeout] || DEFAULT_TIMEOUT
@release = options[:release]
@debug = options[:debug] || false
Expand Down Expand Up @@ -178,7 +179,6 @@ def track_payload(event, distinct_id, options)
payload = {
timestamp: options[:timestamp] || default_timestamp,
event: event,
environment: @environment,
distinct_id: distinct_id,
properties: {
'$lib': "altertable-ruby",
Expand All @@ -201,7 +201,6 @@ def audience_payload(payload)
def identify_payload(user_id, options)
payload = {
timestamp: options[:timestamp] || default_timestamp,
environment: @environment,
distinct_id: user_id,
traits: options[:traits] || {}
}
Expand All @@ -213,7 +212,6 @@ def identify_payload(user_id, options)
def alias_payload(distinct_id, new_user_id, options)
{
timestamp: options[:timestamp] || default_timestamp,
environment: @environment,
distinct_id: distinct_id,
new_user_id: new_user_id
}
Expand Down
1 change: 1 addition & 0 deletions rbi/altertable.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Altertable
class Client
DEFAULT_BASE_URL = T.let(T.unsafe(nil), String)
DEFAULT_TIMEOUT = T.let(T.unsafe(nil), Integer)
# @deprecated Environments are selected by the Product Analytics API key.
DEFAULT_ENVIRONMENT = T.let(T.unsafe(nil), String)

sig { params(api_key: String, options: T::Hash[Symbol, T.untyped]).void }
Expand Down
2 changes: 1 addition & 1 deletion sig/altertable/client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Altertable
@mode: :product | :audience
@api_key: String
@base_url: String
@environment: String
@timeout: Integer | Float
@release: String?
@debug: bool
Expand All @@ -12,6 +11,7 @@ module Altertable

DEFAULT_BASE_URL: String
DEFAULT_TIMEOUT: Integer
# @deprecated Environments are selected by the Product Analytics API key.
DEFAULT_ENVIRONMENT: String

def initialize: (String api_key, ?::Hash[Symbol, untyped] options) -> void
Expand Down
6 changes: 4 additions & 2 deletions spec/altertable/client_audience_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def expect_audience(payload)
product = build_client(mode: mode, environment: "staging")
product.track("Opened", "user", anonymous_id: "anon", device_id: "device", mode: :audience,
properties: { "$url" => "https://site.example/?private=yes", "$viewport" => "100x100" })
expect(calls.last[:body]).to include("distinct_id" => "user", "anonymous_id" => "anon", "device_id" => "device", "environment" => "staging")
expect(calls.last[:body]).to include("distinct_id" => "user", "anonymous_id" => "anon", "device_id" => "device")
expect(calls.last[:body]).not_to have_key("environment")
expect(calls.last[:body]).not_to have_key("mode")
expect(calls.last[:body]["properties"]).to include("$url" => "https://site.example/?private=yes", "$viewport" => "100x100")
expect { product.track({ event: "No ID" }) }.to raise_error(ArgumentError, /distinct_id/)
Expand All @@ -102,7 +103,8 @@ def expect_audience(payload)

it "defaults to product mode" do
build_client.track("Opened", "user")
expect(calls.last[:body]).to include("distinct_id" => "user", "environment" => "production")
expect(calls.last[:body]).to include("distinct_id" => "user")
expect(calls.last[:body]).not_to have_key("environment")
expect(calls.last[:body]).not_to have_key("mode")
end

Expand Down
22 changes: 18 additions & 4 deletions spec/altertable/client_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def post(path, body: nil, params: {})
expect(payload).to include(
"event" => "signup",
"distinct_id" => "u1",
"environment" => "production",
"timestamp" => "2025-06-15T14:30:00.000Z"
)
expect(payload["properties"]).to include("plan" => "pro", "$lib" => "altertable-ruby")
Expand Down Expand Up @@ -75,7 +74,6 @@ def post(path, body: nil, params: {})
expect(payloads[0]).to include(
"event" => "signup",
"distinct_id" => "u1",
"environment" => "production",
"timestamp" => "2025-06-15T14:30:00.000Z"
)
expect(payloads[0]["properties"]).to include("plan" => "pro", "$lib" => "altertable-ruby", "$lib_version" => Altertable::VERSION, "$release" => "1.2.3")
Expand Down Expand Up @@ -161,7 +159,6 @@ def post(path, body: nil, params: {})
payloads = adapter.calls.first[:body]
expect(payloads[0]).to include(
"distinct_id" => "u1",
"environment" => "production",
"timestamp" => "2025-06-15T14:30:00.000Z"
)
expect(payloads[0]["traits"]).to include("email" => "a@example.com")
Expand Down Expand Up @@ -210,7 +207,6 @@ def post(path, body: nil, params: {})
expect(payloads[0]).to include(
"distinct_id" => "anon-1",
"new_user_id" => "u1",
"environment" => "production",
"timestamp" => "2025-06-15T14:30:00.000Z"
)
expect(payloads[1]).to include("distinct_id" => "anon-2", "new_user_id" => "u2")
Expand All @@ -232,4 +228,22 @@ def post(path, body: nil, params: {})
expect(adapter.calls.first[:body].first).to include("distinct_id" => "anon-1", "new_user_id" => "u1")
end
end

it "accepts deprecated environments without sending them for scalar, hash, and batch calls" do
deprecated = nil
expect { deprecated = described_class.new(api_key, environment: "staging", debug: true) }.not_to output.to_stderr
deprecated.instance_variable_set(:@adapter, adapter)

deprecated.track("Scalar", "u1", environment: "event")
deprecated.track({ event: "Hash", distinct_id: "u2", environment: "hash", properties: { environment: "business" } })
deprecated.track([{ event: "Batch", distinct_id: "u3", "environment" => "batch" }])
deprecated.identify([{ user_id: "u1", environment: "identify" }])
deprecated.alias([{ distinct_id: "old", new_user_id: "new", environment: "alias" }])

adapter.calls.each do |call|
payloads = call[:body].is_a?(Array) ? call[:body] : [call[:body]]
payloads.each { |payload| expect(payload).not_to have_key("environment") }
end
expect(adapter.calls[1][:body]["properties"]).to include("environment" => "business")
end
end
Loading