forked from segmentio/analytics-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·123 lines (110 loc) · 2.85 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
121
122
123
#!/usr/bin/env node
'use strict'
const program = require('commander')
const Analytics = require('.')
const pkg = require('./package')
const toObject = str => JSON.parse(str)
program
.version(pkg.version)
.option('-w, --writeKey <key>', 'the Segment write key to use')
.option('-h, --host <host>', 'the Segment API hostname to use')
.option('-t, --type <type>', 'the Segment message type')
.option('-u, --userId <id>', 'the user id to send the event as')
.option('-a, --anonymousId <id>', 'the anonymous user id to send the event as')
.option('-c, --context <context>', 'additional context for the event (JSON-encoded)', toObject)
.option('-i, --integrations <integrations>', 'additional integrations for the event (JSON-encoded)', toObject)
.option('-e, --event <event>', 'the event name to send with the event')
.option('-p, --properties <properties>', 'the event properties to send (JSON-encoded)', toObject)
.option('-n, --name <name>', 'name of the screen or page to send with the message')
.option('-t, --traits <traits>', 'the identify/group traits to send (JSON-encoded)', toObject)
.option('-g, --groupId <groupId>', 'the group id')
.option('-pid, --previousId <previousId>', 'the previous id')
.parse(process.argv)
if (program.args.length !== 0) {
program.help()
}
const writeKey = program.writeKey
const host = program.host
const type = program.type
const userId = program.userId
const anonymousId = program.anonymousId
const context = program.context
const integrations = program.integrations
const event = program.event
const properties = program.properties
const name = program.name
const traits = program.traits
const groupId = program.groupId
const previousId = program.previousId
const run = (method, args) => {
const analytics = new Analytics(writeKey, { host, flushAt: 1 })
analytics[method](args, err => {
if (err) {
console.error(err.stack)
process.exit(1)
}
})
}
switch (type) {
case 'track':
run('track', {
event,
properties,
userId,
anonymousId,
context,
integrations
})
break
case 'page':
run('page', {
name,
properties,
userId,
anonymousId,
context,
integrations
})
break
case 'screen':
run('screen', {
name,
properties,
userId,
anonymousId,
context,
integrations
})
break
case 'identify':
run('identify', {
traits,
userId,
anonymousId,
context,
integrations
})
break
case 'group':
run('group', {
groupId,
traits,
userId,
anonymousId,
context,
integrations
})
break
case 'alias':
run('alias', {
previousId,
userId,
anonymousId,
context,
integrations
})
break
default:
console.error('invalid type:', type)
process.exit(1)
}