RudderStack's Ruby SDK lets you track and send the events from your Ruby applications to the specified destinations.

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.

The Setup tab in the RudderStack dashboard (seen above) 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:

gem 'rudder_analytics_sync'

You can also install it by running the following command:

gem install rudder_analytics_sync

Initializing the SDK

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

analytics = RudderAnalyticsSync::Client.new(
write_key: 'WRITE_KEY', # Required
data_plane_url: 'DATA_PLANE_URL',
gzip: true, # Set to false to disable Gzip compression
on_error: proc { |error_code, error_body, exception, response|
# defaults to an empty proc
}
)

Batching events

To manually batch events, use Analytics.batch as shown:

Analytics.batch do |batch|
batch.context = {...} # shared context for all events
batch.integrations = {...} # shared integrations hash for all events
batch.identify(...)
batch.track(...)
batch.track(...)
...
end

Gzipping requests

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

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

analytics = RudderAnalyticsSync::Client.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.

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", friends: 1 },
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.
StringThe SDK automatically sets this identifier in cases where there is no unique identifier for the user.
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.
StringThe SDK automatically sets this identifier in cases where there is no unique identifier for the user.
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://dominos.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.
StringThe SDK automatically sets this identifier in cases where there is no unique identifier for the user.
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

From version 2.0.0, the Ruby SDK supports screen events.

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://dominos.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.
StringThe SDK automatically sets this identifier in cases where there is no unique identifier for the user.
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.
StringThe SDK automatically sets this identifier in cases where there is no unique identifier for the user.
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.

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.
StringThe SDK automatically sets this identifier in cases where there is no unique identifier for the user.
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.


Contact us

For more information on the topics covered on this page, email us or start a conversation in our Slack community.

On this page