This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
forked from segmentio/analytics-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·120 lines (108 loc) · 3.91 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env node
'use strict'
const assert = require('assert')
const program = require('commander')
const Analytics = require('.')
const pkg = require('./package')
const run = (method, options) => {
const 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.')
const analytics = new Analytics(writeKey, { flushAt: 1 })
analytics[method](options, err => {
if (err) {
console.error(err.stack)
process.exit(1)
}
})
}
const toDate = str => new Date(str)
const toObject = str => JSON.parse(str)
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((event, options) => {
run('track', {
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(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(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(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(options => {
run('alias', {
userId: options.user,
previousId: options.previous
})
})
program.parse(process.argv)
if (program.args.length === 0) {
program.help()
}