Skip to content

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

fix(mailchimp): send member tag updates to their supported endpoint

Rudy Celekli44a77ef1092d
diff --git a/tests/clis/mailchimp-update-tags.test.cjs b/tests/clis/mailchimp-update-tags.test.cjs
new file mode 100644
index 0000000..918a2e9
--- /dev/null
+++ b/tests/clis/mailchimp-update-tags.test.cjs
@@ -0,0 +1,70 @@
+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/mailchimp.js')
+function run(args) {
+ // A real local HTTP server enforces the published distinction between
+ // PATCH member fields and POST tag activation objects, without Mailchimp calls.
+ const code = `const http = require('node:http'); const realFetch = global.fetch;
+ const server = http.createServer(async (req, res) => {
+ let raw = ''; for await (const part of req) raw += part;
+ const body = raw ? JSON.parse(raw) : null;
+ process.stderr.write('fixture_request=' + JSON.stringify({method: req.method, path: req.url, body}) + String.fromCharCode(10));
+ if (req.method === 'POST' && req.url.endsWith('/tags') && body.tags.every(tag => tag.name && tag.status === 'active')) {
+ res.writeHead(204); res.end();
+ } else if (req.method === 'PATCH' && body.tags) {
+ res.writeHead(400, {'Content-Type': 'application/json'}); res.end(JSON.stringify({title: 'Unsupported member update field'}));
+ } else { res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify({id: 'member123'})); }
+ });
+ server.listen(0, '127.0.0.1', () => {
+ const write = process.stdout.write.bind(process.stdout);
+ process.stdout.write = (chunk, ...rest) => { const result = write(chunk, ...rest); server.close(); server.closeAllConnections(); return result; };
+ global.fetch = (url, options) => {
+ const local = new URL(url); local.protocol = 'http:'; local.hostname = '127.0.0.1'; local.port = server.address().port;
+ return realFetch(local, options);
+ };
+ process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});
+ });`
+ const r = spawnSync(process.execPath, ['-e', code], {encoding: 'utf8', env: {...process.env, MAILCHIMP_API_KEY: 'test-only-us7'}, timeout: 10000})
+ assert.equal(r.status, 0, r.stderr)
+ const requests = r.stderr.split('\n').filter(line => line.startsWith('fixture_request=')).map(line => JSON.parse(line.slice('fixture_request='.length)))
+ return {result: JSON.parse(r.stdout), requests}
+}
+const update = ['members', 'update', 'hash123', '--list-id', 'audience123']
+test('tag-only updates use the tag activation endpoint on actual HTTP', () => {
+ const r = run([...update, '--tags', 'newsletter, trial'])
+ assert.equal(r.result.status, 204)
+ assert.deepEqual(r.requests, [{method: 'POST', path: '/3.0/lists/audience123/members/hash123/tags', body: {tags: [{name: 'newsletter', status: 'active'}, {name: 'trial', status: 'active'}]}}])
+})
+for (const extra of [['--status', 'unsubscribed'], ['--first-name', 'Zoë']]) {
+ test(`mixed tag and member-field update ${extra[0]} fails before either mutation`, () => {
+ const r = run([...update, '--tags', 'newsletter', ...extra])
+ assert.match(r.result.error || '', /separately/)
+ assert.equal(r.requests.length, 0)
+ })
+}
+for (const tags of ['newsletter,,trial', ' ', null]) {
+ test(`invalid tag list ${JSON.stringify(tags)} is rejected before HTTP`, () => {
+ const r = run([...update, '--tags', ...(tags === null ? [] : [tags])])
+ assert.match(r.result.error || '', /non-empty tag/)
+ assert.equal(r.requests.length, 0)
+ })
+}
+test('tag previews redact credentials and send no HTTP request', () => {
+ const r = run([...update, '--tags', 'newsletter', '--dry-run'])
+ assert.equal(r.result.method, 'POST')
+ assert.equal(r.result.url.endsWith('/tags'), true)
+ assert.equal(r.result.headers.Authorization, '***')
+ assert.deepEqual(r.result.body, {tags: [{name: 'newsletter', status: 'active'}]})
+ assert.equal(r.requests.length, 0)
+})
+test('profile updates remain a single PATCH without tags', () => {
+ const r = run([...update, '--first-name', 'Zoë', '--status', 'pending'])
+ assert.deepEqual(r.requests, [{method: 'PATCH', path: '/3.0/lists/audience123/members/hash123', body: {status: 'pending', merge_fields: {FNAME: 'Zoë'}}}])
+})
+test('creation keeps its supported string-array tags contract', () => {
+ const r = run(['members', 'add', '--list-id', 'audience123', '--email', '[email protected]', '--tags', 'newsletter,trial'])
+ assert.equal(r.requests[0].method, 'POST')
+ assert.deepEqual(r.requests[0].body.tags, ['newsletter', 'trial'])
+})
diff --git a/tools/clis/mailchimp.js b/tools/clis/mailchimp.js
index bc21b35..e303eb8 100755
--- a/tools/clis/mailchimp.js
+++ b/tools/clis/mailchimp.js
@@ -125,7 +125,17 @@ async function main() {
if (args['first-name']) body.merge_fields.FNAME = args['first-name']
if (args['last-name']) body.merge_fields.LNAME = args['last-name']
}
- if (args.tags) body.tags = args.tags.split(',')
+ if (args.tags !== undefined) {
+ if (typeof args.tags !== 'string' || args.tags.split(',').some(tag => !tag.trim())) {
+ result = { error: '--tags must contain comma-separated non-empty tag names' }; break
+ }
+ if (Object.keys(body).length) {
+ result = { error: 'Update tags and member fields separately: Mailchimp uses separate endpoints' }; break
+ }
+ const tags = args.tags.split(',').map(name => ({ name: name.trim(), status: 'active' }))
+ result = await api('POST', `/lists/${args['list-id']}/members/${subscriberHash}/tags`, { tags })
+ break
+ }
result = await api('PATCH', `/lists/${args['list-id']}/members/${subscriberHash}`, body)
break
}
diff --git a/tools/integrations/mailchimp.md b/tools/integrations/mailchimp.md
index 050676f..f2cb431 100644
--- a/tools/integrations/mailchimp.md
+++ b/tools/integrations/mailchimp.md
@@ -17,6 +17,12 @@ Email marketing platform for campaigns, automation, and audience management.
- **Header**: `Authorization: Bearer {api_key}` or `Authorization: apikey {api_key}`
- **Base URL**: `https://{dc}.api.mailchimp.com/3.0/` (dc = datacenter from API key)
+## Updating member tags
+
+`members update <subscriber_hash> --list-id <list_id> --tags newsletter,trial` activates named tags through Mailchimp's member-tags endpoint. This adds the requested tags and does not replace or remove existing tags. Member creation still accepts its supported string-array `--tags` field.
+
+Tag changes and member profile/status changes use separate API endpoints. Run them as separate commands; combining `--tags` with `--status`, `--first-name` or `--last-name` is rejected before either write. A tag-only preview shows the POST `/lists/{list_id}/members/{subscriber_hash}/tags` request with `{tags: [{name, status: "active"}]}`. Its successful response is HTTP 204 with no JSON body.
+
## Common Agent Operations
### List audiences (lists)