RudderStack's Ruby SDK lets you track and send the events from your Ruby applications to the specified destinations.
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.
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 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 })
Sending events
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:
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 | The SDK automatically sets this identifier in cases where there is no unique identifier for the user. |
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 | The SDK automatically sets this identifier in cases where there is no unique identifier for the user. |
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://dominos.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 | The SDK automatically sets this identifier in cases where there is no unique identifier for the user. |
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
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:
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 | The SDK automatically sets this identifier in cases where there is no unique identifier for the user. |
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 | The SDK automatically sets this identifier in cases where there is no unique identifier for the user. |
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.
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:
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 | The SDK automatically sets this identifier in cases where there is no unique identifier for the user. |
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. |
Contact us
For more information on the topics covered on this page, email us or start a conversation in our Slack community.