Segment

image0

Segment is a simple analytics API that transforms business analytics data and routes it to other analytics services, such as Google Analytics, Mixpanel, KissMetrics, and Librato.

Segment Set-up

To get started with Segment create an account and follow the set up instructions. To activate the Librato integration you will need a token:

Create an API token

We recommend creating a Record Only API token specifically for this service. You can learn how in the knowledge base article about Librato API Tokens.

Copy the new API token and paste it into the appropriate field in the Librato integration.

Tracking Metrics

segment-io-integration

When you toggle on Librato in Segment, their CDN is updated within 5-10 minutes with their custom Librato provider inside analytics.js.

Since Librato only records custom events, no metrics will appear in Librato until you start using the API as outlined in their docs.

Librato supports their track method for both client-side and server-side.

For more configuration details such as API methods and how to override the source dimension, check out the Librato Integration page on Segment’s site.

NOTE: Some of the code examples on Segment’s page are incorrect. You can post multiple values in the property such as

properties: {value: 2, revenue: 14.99}

but anything other than value will be ignored. Also, to include the source dimension, use context as a parameter (Librato for Python).

Example

Here’s some example code that uses the ruby gem that Segment provides:

require 'analytics-ruby'

Analytics = AnalyticsRuby # Alias for convenience
segment_token = 'asdfasdf' # The secret write key for my project

Analytics.init({
    secret: segment_token,
    on_error: Proc.new { |status, msg| print msg }  # Optional error handler
})

while true
    Analytics.track(user_id: 123, writeKey: segment_token, event: 'segment.librato',
        properties: { value: rand(100) }, context: { source:'web-server-10' })
    Analytics.track(user_id: 234, writeKey: segment_token, event: 'segment.librato',
        properties: { value: rand(100) }, context: { source:'web-server-11' })
    Analytics.track(user_id: 456, writeKey: segment_token, event: 'segment.librato',
        properties: { value: rand(100) }, context: { source:'web-server-12' })
    Analytics.track(user_id: 789, writeKey: segment_token, event: 'segment.librato',
        properties: { value: rand(100) }, context: { source:'web-server-13' })
sleep (30)
end

This will generate a graph for one metric (segment.librato) with four sources (web-server-10…13).

image1

You will notice that the Segment integration switches on Service-Side Aggregation (SSA) by default.

Here’s an example using Python:

userID = '12345'
analytics.track(userID, 'event', {
      'value': 1
 }, {
      'Librato': {
           'source': userID
           }
 })

Here’s an example using CURL:

curl https://api.segment.io/v1/track \
    -d userId=123 \
    -d writeKey=segment_token \
    -d event=segment.librato \
    -d properties[value]=2 \
    -d context[source]=web-server-10.0.0.1

Here’s an example using JavaScript:

analytics.track('segment.librato',
    { value: 2 },
    { context: { source: 'custom_source' }}
);