130 lines
4.0 KiB
JavaScript
Executable File
130 lines
4.0 KiB
JavaScript
Executable File
#!/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 <key>', 'the segment write key to use')
|
|
|
|
program
|
|
.command('track <event>')
|
|
.description('track a user event')
|
|
.option('-u, --user <id>', 'the user id to send the event as')
|
|
.option('-a, --anonymous <id>', 'the anonymous user id to send the event as')
|
|
.option('-p, --properties <data>', 'the event properties to send (JSON-encoded)', toObject)
|
|
.option('-t, --timestamp <date>', 'the date of the event', toDate)
|
|
.option('-c, --context <data>', '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 <id>', 'the user id to send the event as')
|
|
.option('-n, --name <name>', 'the name of the page')
|
|
.option('-C, --category <category>', 'the category of the page')
|
|
.option('-p, --properties <data>', 'attributes of the page (JSON-encoded)', toObject)
|
|
.option('-t, --timestamp <date>', 'the date of the event', toDate)
|
|
.option('-c, --context <data>', '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 <id>', 'the user id to send the event as')
|
|
.option('-T, --traits <data>', 'the user traits to send (JSON-encoded)', toObject)
|
|
.option('-t, --timestamp <date>', 'the date of the event', toDate)
|
|
.option('-c, --context <data>', '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 <id>', 'the user id to send the event as')
|
|
.option('-a, --anonymous <id>', 'the anonymous id to associate with this group')
|
|
.option('-g, --group <id>', 'the group id to associate this user with')
|
|
.option('-T, --traits <data>', 'attributes about the group (JSON-encoded)', toObject)
|
|
.option('-t, --timestamp <date>', 'the date of the event', toDate)
|
|
.option('-c, --context <data>', '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 <id>', 'the user id to send the event as')
|
|
.option('-p, --previous <id>', '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)
|
|
}
|
|
}
|