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.
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.
SDK setup requirements
- Sign up to RudderStack Cloud.
- 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.
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:
- Add the following line to your application’s Gem file:
- 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
)
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
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
}
)
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:
Sending events
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:
Field | Type | Description |
---|
user_id Required, if anonymous_id is absent. | String | Unique identifier for a user in your database. |
anonymous_id Required, if user_id is absent. | String | Use this field to set an identifier in cases where there is no unique user identifier. |
traits | Object | An optional dictionary of the user’s traits like name or email . |
context | Object | An optional dictionary of information that provides context about the event. It is not directly related to the API call. |
integrations | Object | An optional dictionary containing the destinations to be either enabled or disabled. |
timestamp | Timestamp in ISO 8601 format | The 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:
Field | Type | Description |
---|
user_id Required, if anonymous_id is absent. | String | Unique identifier for a user in your database. |
anonymous_id Required, if user_id is absent. | String | Use this field to set an identifier in cases where there is no unique user identifier. |
event Required | String | Name of the event. |
properties | Object | An optional dictionary of the properties associated with the event. |
context | Object | An optional dictionary of information that provides context about the event. It is not directly related to the API call. |
timestamp | Timestamp in ISO 8601 format | The timestamp of the event’s arrival. |
integrations | Object | An 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:
Field | Type | Description |
---|
user_id Required, if anonymous_id is absent. | String | Unique identifier for a user in your database. |
anonymous_id Required, if user_id is absent. | String | Use this field to set an identifier in cases where there is no unique user identifier. |
name Required | String | Name of the viewed page. |
properties | Object | An optional dictionary of the properties associated with the viewed page, like url or referrer . |
integrations | Object | An optional dictionary containing the destinations to be either enabled or disabled. |
context | Object | An optional dictionary of information that provides context about the event. It is not directly related to the API call. |
timestamp | Timestamp in ISO 8601 format | The 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:
Field | Type | Description |
---|
user_id Required, if anonymous_id is absent. | String | Unique identifier for a user in your database. |
anonymous_id Required, if user_id is absent. | String | Use this field to set an identifier in cases where there is no unique user identifier. |
name Required | String | Name of the viewed screen. |
properties | Object | An optional dictionary of the properties associated with the viewed screen, like url or referrer . |
integrations | Object | An optional dictionary containing the destinations to be either enabled or disabled. |
context | Object | An optional dictionary of information that provides context about the event. It is not directly related to the API call. |
timestamp | Timestamp in ISO 8601 format | The 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:
Field | Type | Description |
---|
user_id Required, if anonymous_id is absent. | String | Unique identifier for a user in your database. |
anonymous_id Required, if user_id is absent. | String | Use this field to set an identifier in cases where there is no unique user identifier. |
group_id Required | String | Unique identifier of the group in your database. |
traits | Object | An optional dictionary of the group’s traits like name or email . |
integrations | Object | An optional dictionary containing the destinations to be either enabled or disabled. |
context | Object | An optional dictionary of information that provides context about the event. It is not directly related to the API call. |
timestamp | Timestamp in ISO 8601 format | The 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.
A sample alias
call is as shown:
analytics.alias(
previous_id: '1hKOmRA4GRlm',
user_id: '12345'
)
The alias
method parameters are as mentioned below:
Field | Type | Description |
---|
user_id Required, if anonymous_id is absent. | String | Unique identifier for a user in your database. |
anonymous_id Required, if user_id is absent. | String | Use this field to set an identifier in cases where there is no unique user identifier. |
previous_id Required | String | The previous unique identifier of the user. |
traits | Object | An optional dictionary of the user’s traits like name or email . |
integrations | Object | An optional dictionary containing the destinations to be either enabled or disabled. |
context | Object | An optional dictionary of information that provides context about the event. It is not directly related to the API call. |
timestamp | Timestamp in ISO 8601 format | The 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