Ruby SDK

Use RudderStack’s Ruby SDK to send server-side events to various destinations.

RudderStack’s Ruby SDK lets you track and send the events from your Ruby applications asynchronously to the specified destinations. You can use the SDK to improve the performance of your application by reducing the time taken to send the data.

info
To send the data from the Ruby SDK synchronously, refer to this documentation. Note that the synchronous SDK version will be deprecated soon.

Refer to the SDK’s GitHub codebase for the implementation-specific details.

Github Badge

SDK setup requirements

  1. Sign up to RudderStack Cloud.
  2. Set up a Ruby source in your dashboard. You should be able to see a write key for this source.

You will also need a data plane URL. Refer to the Dashboard Overview guide for more information on the data plane URL and where to find it.

success
The Setup tab in the RudderStack dashboard has the SDK installation snippet containing both the write key and the data plane URL. Copy it to integrate the Ruby SDK into your application.

Installing the Ruby SDK

To install the RudderStack Ruby SDK:

  1. Add the following line to your application’s Gem file:
gem 'rudder-sdk-ruby'
  1. Run bundle install to install the gem.

Initializing the SDK

To initialize the SDK, create a client instance as shown below:

require 'rudder-sdk-ruby'

analytics = Rudder::Analytics.new(
  write_key: 'WRITE_KEY',
  data_plane_url: 'DATA_PLANE_URL',
  gzip: true
)
warning
Make sure to replace the WRITE_KEY and DATA_PLANE_URL in the above snippet with the actual values from your RudderStack dashboard.

Gzipping requests

success
The Gzip feature is enabled by default in the Ruby SDK.

The Ruby SDK automatically gzips requests. However, you can disable this feature by setting the Gzip parameter to false while initializing the SDK:

analytics = Rudder::Analytics.new(
  write_key: 'WRITE_KEY', # required
  data_plane_url: 'DATA_PLANE_URL',
  gzip: false, // Set to true to enable Gzip compression
  on_error: proc { |error_code, error_body, exception, response|
    # defaults to an empty proc
  }
)
warning
Gzip requires rudder-server v1.4 or higher. Otherwise, your events might fail.

SDK initialization options

The RudderStack Ruby SDK provides the following initialization options:

FieldTypeDescription
write_key
Required
StringSource write keyThe write key (or source write key) is a unique identifier for your source. RudderStack uses this key to send events from a source to the specified destination. .
data_plane_url
Required
StringYour data plane URLThe data plane URL is the location where events are routed and sent to the RudderStack backend for processing. You can find this URL in the home page of your RudderStack dashboard. .
stubBooleanStubs the event requests.

Default value: false
gzipBooleanGzips the event requests.

Default value: true
max_queue_sizeIntegerMaximum queue size.

Default value: 10000.
retriesIntegerMaximum number of retry attempts to send an event.

Default value: 10
sslBooleanDetermines whether to use SSL for the socket connection.

Default value: true.

Sending events

warning
RudderStack does not store or persist the user state in any of the server-side SDKs.

Unlike the client-side SDKs that deal with only a single user at a given time, the server-side SDKs deal with multiple users simultaneously. Therefore, you must specify either the user_id or anonymous_id every time while making any API calls supported by the Ruby SDK.

Identify

The identify call lets you identify a visiting user and associate them to their actions. It also lets you record the traits about them like their name, email address, etc.

A sample identify call made using the Ruby SDK is shown below:

analytics.identify(
  user_id: '1hKOmRA4GRlm',
  traits: {
    email: "alex@example.com",
    createdAt: "2023-07-24T00:00:00Z",
    subscribe: true
  },
  context: { ip: '10.81.20.10' }
)

The identify method parameters are as shown:

FieldTypeDescription
user_id
Required, if anonymous_id is absent.
StringUnique identifier for a user in your database.
anonymous_id
Required, if user_id is absent.
StringUse this field to set an identifier in cases where there is no unique user identifier.
traitsObjectAn optional dictionary of the user’s traits like name or email.
contextObjectAn optional dictionary of information that provides context about the event. It is not directly related to the API call.
integrationsObjectAn optional dictionary containing the destinations to be either enabled or disabled.
timestampTimestamp in ISO 8601 formatThe timestamp of the event’s arrival.

Track

The track call lets you record the user actions along with their associated properties. Each user action is called an event.

A sample track call is shown below:

analytics.track(
  user_id: '1hKOmRA4GRlm',
  event: 'Item Sold',
  properties: {
    revenue: 9.95,
    shipping: 'Free'
  }
)

The track method parameters are as described below:

