Skip to content

SamyPesse/segment.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

segment.js

NPM version

Segment.com client that works in the browser (browserify) and Node.js.

Installation

$ npm install segment.js

Usage

Create a Segment.com client:

var Segment = require('segment.js');

var analytics = new Segment({
    writeKey: "your-project-write-key"
});

Default options are:

{
    // Endpoint for the Segment.com API
    endpoint: 'https://api.segment.io/v1/',

    // Write key for your project
    writeKey: "",

    // The number of milliseconds to delay flushing events
    flushWait: 1000,

    // The maximum time flush is allowed to be delayed before it’s invoked
    flushMaxWait: 100
}
Identify

The identify method is how you tie one of your users to a recognizable userId and traits.

analytics.identify({
    userId: '1e810c197e',
    traits: {
        name: 'Bill Lumbergh',
        email: 'bill@initech.com'
    }
});
Track

The track method lets you record any actions your users perform. Here’s a basic example:

analytics.track({
    event: 'Signed Up',
    properties: {
        plan: 'Startup',
        source: 'Analytics Academy'
    }
});
Flush

You can manually flush events to Segment.com using the analytics.flush([sync]) method:

analytics.flush();

It's also possible in the browser to synchronously send events, for example:

window.onbeforeunload = function() {
    analytics.track('Page Closed');
    analytics.flush(true);
};

About

Segment.com client for the browser and node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published