Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

Quickstart: Ruby


This tutorial will help you start sending data from your Ruby servers to Segment and any of Segment's destinations, using the Analytics for Ruby library. As soon as you're set up, you'll be able to enable any new destinations with minimal engineering hours.

If you want to dive deeper at any point, check out the Ruby library reference.


Step 1: Create a Source in the Segment app

step-1-create-a-source-in-the-segment-app page anchor

Before you begin, you need a Workspace (which is a container that holds all of the sources and destinations which are billed together for an organization).

Once you have a Workspace, create a new Ruby source:

  1. Click Add Source.
  2. From the source catalog page, click Ruby.
  3. Click Add Source again from the informational panel that appears to the right.
  4. Give the source a display name, and enter the URL the source will collect data from.

When you create a Source in the Segment web app, it tells the Segment servers that you'll be sending data from a specific source type. When you create (or change) a Source in the Segment app, Segment generates a new write key for that source. You use the write key in your code to tell the Segment servers where the data is coming from, so Segment can route it to your destinations and other tools.


Step 2: Install the gem

step-2-install-the-gem page anchor

If you're using bundler, add the following line to your project's Gemfile:

gem 'analytics-ruby', '~> 2.0.0', :require => 'segment/analytics'

Or, if you're using the gem directly from your application, you'll need to:

gem install analytics-ruby

Then you can initialize the gem with your Segment source's Write Key and an optional error handler, like so:

1
require 'segment/analytics'
2
3
Analytics = Segment::Analytics.new({
4
write_key: 'YOUR_WRITE_KEY',
5
on_error: Proc.new { |status, msg| print msg }
6
})

That will create an instance of Analytics that you can use to send data to Segment for your source.

If you're using Rails, you can stick that initialization logic in config/initializers/analytics_ruby.rb and omit the require call.

(information)

Segment's Ruby gem makes requests asynchronously

Asynchronous requests can sometimes be suboptimal and difficult to debug if you're pairing it with a queuing system like Sidekiq/delayed job/sucker punch/resqueue. If you'd prefer to use a gem that makes requests synchronously, consider simple_segment(link takes you to an external page), an API-compatible drop-in replacement for the standard gem that does its work synchronously inline. Big thanks to Mikhail Topolskiy(link takes you to an external page) for his stewardship of this alternative gem!

Once you've installed the gem, you're ready to identify your users with Segment's Identify method.


Step 3: Identify users

step-3-identify-users page anchor
(success)

Success!

For any of the different methods described in this quickstart, you can replace the properties and traits in the code samples with variables that represent the data collected.

The Identify method is how you tell Segment who the current user is. It includes a unique User ID and any optional traits you know about them. You can read more about it in the Identify reference.

Here's what a basic call to Identify might look like:

1
Analytics.identify(
2
user_id: 'f4ca124298',
3
traits: {
4
name: 'Michael Bolton',
5
email: 'mbolton@example.com',
6
created_at: DateTime.now
7
})

That's identifying Michael by his unique User ID (the one you know him by in your database) and labeling him with name and email traits.

When you're using Segment's Ruby library, you don't need to identify a user on every request they make to your servers. Instead, Segment recommends calling Identify a single time when the user's account is first created and only identifying again later when their traits change.

Once you've added an identify call, you can move on to...


The Track method is how you tell Segment about which actions your users are performing. Every action triggers an event, which can also have associated properties. You can read more about Track in the Track reference.

Here's what a call to Track might look like when a user signs up:

1
Analytics.track(
2
user_id: 'f4ca124298',
3
event: 'Signed Up',
4
properties: { plan: 'Enterprise' })

That's just telling you that your user just triggered the Signed Up event and chose your hypothetical 'Enterprise' plan. Properties can be anything you want to record. For example:

1
Analytics.track(
2
user_id: 'f4ca124298',
3
event: 'Article Bookmarked',
4
properties: {
5
title: 'Snow Fall',
6
subtitle: 'The Avalance at Tunnel Creek',
7
author: 'John Branch'
8
})

You'll want to track events that are indicators of success for your site, like Signed Up, Item Purchased or Article Bookmarked.

To get started, Segment recommends tracking just a few important events. You can always add more later!

Once you've added a few Track calls, you've successfully installed analytics tracking on your servers. Now you're ready to turn on any destination from Segment's catalog.


You might also want to consider installing Analytics.js so that you can use destinations that require being loaded in the browser, like live chat tools or user feedback systems.