The RudderStack Ruby SDK lets you track your customer event data from your Ruby code and send it to your specified destinations via RudderStack.

Check out the GitHub codebase to get a more hands-on understanding of the SDK.

Github Badge

SDK setup requirements

To set up the RudderStack Ruby SDK, the following prerequisites must be met:

  • You will need to set up a RudderStack account.
  • Once signed up, set up a Ruby source in the dashboard. For more information, see Adding a source. You should then see a Write Key for this source, as shown below:
Ruby source write key
  • You will also need a data plane URL. Follow this section for more information on the data plane URL and where to get it.

Installing the Ruby SDK

To install the RudderStack Ruby SDK, add this line to your application's Gem file:

gem 'rudder_analytics_sync'

You can also install it yourself, as shown:

gem install rudder_analytics_sync

Using the SDK

To use the SDK, create a client instance as shown in the following snippet:

require 'rudder_analytics_sync'
Analytics = RudderAnalyticsSync::Client.new(
write_key: WRITE_KEY, # required
data_plane_url: DATA_PLANE_URL
on_error: proc { |error_code, error_body, exception, response|
# defaults to an empty proc
}
)

You can then use this client to send event requests, as shown:

Analytics.track(
user_id: user.id,
event: 'Created Account'
)

Manually batching events

You can manually batch events with 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

Identify

The identify call lets you identify a visiting user and capture any related information such as their name, email address, etc.

RudderStack does not store 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 at the same time. Therefore, for any of the calls supported by the Node.js SDK, you need to specify either userId or anonymousId every time.

A sample identify call is as shown:

Analytics.identify(
user_id: '12345',
traits: { email: "#{ username.email }", friends: 1 },
context: {ip: '10.81.20.10'}
)

The identify method parameters are as shown:

FieldTypePresenceDescription
anonymousIdStringOptionalSets the user ID for cases where there is no unique identifier for the user. Either userId or anonymousId is required.
userIdStringOptional, if anonymousId is already setUnique identifier for a particular user in your database.
contextObjectOptionalDictionary of information that provides context about a message. However, it is not directly related to the API call.
integrationsObjectOptionalA dictionary containing the destinations to be either enabled or disabled.
timestampDateOptionalThe timestamp of the message's arrival.
traitsObjectOptionalDictionary of the traits associated with the user, such as nameor email.

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: '12345',
event: 'Item Sold',
properties: { revenue: 9.95, shipping: 'Free' }
)

The track method parameters are as described below:

NameTypePresenceDescription
user_idStringRequiredThe developer identification for your user
eventStringRequiredName of the event being performed by the user
propertiesObjectOptionalDictionary of the properties associated with a particular event.
contextObjectOptionalDictionary of information that provides context about a message. However, it is not directly related to the API call.
timestampDateOptionalThe timestamp of the message's arrival.
anonymous_idStringOptionalSets the user ID for cases where there is no unique identifier for the user. Either userId or anonymousId is required.
integrationsObjectOptionalA dictionary containing the destinations to be either enabled or disabled.

Page

The page call allows you to record the page views on your website along with the other relevant information about the viewed page.

A sample page call is as shown:

Analytics.page(
user_id: userid,
category: 'Food',
name: 'Pizza',
properties: { url: 'https://dominos.com' }
)

The page method parameters are as described below:

FieldTypePresenceDescription
anonymousIdStringOptionalSets the user ID for cases where there is no unique identifier for the user. Either userId or anonymousId is required.
userIdStringOptional, if anonymousId is already setUnique identifier for a particular user in your database.
contextObjectOptionalDictionary of information that provides context about a message. However, it is not directly related to the API call.
integrationsObjectOptionalA dictionary containing the destinations to be either enabled or disabled.
nameStringRequiredName of the page being viewed.
propertiesObjectOptionalDictionary of the properties associated with the page being viewed, such as url and referrer
timestampDateOptionalThe timestamp of the message's arrival.

Group

The group call lets you associate an identified user to a group - either a company, project or a team and record any custom traits or properties associated with that group.

A sample group call is as shown:

Analytics.group(
user_id: '12345',
group_id: '12',
traits: { name: 'Company', description: 'Software'}
)

The group method parameters are as follows:

FieldTypePresenceDescription
anonymousIdStringOptionalSets the user ID for cases where there is no unique identifier for the user. Either userId or anonymousId is required.
userIdStringOptional, if anonymousId is already setUnique identifier for a particular user in your database.
contextObjectOptionalDictionary of information that provides context about a message. However, it is not directly related to the API call.
integrationsObjectOptionalA dictionary containing the destinations to be either enabled or disabled.
groupIdStringRequiredUnique identifier of the group, as present in your database.
traitsObjectOptionalDictionary of the properties or traits associated with the group, such as email or name.
timestampDateOptionalThe timestamp of the message's arrival.

Alias

The alias call lets you merge different identities of a known user.

alias is an advanced method that lets you change the tracked user's ID explicitly. This method is useful when managing identities for some of the downstream destinations.

A sample alias call is as shown:

Analytics.alias(previous_id: '12345', user_id: '123')

The alias method parameters are as mentioned below:

FieldTypePresenceDescription
userIdStringOptional, if anonymousId is already setUnique identifier for a particular user in your database.
contextObjectOptionalDictionary of information that provides context about a message. However, it is not directly related to the API call.
integrationsObjectOptionalA dictionary containing the destinations to be either enabled or disabled.
previousIdStringRequiredThe previous unique identifier of the user.
traitsObjectOptionalDictionary of the properties or traits associated with the group, such as email or name.
timestampDateOptionalThe timestamp of the message's arrival.

For a detailed explanation of the alias call, refer to our RudderStack API Specification guide.

Contact us

For any queries on any of the sections covered in this guide, you can contact us, or start a conversation on our Slack channel.

Contents