Skip to content

Experiment Ruby SDK (Beta)

Official documentation for Amplitude Experiment's server-side Ruby SDK implementation.

Gem Version

SDK Resources

Github · Releases · API Reference

Ruby SDK supports remote evaluation now.

Remote evaluation

Implements fetching variants for a user via remote evaluation.

Install

Ruby version compatibility

The Ruby Server SDK works with Ruby 2.0+.

Install the Ruby Server SDK with bundler or gem directly.

gem 'amplitude-experiment'
gem install amplitude-experiment --pre

Quick Start

  1. Initialize the experiment client
  2. Fetch variants for the user
  3. Access a flag's variant
require 'amplitude-experiment'

# (1) Initialize the experiment client
experiment = AmplitudeExperiment.init('<DEPLOYMENT_KEY>', AmplitudeExperiment::Config.new)

# (2) Fetch variants for a user
user = AmplitudeExperiment::User.new(
  user_id: 'user@company.com',
  device_id: 'abcdefg',
  user_properties: {
    'premium' => true
  }
)
variants = experiment.fetch(user)

# (3) Access a flag's variant
variant = variants['YOUR-FLAG-KEY']
unless variant.nil?
    if variant.value == 'on'
        # Flag is on
    else
        # Flag is off
    end
end

Initialize

The SDK client should be initialized in your server on startup. The deployment key argument passed into the apiKey parameter must live within the same project that you are sending analytics events to.

init(apiKey, config = nil) : Client
Parameter Requirement Description
apiKey required The deployment key which authorizes fetch requests and determines which flags should be evaluated for the user.
config optional The client configuration used to customize SDK client behavior.

Timeout & Retry Configuration

Please configure the timeout and retry options to best fit your performance requirements.

experiment = AmplitudeExperiment.init('<DEPLOYMENT_KEY>', AmplitudeExperiment::Config.new)

Configuration

The SDK client can be configured on initialization.

Configuration Options
Name
Description Default Value
debug Enable additional debug logging. false
server_url The host to fetch variants from. https://api.lab.amplitude.com
fetch_timeout_millis The timeout for fetching variants in milliseconds. This timeout only applies to the initial request, not subsequent retries 10000
fetch_retries The number of retries to attempt if a request to fetch variants fails. 0
fetch_retry_backoff_min_millis The minimum (initial) backoff after a request to fetch variants fails. This delay is scaled by the fetchRetryBackoffScalar 500
fetch_retry_backoff_max_millis The maximum backoff between retries. If the scaled backoff becomes greater than the max, the max is used for all subsequent requests 10000
fetch_retry_backoff_scalar Scales the minimum backoff exponentially. 1.5
fetch_retry_timeout_millis The request timeout for retrying variant fetches. 10000

Fetch

Fetches variants for a user and returns the results. This function remote evaluates the user for flags associated with the deployment used to initialize the SDK client.

fetch(user: AmplitudeExperiment::User) : Variants
Parameter Requirement Description
user required The user user to remote fetch variants for.
user = AmplitudeExperiment::User.new(
    user_id: 'user@company.com',
    device_id: 'abcdefg',
    user_properties: {
        'premium' => true
    }
)
variants = experiment.fetch(user)

After fetching variants for a user, you may to access the variant for a specific flag.

variant = variants['YOUR-FLAG-KEY']
unless variant.nil?
  if variant.value == 'on'
    # Flag is on
  else
    # Flag is off
  end
end

Fetch Async

The fetch method is synchronous. To fetch asynchronously, you can use fetch_async method

fetch_async(user: AmplitudeExperiment::User, &callback)

Parameter Requirement Description
user required The user user to remote fetch variants for.
callback optional The callback to handle the variants. Callback takes two arguments: User object and returned Variants.
experiment.fetch_async(user) do |_, variants|
  variant = variants['sdk-ci-test']
  unless variant.nil?
    if variant.value == 'on'
      # Flag is on
    else
      # Flag is off
    end
  end
end

Accessing Amplitude cookies

If you're using the Amplitude Analytics SDK on the client-side, the Ruby server SDK provides an AmplitudeCookie class with convenience functions for parsing and interacting with the Amplitude identity cookie. This is useful for ensuring that the Device ID on the server matches the Device ID set on the client, especially if the client has not yet generated a Device ID.

require 'amplitude-experiment'

# grab amp device id if present
amp_cookie_name = AmplitudeExperiment::AmplitudeCookie.cookie_name('amplitude-api-key')
device_id = nil
unless cookies[amp_cookie_name].nil?
  device_id = AmplitudeExperiment::AmplitudeCookie.parse(cookies[amp_cookie_name]).device_id
end

if device_id.nil?
  # deviceId doesn't exist, set the Amplitude Cookie
  device_id = SecureRandom.uuid
  amp_cookie_value = AmplitudeExperiment::AmplitudeCookie.generate(device_id)
  cookies[amp_cookie_name] = {
    value: amp_cookie_value,
    domain: '.yourdomain.com', # this should be the same domain used by the Amplitude JS SDK
    httponly: false,
    secure: false
  }
end

Last update: 2022-07-18