Skip to content

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

fix: preserve Segment profile identifiers in request paths

Rudy Celekli9b69dfb7a60c
diff --git a/tests/clis/segment-profile-identifiers.test.cjs b/tests/clis/segment-profile-identifiers.test.cjs
new file mode 100644
index 0000000..f0dff21
--- /dev/null
+++ b/tests/clis/segment-profile-identifiers.test.cjs
@@ -0,0 +1,34 @@
+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/segment.js')
+function run(args, oracle = '', env = {}) {
+ const code = `global.fetch = async (url, options) => {
+ const assert = require('node:assert/strict');
+ const parsed = new URL(url);
+ const body = options.body ? JSON.parse(options.body) : null;
+ ${oracle}
+ return new Response(JSON.stringify({accepted:true, url, body}), {status:200});
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ const childEnv = {...process.env, SEGMENT_WRITE_KEY:'test-write', SEGMENT_ACCESS_TOKEN:'test-access', ...env}
+ for (const [key, value] of Object.entries(childEnv)) if (value === undefined) delete childEnv[key]
+ const r = spawnSync(process.execPath, ['-e', code], {encoding:'utf8', env:childEnv, timeout:10000})
+ return r
+}
+function result(r) { assert.equal(r.status,0,r.stderr); return JSON.parse(r.stdout) }
+for (const sub of ['traits','events']) test(`profile ${sub} preserves complete special-character user ID`, () => {
+ const id = 'tenant/[email protected]?#details'
+ const r = run(['profiles',sub,'--space-id','space123','--user-id',id], `assert.equal(parsed.search, ''); assert.equal(parsed.hash, ''); assert.equal(decodeURIComponent(parsed.pathname.split('/')[7]), 'user_id:' + ${JSON.stringify(id)}); assert.equal(parsed.pathname.split('/')[8], ${JSON.stringify(sub)});`)
+ assert.equal(result(r).accepted,true)
+})
+test('simple profile identifier remains routable', () => {
+ assert.equal(result(run(['profiles','traits','--space-id','space123','--user-id','user123'], "assert.equal(parsed.pathname, '/v1/spaces/space123/collections/users/profiles/user_id:user123/traits');")).accepted,true)
+})
+test('tracking event payload is unchanged', () => {
+ assert.deepEqual(result(run(['track','event','--user-id','tenant/customer','--event','Signup'], "assert.equal(parsed.pathname, '/v1/track');")).body, {userId:'tenant/customer',event:'Signup'})
+})
+test('profile dry-run uses the encoded path without leaking authorization', () => {
+ const r = run(['profiles','events','--space-id','space123','--user-id','a/b?c','--dry-run'], "throw new Error('unexpected fetch')")
+ const p = result(r); assert.equal(p.url,'https://profiles.segment.com/v1/spaces/space123/collections/users/profiles/user_id:a%2Fb%3Fc/events'); assert.equal(p.headers.Authorization,'***')
+})
diff --git a/tools/clis/segment.js b/tools/clis/segment.js
index d1c9d90..5468f62 100755
--- a/tools/clis/segment.js
+++ b/tools/clis/segment.js
@@ -156,13 +156,13 @@ async function main() {
case 'traits': {
if (!args['space-id']) { result = { error: '--space-id required' }; break }
if (!args['user-id']) { result = { error: '--user-id required' }; break }
- result = await profileApi('GET', `/spaces/${args['space-id']}/collections/users/profiles/user_id:${args['user-id']}/traits`)
+ result = await profileApi('GET', `/spaces/${encodeURIComponent(args['space-id'])}/collections/users/profiles/user_id:${encodeURIComponent(args['user-id'])}/traits`)
break
}
case 'events': {
if (!args['space-id']) { result = { error: '--space-id required' }; break }
if (!args['user-id']) { result = { error: '--user-id required' }; break }
- result = await profileApi('GET', `/spaces/${args['space-id']}/collections/users/profiles/user_id:${args['user-id']}/events`)
+ result = await profileApi('GET', `/spaces/${encodeURIComponent(args['space-id'])}/collections/users/profiles/user_id:${encodeURIComponent(args['user-id'])}/events`)
break
}
default:
diff --git a/tools/integrations/segment.md b/tools/integrations/segment.md
index 799c464..3d5a9be 100644
--- a/tools/integrations/segment.md
+++ b/tools/integrations/segment.md
@@ -157,3 +157,9 @@ analytics.page('Pricing');
- analytics
- emails
- ads
+
+### Profile identifiers in the CLI
+
+Pass the raw user ID to `profiles traits` or `profiles events`. The CLI URL-encodes
+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).