Skip to content

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

feat(mixpanel): select regional ingestion, query and export hosts (#744)

Rudy Mizrahi Celekli0f8d89961cdf
diff --git a/tests/clis/mixpanel-region.test.cjs b/tests/clis/mixpanel-region.test.cjs
new file mode 100644
index 0000000..0dc94f7
--- /dev/null
+++ b/tests/clis/mixpanel-region.test.cjs
@@ -0,0 +1,30 @@
+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/mixpanel.js')
+const hosts={us:['api.mixpanel.com','mixpanel.com','data.mixpanel.com'],eu:['api-eu.mixpanel.com','eu.mixpanel.com','data-eu.mixpanel.com'],in:['api-in.mixpanel.com','in.mixpanel.com','data-in.mixpanel.com']}
+const operations=[
+ {args:['track','event','--distinct-id','owned','--event','Owned'],host:0,path:'/track',method:'POST'},
+ {args:['profiles','set','--distinct-id','owned','--properties','{"plan":"owned"}'],host:0,path:'/engage',method:'POST'},
+ {args:['query','events','--project-id','42'],host:1,path:'/api/2.0/insights',method:'POST'},
+ {args:['funnels','get','--funnel-id','owned','--project-id','42'],host:1,path:'/api/2.0/funnels',method:'GET'},
+ {args:['retention','get','--from-date','2026-01-01','--to-date','2026-01-02','--project-id','42'],host:1,path:'/api/2.0/retention',method:'GET'},
+ {args:['export','events','--from-date','2026-01-01','--to-date','2026-01-02','--project-id','42'],host:2,path:'/api/2.0/export',method:'GET'}
+]
+function run(args,region,oracle=''){
+ const code=`global.fetch=async(url,options)=>{const assert=require('node:assert/strict');const parsed=new URL(url);${oracle};return new Response(JSON.stringify({owned:true}),{status:200})};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ const env={...process.env,MIXPANEL_TOKEN:'owned-token',MIXPANEL_API_KEY:'owned-key',MIXPANEL_SECRET:'owned-secret'}
+ if(region===undefined)delete env.MIXPANEL_REGION;else env.MIXPANEL_REGION=region
+ return spawnSync(process.execPath,['-e',code],{env,encoding:'utf8',timeout:10000})
+}
+function output(r){assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout)}
+for(const region of Object.keys(hosts))for(const op of operations)test(`${region} ${op.args.slice(0,2).join(' ')} selects its documented API family`,()=>{
+ const oracle=`assert.equal(parsed.hostname,${JSON.stringify(hosts[region][op.host])});assert.equal(parsed.pathname,${JSON.stringify(op.path)});assert.equal(options.method,${JSON.stringify(op.method)});`
+ assert.deepEqual(output(run(op.args,region,oracle)),{owned:true})
+ const preview=output(run([...op.args,'--dry-run'],region,"throw new Error('unexpected fetch')"));assert.equal(new URL(preview.url).hostname,hosts[region][op.host]);assert.equal(new URL(preview.url).pathname,op.path)
+ if(op.host===0){assert.equal(preview.body[0].properties?.token??preview.body[0].$token,'***')}else assert.equal(preview.headers.Authorization,'***')
+})
+test('omitted region preserves the US endpoint',()=>{assert.deepEqual(output(run(operations[0].args,undefined,"assert.equal(parsed.hostname,'api.mixpanel.com')")),{owned:true})})
+for(const region of ['EU','unknown','https://owned.invalid'])test(`invalid region fails before transport ${region}`,()=>{const r=run(operations[0].args,region,"throw new Error('unexpected fetch')");assert.notEqual(r.status,0);assert.match(r.stderr,/MIXPANEL_REGION/);assert.doesNotMatch(r.stderr,/unexpected fetch/)})
+test('no-argument help succeeds despite an invalid region',()=>{assert.equal(output(run([],'unknown',"throw new Error('unexpected fetch')")).error,'Unknown command')})
diff --git a/tools/clis/mixpanel.js b/tools/clis/mixpanel.js
index 0572706..4c2bb0b 100755
--- a/tools/clis/mixpanel.js
+++ b/tools/clis/mixpanel.js
@@ -5,9 +5,16 @@ const TOKEN = process.env.MIXPANEL_TOKEN
const API_KEY = process.env.MIXPANEL_API_KEY
const SECRET = process.env.MIXPANEL_SECRET
const PROJECT_SECRET = process.env.MIXPANEL_PROJECT_SECRET
-const INGESTION_URL = 'https://api.mixpanel.com'
-const QUERY_URL = 'https://mixpanel.com/api/2.0'
-const EXPORT_URL = 'https://data.mixpanel.com/api/2.0'
+const REGION = process.env.MIXPANEL_REGION ?? 'us'
+const REGION_HOSTS = {
+ us: ['api.mixpanel.com', 'mixpanel.com', 'data.mixpanel.com'],
+ eu: ['api-eu.mixpanel.com', 'eu.mixpanel.com', 'data-eu.mixpanel.com'],
+ in: ['api-in.mixpanel.com', 'in.mixpanel.com', 'data-in.mixpanel.com'],
+}
+const hosts = Object.hasOwn(REGION_HOSTS, REGION) ? REGION_HOSTS[REGION] : REGION_HOSTS.us
+const INGESTION_URL = `https://${hosts[0]}`
+const QUERY_URL = `https://${hosts[1]}/api/2.0`
+const EXPORT_URL = `https://${hosts[2]}/api/2.0`
if ((!TOKEN && !API_KEY && !SECRET && !PROJECT_SECRET) && rawArgs.length > 0) {
console.error(JSON.stringify({ error: 'MIXPANEL_TOKEN (ingestion), MIXPANEL_API_KEY + MIXPANEL_SECRET (service account), or MIXPANEL_PROJECT_SECRET (legacy project auth) required' }))
@@ -132,6 +139,7 @@ const args = parseArgs(rawArgs)
const [cmd, sub, ...rest] = args._
async function main() {
+ if (rawArgs.length > 0 && !Object.hasOwn(REGION_HOSTS, REGION)) throw new Error('MIXPANEL_REGION must be us, eu, or in')
let result
switch (cmd) {
diff --git a/tools/integrations/mixpanel.md b/tools/integrations/mixpanel.md
index 15d1654..b551a77 100644
--- a/tools/integrations/mixpanel.md
+++ b/tools/integrations/mixpanel.md
@@ -149,3 +149,26 @@ mixpanel.track('Feature Used', {
- analytics
- ab-testing
- onboarding
+
+### Select the project's API region
+
+Set `MIXPANEL_REGION` to your existing project's region: `us` (default), `eu`,
+or `in`. One setting selects the documented ingestion, query, and raw-export
+API families consistently:
+
+| Region | Ingestion | Query | Export |
+|--------|-----------|-------|--------|
+| `us` | `api.mixpanel.com` | `mixpanel.com` | `data.mixpanel.com` |
+| `eu` | `api-eu.mixpanel.com` | `eu.mixpanel.com` | `data-eu.mixpanel.com` |
+| `in` | `api-in.mixpanel.com` | `in.mixpanel.com` | `data-in.mixpanel.com` |
+
+```bash
+MIXPANEL_REGION=eu node tools/clis/mixpanel.js track event \
+ --distinct-id owned-test-user --event 'Owned test event' --dry-run
+```
+
+Unknown regions fail before any request. Dry runs use the selected hosts and
+mask credentials as before. Authentication, payloads, and response parsing are
+unchanged. Match the region in your Mixpanel project settings; selecting a host
+does not move a project or establish a data-residency guarantee. See the
+[official API overview](https://docs.mixpanel.com/reference/overview).