FieldTypeDescription
user_id
Required, if anonymous_id is absent.
StringUnique identifier for a user in your database.
anonymous_id
Required, if user_id is absent.
StringUse this field to set an identifier in cases where there is no unique user identifier.
event
Required
StringName of the event.
propertiesObjectAn optional dictionary of the properties associated with the event.
contextObjectAn optional dictionary of information that provides context about the event. It is not directly related to the API call.
timestampTimestamp in ISO 8601 formatThe timestamp of the event’s arrival.
integrationsObjectAn optional dictionary containing the destinations to be either enabled or disabled.

Page

The page call lets you record the page views on your application along with the other relevant information about the page.

A sample page call is as shown:

analytics.page(
  user_id: `1hKOmRA4GRlm`,
  category: 'Food',
  name: 'Pizza',
  properties: {
    URL: 'https://website.com'
  }
)

The page method parameters are as described below:

FieldTypeDescription
user_id
Required, if anonymous_id is absent.
StringUnique identifier for a user in your database.
anonymous_id
Required, if user_id is absent.
StringUse this field to set an identifier in cases where there is no unique user identifier.
name
Required
StringName of the viewed page.
propertiesObjectAn optional dictionary of the properties associated with the viewed page, like url or referrer.
integrationsObjectAn optional dictionary containing the destinations to be either enabled or disabled.
contextObjectAn optional dictionary of information that provides context about the event. It is not directly related to the API call.
timestampTimestamp in ISO 8601 formatThe timestamp of the event’s arrival.

Screen

The screen call is the mobile equivalent of the page call. It lets you record the screen views on your mobile app along with other relevant information about the screen.

A sample screen call is as shown:

analytics.screen(
  user_id: `1hKOmRA4GRlm`,
  category: 'Food',
  name: 'Pizza',
  properties: {
    URL: 'https://website.com'
  }
)

The screen method parameters are as described below:

FieldTypeDescription
user_id
Required, if anonymous_id is absent.
StringUnique identifier for a user in your database.
anonymous_id
Required, if user_id is absent.
StringUse this field to set an identifier in cases where there is no unique user identifier.
name
Required
StringName of the viewed screen.
propertiesObjectAn optional dictionary of the properties associated with the viewed screen, like url or referrer.
integrationsObjectAn optional dictionary containing the destinations to be either enabled or disabled.
contextObjectAn optional dictionary of information that provides context about the event. It is not directly related to the API call.
timestampTimestamp in ISO 8601 formatThe timestamp of the event’s arrival.

Group

The group call lets you link an identified user with a group, such as a company, organization, or an account. It also lets you record any custom traits or properties associated with that group.

A sample group call made using the Ruby SDK is shown below:

analytics.group(
  user_id: '1hKOmRA4GRlm',
  group_id: '12',
  traits: {
    name: 'Company',
    description: 'Software'
  }
)

The group method parameters are as follows:

FieldTypeDescription
user_id
Required, if anonymous_id is absent.
StringUnique identifier for a user in your database.
anonymous_id
Required, if user_id is absent.
StringUse this field to set an identifier in cases where there is no unique user identifier.
group_id
Required
StringUnique identifier of the group in your database.
traitsObjectAn optional dictionary of the group’s traits like nameor email.
integrationsObjectAn optional dictionary containing the destinations to be either enabled or disabled.
contextObjectAn optional dictionary of information that provides context about the event. It is not directly related to the API call.
timestampTimestamp in ISO 8601 formatThe timestamp of the event’s arrival.

Alias

The alias call lets you merge different identities of a known user. It is an advanced method that lets you change the tracked user’s ID explicitly. You can use alias for managing the user’s identity in some of the downstream destinations.

warning
RudderStack supports sending alias events only to select downstream destinations. Refer to the destination-specific documentation for more details.

A sample alias call is as shown:

analytics.alias(
  previous_id: '1hKOmRA4GRlm',
  user_id: '12345'
)

The alias method parameters are as mentioned below:

FieldTypeDescription
user_id
Required, if anonymous_id is absent.
StringUnique identifier for a user in your database.
anonymous_id
Required, if user_id is absent.
StringUse this field to set an identifier in cases where there is no unique user identifier.
previous_id
Required
StringThe previous unique identifier of the user.
traitsObjectAn optional dictionary of the user’s traits like name or email.
integrationsObjectAn optional dictionary containing the destinations to be either enabled or disabled.
contextObjectAn optional dictionary of information that provides context about the event. It is not directly related to the API call.
timestampTimestamp in ISO 8601 formatThe timestamp of the event’s arrival.

FAQ

How does the Ruby SDK handle events larger than 32KB?

The Ruby SDK drops any events greater than 32KB.

Does the Ruby SDK support event ordering?

The Ruby SDK does not support event ordering by default.



Questions? Contact us by email or on Slack