Klaviyo Core Workflow B -- Events, Segments & Campaigns
Overview
Secondary workflow: track customer events, query segments, create/send campaigns, and
trigger metric-based flows via the klaviyo-api SDK. This page summarizes the five steps
and their skeletons; the full copy-ready code lives in
references/implementation.md and worked scenarios in
references/examples.md.
Prerequisites
- Completed
klaviyo-core-workflow-a (profiles/lists set up)
- API key scopes:
events:write, segments:read, campaigns:read, campaigns:write, flows:read
klaviyo-api installed and KLAVIYO_PRIVATE_KEY set in the environment
Instructions
Open one session, then use the API class each step needs. Full parameter shapes for every
step are in references/implementation.md.
import { ApiKeySession, EventsApi } from 'klaviyo-api';
const session = new ApiKeySession(process.env.KLAVIYO_PRIVATE_KEY!);
- Step 1 — Track server-side events.
new EventsApi(session).createEvent(...). Include
metric.data.attributes.name (auto-creates the metric), a profile, a value for revenue
attribution, and a uniqueId for deduplication. Custom metrics trigger listening flows.
- Step 2 — Query events and metrics.
MetricsApi.getMetrics() lists event types;
EventsApi.getEvents({ sort: '-datetime', filter: 'equals(metric_id,"...")' }) reads recent events.
- Step 3 — Work with segments.
SegmentsApi.getSegments() lists them,
getSegmentProfiles() reads members, and getSegment({ additionalFieldsSegment: ['profile_count'] })
returns the size — check it before a send.
- Step 4 — Create an email campaign. Four ordered calls: create a template, create the
campaign (with
audiences.included/excluded), assign the template to the campaign message,
then create the campaign-send-job. Sending before the template is assigned returns a 400.
- Step 5 — Query flows (read-only).
FlowsApi.getFlows() lists flows;
getFlowFlowActions({ id }) returns each flow's steps and their status.
Output
- Event tracking returns an HTTP 202 Accepted acknowledgement (Klaviyo queues events
asynchronously); the event appears in the profile's
activity feed and fires any flow listening on that metric.
- Metric/segment/flow queries return a
body.data array of records with id and
attributes (name, status, profileCount, etc.).
- Campaign creation yields a campaign
id; the send job queues the campaign, and Klaviyo
reports delivery back in the app's campaign analytics.
Common Event Names for Flow Triggers
| Event name |
Typical trigger |
Flow type |
Placed Order |
Purchase completed |
Post-purchase / cross-sell |
Started Checkout |
Cart created |
Abandoned cart |
Viewed Product |
Product page visit |
Browse abandonment |
Ordered Product |
Per-item tracking |
Product review request |
Fulfilled Order |
Shipment sent |
Shipping confirmation |
Cancelled Order |
Order cancelled |
Win-back |
Subscribed to List |
Email/SMS signup |
Welcome series |
Custom Event |
Any API event |
Custom automation |
Error Handling
| Error |
Status |
Cause |
Solution |
| Invalid metric name |
400 |
Empty or null metric |
Always include metric.data.attributes.name |
| Segment not found |
404 |
Wrong segment ID |
List segments with getSegments() |
| Campaign send failed |
400 |
Missing template/audience |
Assign template and set audience first |
| Duplicate event |
N/A |
Same uniqueId |
Deduplication built-in; safe to retry |
Examples
Concrete, runnable scenarios are collected in
references/examples.md:
- Fire an abandoned-cart signal — track a
Started Checkout event for a flow to pick up.
- Size a segment before sending — read
profileCount and refuse to send to an empty audience.
- Create and send a campaign to a segment — the ordered template → campaign → assign → send-job chain.
A minimal event track:
import { EventsApi, EventEnum, ProfileEnum } from 'klaviyo-api';
const eventsApi = new EventsApi(session);
await eventsApi.createEvent({
data: {
type: EventEnum.Event,
attributes: {
metric: { data: { type: 'metric', attributes: { name: 'Placed Order' } } },
profile: { data: { type: ProfileEnum.Profile, attributes: { email: 'customer@example.com' } } },
value: 99.97,
time: new Date().toISOString(),
uniqueId: 'ORD-12345',
},
},
});
Resources
Source: jeremylongshore/claude-code-plugins-plus-skills → plugins/saas-packs/klaviyo-pack/skills/klaviyo-core-workflow-b/SKILL.md
1---2name: klaviyo-core-workflow-b3description: | Execute the Klaviyo secondary workflow: event tracking, segments, and campaigns. Use when you need to track customer events, query or size segments, build and send email campaigns, or inspect metric-triggered flows through the Klaviyo API. Trigger with phrases like "klaviyo events", "klaviyo segments", "klaviyo campaigns", "track klaviyo event", "klaviyo flow trigger".4---56# Klaviyo Core Workflow B -- Events, Segments & Campaigns78## Overview910Secondary workflow: track customer events, query segments, create/send campaigns, and11trigger metric-based flows via the `klaviyo-api` SDK. This page summarizes the five steps12and their skeletons; the full copy-ready code lives in13[references/implementation.md](references/implementation.md) and worked scenarios in14[references/examples.md](references/examples.md).1516## Prerequisites1718- Completed `klaviyo-core-workflow-a` (profiles/lists set up)19- API key scopes: `events:write`, `segments:read`, `campaigns:read`, `campaigns:write`, `flows:read`20- `klaviyo-api` installed and `KLAVIYO_PRIVATE_KEY` set in the environment2122## Instructions2324Open one session, then use the API class each step needs. Full parameter shapes for every25step are in [references/implementation.md](references/implementation.md).2627```typescript28import { ApiKeySession, EventsApi } from 'klaviyo-api';29const session = new ApiKeySession(process.env.KLAVIYO_PRIVATE_KEY!);30```31321. **Step 1 — Track server-side events.** `new EventsApi(session).createEvent(...)`. Include33 `metric.data.attributes.name` (auto-creates the metric), a `profile`, a `value` for revenue34 attribution, and a `uniqueId` for deduplication. Custom metrics trigger listening flows.352. **Step 2 — Query events and metrics.** `MetricsApi.getMetrics()` lists event types;36 `EventsApi.getEvents({ sort: '-datetime', filter: 'equals(metric_id,"...")' })` reads recent events.373. **Step 3 — Work with segments.** `SegmentsApi.getSegments()` lists them,38 `getSegmentProfiles()` reads members, and `getSegment({ additionalFieldsSegment: ['profile_count'] })`39 returns the size — check it before a send.404. **Step 4 — Create an email campaign.** Four ordered calls: create a template, create the41 campaign (with `audiences.included`/`excluded`), assign the template to the campaign message,42 then create the `campaign-send-job`. Sending before the template is assigned returns a 400.435. **Step 5 — Query flows (read-only).** `FlowsApi.getFlows()` lists flows;44 `getFlowFlowActions({ id })` returns each flow's steps and their status.4546## Output4748- **Event tracking** returns an HTTP 202 Accepted acknowledgement (Klaviyo queues events49 asynchronously); the event appears in the profile's50 activity feed and fires any flow listening on that metric.51- **Metric/segment/flow queries** return a `body.data` array of records with `id` and52 `attributes` (name, status, `profileCount`, etc.).53- **Campaign creation** yields a campaign `id`; the send job queues the campaign, and Klaviyo54 reports delivery back in the app's campaign analytics.5556## Common Event Names for Flow Triggers5758| Event name | Typical trigger | Flow type |59|-----------|----------------|-----------|60| `Placed Order` | Purchase completed | Post-purchase / cross-sell |61| `Started Checkout` | Cart created | Abandoned cart |62| `Viewed Product` | Product page visit | Browse abandonment |63| `Ordered Product` | Per-item tracking | Product review request |64| `Fulfilled Order` | Shipment sent | Shipping confirmation |65| `Cancelled Order` | Order cancelled | Win-back |66| `Subscribed to List` | Email/SMS signup | Welcome series |67| `Custom Event` | Any API event | Custom automation |6869## Error Handling7071| Error | Status | Cause | Solution |72|-------|--------|-------|----------|73| Invalid metric name | 400 | Empty or null metric | Always include `metric.data.attributes.name` |74| Segment not found | 404 | Wrong segment ID | List segments with `getSegments()` |75| Campaign send failed | 400 | Missing template/audience | Assign template and set audience first |76| Duplicate event | N/A | Same `uniqueId` | Deduplication built-in; safe to retry |7778## Examples7980Concrete, runnable scenarios are collected in81[references/examples.md](references/examples.md):8283- **Fire an abandoned-cart signal** — track a `Started Checkout` event for a flow to pick up.84- **Size a segment before sending** — read `profileCount` and refuse to send to an empty audience.85- **Create and send a campaign to a segment** — the ordered template → campaign → assign → send-job chain.8687A minimal event track:8889```typescript90import { EventsApi, EventEnum, ProfileEnum } from 'klaviyo-api';91const eventsApi = new EventsApi(session);9293await eventsApi.createEvent({94 data: {95 type: EventEnum.Event,96 attributes: {97 metric: { data: { type: 'metric', attributes: { name: 'Placed Order' } } },98 profile: { data: { type: ProfileEnum.Profile, attributes: { email: 'customer@example.com' } } },99 value: 99.97,100 time: new Date().toISOString(),101 uniqueId: 'ORD-12345',102 },103 },104});105```106107## Resources108109- [Full implementation walkthrough](references/implementation.md) — copy-ready code for all five steps110- [Worked examples](references/examples.md) — end-to-end scenarios111- [Events API](https://developers.klaviyo.com/en/reference/events_api_overview)112- [Segments API](https://developers.klaviyo.com/en/reference/segments_api_overview)113- [Campaigns API](https://developers.klaviyo.com/en/reference/campaigns_api_overview)114- [Flows API](https://developers.klaviyo.com/en/reference/flows_api_overview)115- [Metrics API](https://developers.klaviyo.com/en/reference/metrics_api_overview)116- For common errors, see the `klaviyo-common-errors` skill.117118---119120**Source:** [`jeremylongshore/claude-code-plugins-plus-skills`](https://github.com/jeremylongshore/claude-code-plugins-plus-skills) → `plugins/saas-packs/klaviyo-pack/skills/klaviyo-core-workflow-b/SKILL.md`