-
Notifications
You must be signed in to change notification settings - Fork 0
Quickstart
Get the Convert Ruby SDK running and serving your first experiment. This page is the fast path; for the conceptual background read Quickstart Overview and How Convert Works first.
You need an SDK key from your Convert dashboard and Ruby 3.1 or newer. The gem has zero runtime dependencies (stdlib only).
Build a client, create a per-visitor context, decide an experience, act on the result, track a conversion, and flush — every step is copy-pasteable:
require "convert_sdk"
# 1. Build ONE client at boot and reuse it for the life of the process.
# Fetch mode: pass an sdk_key. Direct-data mode: pass a pre-fetched `data:`.
CONVERT_SDK = ConvertSdk.create(sdk_key: ENV.fetch("CONVERT_SDK_KEY"))
# 2. One context per visitor (per web request / per job). Cheap — no network,
# no thread. The singleton client owns all shared state.
context = CONVERT_SDK.create_context("visitor-123", { "country" => "US" })
# 3. Decide an experience. Returns a BucketedVariation on a hit, or a Sentinel
# on a miss — NEVER raises, NEVER a bare nil.
variation = context.run_experience("homepage-test")
# 4. Act on the result. `variation&.key` is the variation key on a hit and nil
# on a miss (a Sentinel's #key is always nil), so a single `case` covers both.
case variation&.key
when nil then render_default # business miss — show the control
when "treatment" then render_treatment
else render_variation(variation.key)
end
# 5. Track a conversion with revenue. Deduplicated per visitor per goal.
context.track_conversion("purchase", goal_data: { amount: 49.99, transaction_id: "tx-1" })
# 6. Flush queued events synchronously. In long-running servers the background
# timer also drains; call flush explicitly before a process exits (Lambda/CLI).
CONVERT_SDK.flush-
Build one client at boot.
ConvertSdk.createis the single entry point — it builds the validated config, fetches the bucketing config synchronously, and fires thereadyevent. Build it once and assign it to a process-lifetime constant; never build a client per request. The only thingcreatemay raise is anArgumentErroron misconfiguration. See Initialization. -
One context per visitor.
create_context(visitor_id, attributes = nil)returns a fresh, independentContext. It is cheap — no network, no thread. A blank/nil visitor id logs an error and returnsnil. -
Decide.
run_experience(key)returns a frozenBucketedVariationon a hit or aSentinelon a business miss. It never raises and never returns a barenil. See Return Types & Sentinels. -
Branch on
variation&.key. ASentinel's#keyis alwaysnil, socase variation&.keyfalls through to thenilbranch on a miss and exposes the real key on a hit — one branch handles both cases. -
Track conversions.
track_conversion(goal_key, goal_data: nil, force_multiple_transactions: false)records a conversion, deduplicated per visitor per goal. Pass revenue/transaction data viagoal_data:with snake_case keys (amount:,transaction_id:, …). See Tracking Conversions. -
Flush.
flush(aliasrelease_queues) delivers queued events synchronously. Long-running servers also drain via the background flush timer, so you do not need to flush per request. Short-lived processes (AWS Lambda, CLI) must flush explicitly before exit — see Fork Safety & Runtime Recipes.
Each runtime has a copy-pasteable recipe on the Fork Safety & Runtime Recipes page:
- Rails (Puma cluster / Unicorn / Passenger) — fork-safe with zero config.
- Sidekiq — singleton client + a shutdown flush.
- AWS Lambda — timers off + a synchronous flush before the handler returns.
-
Plain CLI / rake — the automatic
at_exitflush.
- Installation — add the gem and the version floor
-
Initialization — fetch vs direct-data mode, the
readyevent - Configuration Options — every config option
- Code Examples — complete snippets for every method
Copyrights © 2026 All Rights Reserved by Convert Insights, Inc.
Getting Started
Ruby SDK
- Quickstart
- Installation
- Initialization
- Configuration
- Return Types & Sentinels
- Code Examples
- Fork Safety & Runtime Recipes
- Tracking Control
Core Concepts
- Experiences & Variations
- Feature Flags
- Bucketing Algorithm
- Rule Evaluation
- Segments
- Data Management
- Event System
- API Communication
How-To Guides
- Running Experiences
- Running Features
- Tracking Conversions
- Visitor Context
- Persistent DataStore
- Troubleshooting
Contributing