Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

Merge PR #760

Corey Haines55cc6c9c7d1a
diff --cc tools/clis/segment.js
index 05cedc2,fbecf71..d231d12
--- a/tools/clis/segment.js
+++ b/tools/clis/segment.js
@@@ -81,14 -81,26 +81,34 @@@ function parseArgs(args)
const args = parseArgs(rawArgs)
const [cmd, sub, ...rest] = args._
+function eventIdentity() {
+ const identity = {}
+ if (typeof args['user-id'] === 'string' && args['user-id']) identity.userId = args['user-id']
+ if (typeof args['anonymous-id'] === 'string' && args['anonymous-id']) identity.anonymousId = args['anonymous-id']
+ if (!Object.keys(identity).length) throw new Error('--user-id or --anonymous-id required')
+ return identity
+}
+
+ function withDeliveryMetadata(body) {
+ if (args.timestamp !== undefined) {
+ const value = args.timestamp
+ const parts = typeof value === 'string' ? /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/.exec(value) : null
+ const [year, month, day, hour, minute, second] = parts ? parts.slice(1).map(Number) : []
+ const leap = year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)
+ const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+ if (!parts || month < 1 || month > 12 || day < 1 || day > days[month - 1] || hour > 23 || minute > 59 || second > 59 || !Number.isFinite(Date.parse(value))) {
+ throw new Error('--timestamp must be a valid ISO-8601 datetime with a timezone')
+ }
+ body.timestamp = value
+ }
+ if (args['message-id'] !== undefined) {
+ const value = args['message-id']
+ if (typeof value !== 'string' || value.length < 1 || value.length > 100) throw new Error('--message-id must contain 1 to 100 characters')
+ body.messageId = value
+ }
+ return body
+ }
+
async function main() {
let result
@@@ -180,10 -195,11 +200,11 @@@
result = {
error: 'Unknown command',
usage: {
- track: 'track event [--user-id <id>] [--anonymous-id <id>] --event <name> [--properties <json>]',
- identify: 'identify user [--user-id <id>] [--anonymous-id <id>] [--traits <json>]',
- page: 'page view [--user-id <id>] [--anonymous-id <id>] [--name <name>] [--properties <json>]',
- track: 'track event --user-id <id> --event <name> [--properties <json>]',
- identify: 'identify user --user-id <id> [--traits <json>]',
- page: 'page view --user-id <id> [--name <name>] [--properties <json>]',
++ track: 'track event [--user-id <id>] [--anonymous-id <id>] --event <name> [--properties <json>] [--timestamp <ISO>] [--message-id <id>]',
++ identify: 'identify user [--user-id <id>] [--anonymous-id <id>] [--traits <json>] [--timestamp <ISO>] [--message-id <id>]',
++ page: 'page view [--user-id <id>] [--anonymous-id <id>] [--name <name>] [--properties <json>] [--timestamp <ISO>] [--message-id <id>]',
batch: 'batch send --events <json_array>',
+ metadata: 'Single track/identify/page calls: [--timestamp <ISO_datetime>] [--message-id <1_to_100_characters>]',
profiles: 'profiles [traits|events] --space-id <id> --user-id <id>',
}
}
diff --cc tools/integrations/segment.md
index 0271bcc,db62222..914739a
--- a/tools/integrations/segment.md
+++ b/tools/integrations/segment.md
@@@ -164,33 -164,20 +164,51 @@@ Pass the raw user ID to `profiles trait
the value so characters such as `/`, `+`, `?`, and `#` remain part of the user ID.
Do not pre-encode it. See [Profile API identifiers](https://www.twilio.com/docs/segment/unify/profile-api).
+## CLI anonymous and known identities
+
+The `track event`, `page view`, and `identify user` commands accept either
+`--user-id`, `--anonymous-id`, or both. Use an existing anonymous identifier
+for pre-signup activity rather than inventing a known customer ID:
+
+```bash
+node tools/clis/segment.js track event --anonymous-id anon-owned \
+ --event 'Pricing Viewed' --properties '{"plan":"pro"}' --dry-run
+
+# When this same visitor becomes a known user
+node tools/clis/segment.js identify user --user-id user-owned \
+ --anonymous-id anon-owned --traits '{"plan":"paid"}' --dry-run
+```
+
+The CLI passes the IDs through unchanged; it does not generate an anonymous
+ID, store identity state, or infer that two people are the same. Reuse the
+actual identifier associated with that visitor and supply both only when the
+association is known. Destination identity handling varies: acceptance by
+Segment does not prove every destination merged the history. An anonymous ID
+is an identifier, not a guarantee of anonymity or permission to collect data.
+Send events only when the collection is already authorized.
+
+At least one ID is required before sending these single-event requests.
+Existing `--user-id` calls and raw batch payloads are unchanged. Profile
+lookups continue to require a known `--user-id`; `--anonymous-id` does not
+change their resource path. Previews mask the write key and make no request.
+
+Official [common identity fields](https://www.twilio.com/docs/segment/connections/spec/common)
+and [Identify semantics](https://www.twilio.com/docs/segment/connections/spec/identify).
++
+ ## Importing and retrying individual events
+
+ Single `track event`, `identify user`, and `page view` calls accept
+ `--timestamp` and `--message-id`. Use an ISO datetime with a timezone to retain
+ when a historical event occurred, and reuse the source event's ID when retrying:
+
+ ```bash
+ node tools/clis/segment.js track event --user-id customer-1 --event "Order Completed" \
+ --timestamp 2024-04-12T10:15:30Z --message-id order-123 --dry-run
+ ```
+
+ Both fields are omitted when flags are absent, leaving Segment's defaults
+ intact. Message IDs must contain 1–100 characters. For `batch send`, place the
+ metadata on each event in `--events`; these individual-call flags do not rewrite
+ batches. See [common fields](https://www.twilio.com/docs/segment/connections/spec/common)
+ and [duplicate handling](https://www.twilio.com/docs/segment/guides/duplicate-data).
+ Downstream destinations can handle these fields differently.