Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.
JavaScript agent-skillsai-agentsai-seoclaudeclaude-codeclaude-code-pluginclaude-skillscodexcontent-marketingconversion-rate-optimizationcopywritingcursoremail-marketinggenerative-engine-optimizationgrowth-marketingmarketingmarketing-automationsaas-marketingseoskill-md
feat(amplitude): support device-only event identities
Rudy Celekli1f84c284a984
diff --git a/tests/clis/amplitude-device-identity.test.cjs b/tests/clis/amplitude-device-identity.test.cjsnew file mode 100644index 0000000..2bcf3f4--- /dev/null+++ b/tests/clis/amplitude-device-identity.test.cjs@@ -0,0 +1,42 @@+const { test } = require('node:test')+const assert = require('node:assert/strict')+const { spawnSync } = require('node:child_process')+const path = require('node:path')+const cli = path.resolve(__dirname, '../../tools/clis/amplitude.js')+function run(extra, allowFetch = true) {+ const args = ['track', 'event', '--event-type', 'Viewed Pricing', ...extra]+ const code = `global.fetch=async(url,options)=>{if(!${allowFetch})throw new Error('unexpected fetch');return {status:200,text:async()=>JSON.stringify({url,body:JSON.parse(options.body)})}};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, AMPLITUDE_API_KEY: 'fixture-only' } })+}+test('pre-signup event uses documented device identity without inventing a user ID', () => {+ const r = run(['--device-id', 'visitor-device-123', '--properties', '{"experiment":"pricing-b"}'])+ assert.equal(r.status, 0, r.stderr)+ const out = JSON.parse(r.stdout)+ assert.equal(out.url, 'https://api2.amplitude.com/2/httpapi')+ assert.deepEqual(out.body.events, [{ device_id: 'visitor-device-123', event_type: 'Viewed Pricing', event_properties: { experiment: 'pricing-b' } }])+})+test('known users can include device identity while user-only requests remain unchanged', () => {+ const both = JSON.parse(run(['--user-id', 'customer-123', '--device-id', 'visitor-device-123']).stdout)+ assert.equal(both.body.events[0].user_id, 'customer-123')+ assert.equal(both.body.events[0].device_id, 'visitor-device-123')+ const existing = JSON.parse(run(['--user-id', 'customer-123']).stdout)+ assert.deepEqual(existing.body.events[0], { user_id: 'customer-123', event_type: 'Viewed Pricing' })+})+test('device-only preview retains event identity while masking API key', () => {+ const r = run(['--device-id', 'visitor-device-123', '--dry-run'], false)+ assert.equal(r.status, 0, r.stderr)+ const out = JSON.parse(r.stdout)+ assert.equal(out.body.api_key, '***')+ assert.equal(out.body.events[0].device_id, 'visitor-device-123')+})+test('missing identities still fail validation without making a request', () => {+ const r = run([], false)+ assert.match(JSON.parse(r.stdout).error, /user-id.*device-id/)+})+for (const extra of [['--device-id'], ['--device-id', ''], ['--user-id'], ['--user-id', 'customer-123', '--device-id']]) {+ test(`identity flags require actual string values: ${extra.join(' ')}`, () => {+ const r = run(extra, false)+ assert.equal(r.status, 1)+ assert.match(JSON.parse(r.stderr).error, /requires a non-empty string/)+ })+}diff --git a/tools/clis/amplitude.js b/tools/clis/amplitude.jsindex 418d11d..fb2402c 100755--- a/tools/clis/amplitude.js+++ b/tools/clis/amplitude.js@@ -94,10 +94,14 @@ async function main() {case 'track':switch (sub) {case 'event': {- if (!args['user-id']) { result = { error: '--user-id required' }; break }+ for (const key of ['user-id', 'device-id']) {+ if (args[key] !== undefined && (typeof args[key] !== 'string' || args[key].trim().length === 0)) throw new Error(`--${key} requires a non-empty string`)+ }+ if (!args['user-id'] && !args['device-id']) { result = { error: '--user-id or --device-id required' }; break }if (!args['event-type']) { result = { error: '--event-type required' }; break }const event = {- user_id: args['user-id'],+ ...(args['user-id'] !== undefined ? { user_id: args['user-id'] } : {}),+ ...(args['device-id'] !== undefined ? { device_id: args['device-id'] } : {}),event_type: args['event-type'],}if (args.properties) {@@ -176,7 +180,7 @@ async function main() {result = {error: 'Unknown command',usage: {- track: 'track [event --user-id <id> --event-type <type> [--properties <json>] | batch --events <json>]',+ track: 'track [event [--user-id <id>] [--device-id <id>] --event-type <type> [--properties <json>] | batch --events <json>]',users: 'users activity --user-id <id>',export: "export events --start <YYYYMMDDThh> --end <YYYYMMDDThh> (ZIP in base64 body; decode with Buffer.from(result.body, 'base64'))",retention: 'retention get --start <YYYYMMDD> --end <YYYYMMDD> [--event <type>]',diff --git a/tools/integrations/amplitude.md b/tools/integrations/amplitude.mdindex 87d3a59..e24a3bf 100644--- a/tools/integrations/amplitude.md+++ b/tools/integrations/amplitude.md@@ -144,3 +144,19 @@ amplitude.track('Feature Used', {- analytics- ab-testing- onboarding++## Tracking before signup++`track event` accepts `--device-id` without `--user-id` for events before an+account exists. At least one identity is required; when both are known, include+both to send them together. Device IDs are identifiers, not a privacy guarantee.++```bash+node tools/clis/amplitude.js track event --device-id visitor-device-123 \+ --event-type "Viewed Pricing" --properties '{"experiment":"pricing-b"}' --dry-run+```++The CLI leaves existing user-only payloads intact and masks the API key in+previews. Amplitude's default minimum identifier length is five characters;+provider-side identity and project ingestion rules still apply. See the+[HTTP V2 API](https://amplitude.com/docs/apis/analytics/http-v2).