This package has been deprecated

Author message:

Please use analytics.js (https://segment.com/docs/sources/website/analytics.js/) or analytics-node (https://www.npmjs.com/package/analytics-node)

segmentio

0.1.4 • Public • Published

Segment.io Node Client

Segment.io is a segmentation-focused analytics platform. If you haven't yet, register for a project here.

This is the official node.js client that wraps the Segment.io REST API .

You can use this driver to identify your users and track their events into your Segment.io project.

Design

This client uses batching to efficiently record your events in aggregate, rather than making an HTTP request every time. This means that it is safe to use in your web server controllers, or in back-end services without worrying that it will make too many HTTP requests and slow down the system.

You can control the batching behavior as described below.

How to Use

Install

npm install segmentio

Initialize the client

The default and easiest method for most apps is to just use the API client as a module. To get started, just use the following:

var segmentio = require('segmentio');
segmentio.init(isProduction ? 'LIVE_API_KEY' : 'TEST_API_KEY');

Then whenever you require('segmentio') from your app, you'll have access to the same client.

You can also create your own client if you'd like a little more customization. The API is exactly the same.

var segmentio = new require('segmentio').Client();
segmentio.init(isProduction ? 'LIVE_API_KEY' : 'TEST_API_KEY');

Identify a User

Identifying a user ties all of their actions to an ID you recognize and records user traits you can segment by.

segmentio.identify({
    sessionId : String
    userId    : String
    traits    : Object,
    timestamp : Date
});

sessionId (string) is a unique id associated with an anonymous user before they are logged in. If the user is logged in, you can use null here.

userId (string) is usually an email, but any unique ID will work. This is how you recognize a signed-in user in your system. Note: it can be null if the user is not logged in. By explicitly identifying a user, you tie all of their actions to their identity. This makes it possible for you to run things like segment-based email campaigns.

traits (object) is a dictionary with keys like “Subscription Plan” or “Favorite Genre”. You can segment your users by any trait you record. Once you record a trait, no need to send it again, so the traits argument is optional.

timestamp (Date) is a Date object representing when the identify took place. It is optional, and if not provided, server-now will be used.

segmentio.identify({
    sessionId : 'DKGXt384hFDT82D',
    userId : 'ilya@segment.io',
    traits : {
        'First Name': 'Ilya',
        'Last Name': 'Volodarsky',
        'Subscription Plan': 'Premium',
        'On Mailing List': true
    },
    timestamp: new Date('2012-12-02T00:30:08.276Z')
});
 

Track an Action

Whenever a user triggers an event on your site, you’ll want to track it so that you can analyze and segment by those events later.

segmentio.track({
    sessionId  : String,
    userId     : String,
    event      : String,
    properties : Object,
    timestamp  : Date
});

sessionId (string) is a unique id associated with an anonymous user before they are logged in. If the user is logged in, you can use null here. Either this or the userId must be supplied.

userId (string) is usually an email, but any unique ID will work. This is how you recognize a signed-in user in your system. Note: it can be null if the user is not logged in. By explicitly identifying a user, you tie all of their actions to their identity. This makes it possible for you to run things like segment-based email campaigns. Either this or the sessionId must be supplied.

event (string) is a human readable description like "Played a Song", "Printed a Report" or "Updated Status". You’ll be able to segment by when and how many times each event was triggered.

properties (object) is a dictionary with items that describe the event in more detail. This argument is optional, but highly recommended—you’ll find these properties extremely useful later.

timestamp (Date) is a Date object representing when the track took place. It is optional, and if not provided, server-now will be used.

 
segmentio.track({
    sessionId : 'DKGXt384hFDT82D',
    userId : 'ilya@segment.io',
    event : 'Listened to a song',
    properties : {
        'Title': 'Eleanor Rigby',
        'Artist': 'Beatles',
        'Playlist': 'Popular'
    },
    timestamp: new Date('2012-12-02T00:30:08.276Z')
});

Adjusting Batching

You can adjust the flush triggers in the client. Take a look into the source.

Error Handling and Integration

In order to handle errors, the node client will emit every time an error occurs. To prevent the segmentio client from crashing your server with an unhandled exception, it emits on err rather than the more conventional error.

During integration, we recommend listening on the err event to make sure that all the data is being properly recorded:

segmentio.on('err', function() { console.warn('A segment.io error occured', err); });

You may also listen on the following events for more fine-grained granularity.

  • flushed - when the client has sent its queue to the server
  • flushing - when the client is in the process of submitting its queue
  • err - when an error in the tracking code or connection happens.
  • initialized - when the client is initialized and able to record events.

Testing

npm test

License

(The MIT License)

Copyright (c) 2012 Segment.io Inc. friends@segment.io

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i segmentio

Weekly Downloads

5

Version

0.1.4

License

none

Last publish

Collaborators

  • cjo2
  • achandrashekaran
  • aparna.singhal
  • ynguyen
  • timmyzsearcy
  • igracheva-twilio
  • yashnit-segment
  • nanotimm
  • srivig21
  • chenchensegmentcom
  • sanket.mishra
  • lweimersegment
  • rcheedhalla
  • jxin_twilio
  • odoren_segment
  • aditi.raveesh
  • dltnbrks-segment
  • sai-patanjali
  • himanshuph
  • alecjacobs-segment
  • sshaikh_segment
  • john.lee1100
  • dazu70
  • brian.aguirre
  • pooja.patil
  • segment_fan
  • nlubchenco
  • jamezhutwilio
  • knksmith57-segment
  • jsh-wu
  • nithin-benny
  • poojasegment
  • ebru.odok
  • bannapple
  • rodhilton_twilio
  • guthriesegment
  • srishti-nema
  • cdelaomartinez
  • miguelpdiaz8
  • sundareswar.jayakumar
  • gbatra
  • spencerattick
  • akodankiry
  • nanette.ranes
  • gsolis_segment
  • nitinpm432
  • aadityabhatt10
  • amigandhi
  • segmentsean
  • hmohanram_seg
  • jrupasinghe
  • myrontin.segment
  • devthale
  • smccoy-twilio
  • seg-leonelsanches
  • jibrang
  • sethgrid_segment
  • light-bringer-blr
  • aubreysine
  • ed-twilion-npm
  • harsh-joshi99
  • irfan.ali.segment
  • kbhargavaram-sg
  • needcaffeine
  • nat-grid
  • wlumsegment
  • vivek-26
  • moyara2
  • bala.singareddy
  • gbbastos
  • akash.gautam07
  • preetyp
  • viveksainaneesegment
  • msaraf
  • kjoerres
  • rokatyal
  • ainatancinco
  • anton-vylushchak
  • sowjanyasegment
  • alayvora
  • msaunders-segment
  • tw-dgarcia
  • parag.panda
  • blangtwilio
  • ryanrouleau-segment
  • twjosiah
  • mcullenmeyer
  • david.anusontarangkul.segment
  • mckern_segment
  • segment-admin
  • nainy.agrawal
  • tdibacco
  • sudojatin
  • nageshgolem
  • brandonheyer-segment
  • alfrimpong
  • dobrin.ganev
  • ankit.gupta.unthinkable
  • marinhero
  • benattwilio
  • bharath.boregowda
  • conniechen
  • sungju.jin
  • pooyaj
  • yli119
  • ea_segment
  • emilyjia
  • kx-segment
  • cvillela_segment
  • xinghao.huang
  • harsh.vardhan
  • joe.ayoub.segment
  • gkochar123
  • rollcode
  • ariel.silvestri
  • cherylj-segment
  • leifdreizler
  • immanoj
  • aaronklish
  • michelr
  • maneesh.dharma29
  • msolorzano-segment
  • brianhumphreystwilio
  • jfehrman.segment
  • joetessy
  • pmunin
  • jalexy12
  • jbandi-twilio
  • prayansh-twilio
  • dominicbarnes
  • brandon.scott-segment
  • bgillan
  • phillip.thomas
  • ricardo.rossi
  • forgetfulfellow
  • fauzy.yy
  • mayur-pitale
  • dbaik-twilio-segment
  • seg-rustybailey
  • tanya.gupta.segment
  • pmiller-twilio
  • nevermore2022
  • aishikawaki
  • csayuso
  • mcoulibali
  • shupadhyay
  • jahood-twilio
  • saisagarkappaganthula
  • rmukundan
  • arubiochavez
  • shuvrajit9904
  • s3gm3nt
  • ama0223
  • tbrennanj
  • dangmai-segment
  • shraddha-twilio
  • azhaotwilio
  • sa-jsooter
  • enyi.asonye
  • afsha-nazim-seg
  • mschaszberger
  • lnamba
  • varadarajan-tw
  • seanhan-segment
  • replatero
  • sayan-das-in
  • justeen
  • sausingh
  • jgabe13
  • meg1000
  • funlu
  • ashwitha.bg
  • whaider_twilio
  • tcgilbert
  • kevinburkesegment
  • felttrip
  • prabhnoor1997
  • akashyap91
  • clintz.seg
  • karimkeshwani
  • wally.tg
  • rhall-twilio
  • yash-twilio
  • brookstaylorjr
  • shayan-golafshani
  • lerahulram
  • mugelstad
  • hdamani
  • rrivera-segment
  • sethnutetwilio
  • manali-bhosale
  • chtoombs
  • sethsegment
  • eric-hyde
  • elmoselyee
  • michaelghseg
  • jayakrishnannair
  • lateefat
  • maryam.sharif
  • wdbetts
  • ryanligon
  • sindhusegment
  • lfdelossantos
  • aramakrishnan
  • segment-camden
  • vbatanov
  • lluque-twilio
  • jair.aviles
  • pmaid
  • song4you
  • peterdemartini
  • emmy.byrne
  • vincen7tran
  • dean-huynh
  • cdignam-segment
  • abhinavsureka
  • arunlalam-segment
  • cjradek
  • neeharikakondipati
  • simpixelated
  • chihchun-twilio
  • acharles14
  • jyim008
  • hema-segment
  • oscb
  • krousseau
  • sachinwathore
  • fhalim-segment
  • cfree
  • hjoonpm
  • celine-segment
  • pmcanseco-segment
  • masira
  • amillet89
  • cholt002
  • av-segment
  • aghotikar
  • vikrant-segment
  • larryatsegment
  • ariel_segment
  • scruwys1
  • lew-gordon
  • kyliepedersen
  • jinapark
  • segmentio
  • rajulvadera
  • lpediredla
  • n2parko
  • tyson_segment
  • bgamwell
  • uditmehta
  • salolivares
  • erikdw
  • chenxiangzhang
  • mericsson
  • prayansh-segmentt
  • jeremylarkin
  • bsneed
  • danieljackins
  • segment-seth
  • james9446
  • priscilla.giatti
  • kellylu
  • nlsun
  • drew-thompson
  • segment-jsingh
  • andrius-segment
  • valerieernst
  • kelcook
  • gilomer
  • marcelopv
  • eric.rogner
  • kdharaiya
  • jon.anderson-at-segment.com
  • stacy.song
  • rexatsegment
  • nickaguilar
  • bradenbecker
  • reneewang
  • dan.lasky
  • sam.tapia
  • vikramkumar19
  • mpriyad25
  • jeremy.parker
  • smidges
  • calvinfo