Skip to content

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

fix(klaviyo): send monetary event value in its API attribute

Rudy Celeklifc236ea229f4
diff --git a/tests/klaviyo-event-value.test.cjs b/tests/klaviyo-event-value.test.cjs
new file mode 100644
index 0000000..26a8342
--- /dev/null
+++ b/tests/klaviyo-event-value.test.cjs
@@ -0,0 +1,46 @@
+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(args, fetch = false) {
+ const source = `global.fetch = async (url, options) => {
+ if (!${fetch}) throw new Error('Unexpected network request');
+ return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ const result = spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', env: { ...process.env, KLAVIYO_API_KEY: 'fake-key' }, timeout: 5000 })
+ assert.equal(result.status, 0, result.stderr)
+ return JSON.parse(result.stdout)
+}
+
+function eventArgs(value) { return ['events', 'create', '--metric', 'Placed Order', '--email', '[email protected]', '--value', value, '--property', 'OrderId:123'] }
+test('event value uses the numeric revenue attribute rather than a custom property', () => {
+ const result = run(eventArgs('99.95'), true)
+ assert.equal(result.method, 'POST')
+ const attributes = result.body.data.attributes
+ assert.equal(attributes.value, 99.95)
+ assert.deepEqual(attributes.properties, { OrderId: '123' })
+})
+test('explicit zero event value is retained', () => {
+ const result = run(eventArgs('0'), true)
+ assert.equal(result.body.data.attributes.value, 0)
+})
+test('invalid event values stop before a request can serialize NaN to null', () => {
+ for (const value of ['not-a-number', 'Infinity', '1e999', '']) {
+ assert.match(run(eventArgs(value)).error, /finite number/)
+ }
+})
+test('events without value retain metric, profile and custom properties', () => {
+ const result = run(['events', 'create', '--metric', 'Viewed Product', '--email', '[email protected]', '--property', 'SKU:123'], true)
+ const attributes = result.body.data.attributes
+ assert.equal(Object.hasOwn(attributes, 'value'), false)
+ assert.equal(attributes.metric.data.attributes.name, 'Viewed Product')
+ assert.equal(attributes.profile.data.attributes.email, '[email protected]')
+ assert.deepEqual(attributes.properties, { SKU: '123' })
+})
+test('dry run exposes exact revenue payload with masked authorization', () => {
+ const result = run([...eventArgs('12.50'), '--dry-run'])
+ assert.equal(result.body.data.attributes.value, 12.5)
+ assert.equal(result.headers.Authorization, '***')
+ assert.equal(JSON.stringify(result).includes('fake-key'), false)
+})
diff --git a/tools/clis/klaviyo.js b/tools/clis/klaviyo.js
index d74966f..52f5d10 100755
--- a/tools/clis/klaviyo.js
+++ b/tools/clis/klaviyo.js
@@ -183,7 +183,13 @@ async function main() {
if (!metric) { result = { error: '--metric required (metric name)' }; break }
if (!email) { result = { error: '--email required' }; break }
const properties = {}
- if (args.value) properties.value = Number(args.value)
+ let value
+ if (args.value !== undefined) {
+ if (typeof args.value !== 'string' || !args.value.trim() || !Number.isFinite(Number(args.value))) {
+ result = { error: '--value must be a finite number' }; break
+ }
+ value = Number(args.value)
+ }
if (args.property) {
const pairs = args.property.split(',')
for (const pair of pairs) {
@@ -198,6 +204,7 @@ async function main() {
metric: { data: { type: 'metric', attributes: { name: metric } } },
profile: { data: { type: 'profile', attributes: { email } } },
properties,
+ ...(value !== undefined ? { value } : {}),
time: new Date().toISOString(),
}
}
diff --git a/tools/integrations/klaviyo.md b/tools/integrations/klaviyo.md
index 3e4664d..a9e8609 100644
--- a/tools/integrations/klaviyo.md
+++ b/tools/integrations/klaviyo.md
@@ -100,6 +100,8 @@ POST https://a.klaviyo.com/api/lists/{listId}/relationships/profiles/
### Track event
+The monetary event `value` belongs in `data.attributes`, alongside `properties`. Custom event properties do not set the metric's monetary value. See the [Create Event API](https://developers.klaviyo.com/en/v2024-10-15/reference/create_event).
+
```bash
POST https://a.klaviyo.com/api/events/
@@ -119,8 +121,8 @@ POST https://a.klaviyo.com/api/events/
"attributes": { "email": "[email protected]" }
}
},
+ "value": 99.99,
"properties": {
- "value": 99.99,
"items": ["Product A"]
},
"time": "2025-01-15T10:00:00Z"