Skip to content

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

feat(klaviyo): preserve structured event properties

Rudy Celekli83b3890e95ea
diff --git a/tests/clis/klaviyo-properties-json.test.cjs b/tests/clis/klaviyo-properties-json.test.cjs
new file mode 100644
index 0000000..35781ab
--- /dev/null
+++ b/tests/clis/klaviyo-properties-json.test.cjs
@@ -0,0 +1,37 @@
+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/klaviyo.js')
+function run(extra, allowFetch = true) {
+ const args = ['events', 'create', '--metric', 'Placed Order', '--email', '[email protected]', ...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, KLAVIYO_API_KEY: 'fixture-only' } })
+}
+const properties = { items: [{ SKU: 'sku:one,two', quantity: 2 }], VIP: true, customer: { tier: 'Gold' }, orderTotal: 42.5 }
+test('event properties preserve structured product arrays and primitive types', () => {
+ const r = run(['--properties', JSON.stringify(properties), '--value', '42.5'])
+ assert.equal(r.status, 0, r.stderr)
+ const out = JSON.parse(r.stdout)
+ assert.equal(out.url, 'https://a.klaviyo.com/api/events/')
+ assert.deepEqual(out.body.data.attributes.properties, properties)
+ assert.equal(out.body.data.attributes.value, 42.5)
+ assert.equal(out.body.data.attributes.metric.data.attributes.name, 'Placed Order')
+})
+test('legacy property shorthand and JSON previews remain compatible', () => {
+ const r = run(['--property', 'SKU:sku:one,OrderId:123'])
+ assert.equal(r.status, 0, r.stderr)
+ assert.deepEqual(JSON.parse(r.stdout).body.data.attributes.properties, { SKU: 'sku:one', OrderId: '123' })
+ const preview = run(['--properties', JSON.stringify(properties), '--dry-run'], false)
+ assert.equal(preview.status, 0, preview.stderr)
+ const out = JSON.parse(preview.stdout)
+ assert.deepEqual(out.body.data.attributes.properties, properties)
+ assert.equal(out.headers.Authorization, '***')
+})
+for (const extra of [['--properties', 'null'], ['--properties', '[]'], ['--properties', 'true'], ['--properties', '{bad'], ['--properties'], ['--property', 'a:b', '--properties', '{}']]) {
+ test(`invalid or ambiguous properties ${extra.join(' ')} stop before fetch`, () => {
+ const r = run(extra, false)
+ assert.equal(r.status, 1)
+ assert.match(JSON.parse(r.stderr).error, /properties|not both/)
+ })
+}
diff --git a/tools/clis/klaviyo.js b/tools/clis/klaviyo.js
index 79dbd32..05a3d49 100755
--- a/tools/clis/klaviyo.js
+++ b/tools/clis/klaviyo.js
@@ -183,7 +183,13 @@ async function main() {
const email = args.email
if (!metric) { result = { error: '--metric required (metric name)' }; break }
if (!email) { result = { error: '--email required' }; break }
- const properties = {}
+ let properties = {}
+ if (args.properties !== undefined) {
+ if (args.property !== undefined) throw new Error('Use --property or --properties, not both')
+ if (typeof args.properties !== 'string') throw new Error('--properties must be a JSON object')
+ try { properties = JSON.parse(args.properties) } catch { throw new Error('--properties must be valid JSON object') }
+ if (properties === null || typeof properties !== 'object' || Array.isArray(properties)) throw new Error('--properties must be a JSON object')
+ }
let value
if (args.value !== undefined) {
if (typeof args.value !== 'string' || !args.value.trim() || !Number.isFinite(Number(args.value))) {
@@ -337,7 +343,7 @@ async function main() {
usage: {
profiles: 'profiles [list | get --id <id> | create --email <email> | update --id <id>]',
lists: 'lists [list | get --id <id> | create --name <name> | delete --id <id> | add-profiles --id <list-id> --profiles <id1,id2> | remove-profiles --id <list-id> --profiles <id1,id2>]',
- events: 'events [list | get --id <id> | create --metric <name> --email <email>]',
+ events: 'events [list | get --id <id> | create --metric <name> --email <email> [--property <pairs> | --properties <json_object>]]',
campaigns: 'campaigns [list | get --id <id>]',
flows: 'flows [list | get --id <id> | update --id <id> --status <status>]',
metrics: 'metrics [list | get --id <id>]',
diff --git a/tools/integrations/klaviyo.md b/tools/integrations/klaviyo.md
index a9e8609..8b684cd 100644
--- a/tools/integrations/klaviyo.md
+++ b/tools/integrations/klaviyo.md
@@ -228,3 +228,19 @@ Klaviyo uses the JSON:API specification. All request/response bodies use `{ "dat
- ecommerce-email
- lifecycle-marketing
- customer-segmentation
+
+## Structured event properties
+
+For product arrays and typed event fields, use a JSON object instead of the
+flat `--property key:value,key:value` shorthand:
+
+```bash
+node tools/clis/klaviyo.js events create --metric "Placed Order" --email [email protected] \
+ --properties '{"items":[{"SKU":"sku-1","quantity":2}],"VIP":true}' --value 42.50 --dry-run
+```
+
+`--properties` preserves JSON types and nested values; it cannot be combined
+with `--property`. The CLI retains the existing API revision. API acceptance
+and eligibility for segmentation are separate: Klaviyo excludes some values
+(such as zero, null and empty strings) from segmentation. See the official
+[Create Event API](https://developers.klaviyo.com/en/reference/create_event).