#!/usr/bin/env node var Analytics = require('..') var assert = require('assert') var pkg = require('../package.json') var program = require('commander') program .version(pkg.version) .option('-w, --write-key ', 'the segment write key to use') program .command('track ') .description('track a user event') .option('-u, --user ', 'the user id to send the event as') .option('-a, --anonymous ', 'the anonymous user id to send the event as') .option('-p, --properties ', 'the event properties to send (JSON-encoded)', toObject) .option('-t, --timestamp ', 'the date of the event', toDate) .option('-c, --context ', 'additional context for the event (JSON-encoded)', toObject) .action(function (event, options) { run('track', { event: event, userId: options.user, anonymousId: options.anonymous, properties: options.properties, timestamp: options.timestamp, context: options.context }) }) program .command('page') .description('track a page view') .option('-u, --user ', 'the user id to send the event as') .option('-n, --name ', 'the name of the page') .option('-C, --category ', 'the category of the page') .option('-p, --properties ', 'attributes of the page (JSON-encoded)', toObject) .option('-t, --timestamp ', 'the date of the event', toDate) .option('-c, --context ', 'additional context for the event (JSON-encoded)', toObject) .action(function (options) { run('page', { userId: options.user, name: options.name, category: options.category, properties: options.properties, timestamp: options.timestamp, context: options.context }) }) program .command('identify') .description('identify a user') .option('-u, --user ', 'the user id to send the event as') .option('-T, --traits ', 'the user traits to send (JSON-encoded)', toObject) .option('-t, --timestamp ', 'the date of the event', toDate) .option('-c, --context ', 'additional context for the event (JSON-encoded)', toObject) .action(function (options) { run('identify', { userId: options.user, traits: options.traits, timestamp: options.timestamp, context: options.context }) }) program .command('group') .description('identify a group of users') .option('-u, --user ', 'the user id to send the event as') .option('-a, --anonymous ', 'the anonymous id to associate with this group') .option('-g, --group ', 'the group id to associate this user with') .option('-T, --traits ', 'attributes about the group (JSON-encoded)', toObject) .option('-t, --timestamp ', 'the date of the event', toDate) .option('-c, --context ', 'additional context for the event (JSON-encoded)', toObject) .action(function (options) { run('group', { userId: options.user, anonymousId: options.anonymous, groupId: options.group, traits: options.traits, timestamp: options.timestamp, context: options.context }) }) program .command('alias') .description('remap a user to a new id') .option('-u, --user ', 'the user id to send the event as') .option('-p, --previous ', 'the previous user id (to add the alias for)') .action(function (options) { run('alias', { userId: options.user, previousId: options.previous }) }) program.parse(process.argv) if (program.args.length === 0) { program.help() } function run (method, options) { var writeKey = process.env.SEGMENT_WRITE_KEY || program.writeKey assert(writeKey, 'you need to define your write key via the $SEGMENT_WRITE_KEY environment variable or the --write-key flag') var analytics = new Analytics(writeKey, { flushAt: 1 }) analytics[method](options, done) } function toDate (str) { return new Date(str) } function toObject (str) { return JSON.parse(str) } function done (err) { if (err) { console.error(err.stack) process.exit(1) } else { process.exit(0) } }