@eancarr/klaviyo-medusa

中文文档
0.2.5发布于 2025/11/01MITCommonJS近 30 天下载 2938

A Klaviyo plugin for Medusa.

原始使用文档
文档目录19 节
  1. Table of Contents
  2. Features
  3. Customer Management
  4. Order Tracking
  5. Return Tracking
  6. Behavioral Tracking (Server-Side)
  7. Product Catalog
  8. Prerequisites
  9. Installation
  10. Configuration
  11. Configuration Options
  12. Environment Variables
  13. Usage
  14. Automatic Event Tracking
  15. Client-Side Integration
  16. Product Feed
  17. Extending the Plugin
  18. Local Development
  19. License

Klaviyo Integration for Medusa

A Medusa plugin that integrates Klaviyo's email marketing and customer engagement platform with your Medusa store.

Note: This is a fork of @variablevic/klaviyo-medusa with feature enhancements including event tracking, return management, cart behavioral tracking, enhanced product feed with brand support, and bulk subscription management.

Table of Contents

Features

Customer Management

  • Automatically sync customers to Klaviyo when created or updated
  • Handle marketing consent for email/SMS subscriptions
  • Bulk subscription management based on customer consent metadata
  • Support for email and SMS marketing consent
  • Transactional SMS consent handling

Order Tracking

  • Order Placed - Track when orders are placed with complete order details
  • Order Shipped - Track fulfillment creation with tracking numbers and carrier info
  • Order Canceled - Track order cancellations with refund amounts
  • Order Delivered - Track when orders are completed/delivered

Return Tracking

  • Return Requested - Track return requests with item details and reasons
  • Return Received - Track when returns are received at warehouse

Behavioral Tracking (Server-Side)

  • Added to Cart - Track when items are added to cart (with product details)
    • Detects new items and quantity increases
    • Supports retrospective tracking when email is added to cart
    • Works for both logged-in users and guests
    • Note: Cart tracking uses in-memory state management. For production at scale, consider implementing Redis-based state management to persist cart state across server restarts.
  • Started Checkout - Track when customers begin checkout process (triggered when shipping address is added)

Product Catalog

  • Klaviyo-compatible product feed for catalog syncing
  • Multi-currency support
  • Complete product details including inventory, pricing, and brand information
  • Supports product variants with calculated prices

Prerequisites

  • Medusa server (v2.4.0 or higher)
  • Klaviyo account with API credentials

Installation

yarn add @eancarr/klaviyo-medusa

Or with npm:

npm install @eancarr/klaviyo-medusa

Then add the plugin to your medusa-config.js file:

const plugins = [
  // ...
  {
    resolve: "@eancarr/klaviyo-medusa",
    options: {
      apiKey: process.env.KLAVIYO_API_KEY,
    },
  },
];

Configuration

Configuration Options

OptionTypeDescriptionDefault
apiKeystringYour Klaviyo API keyundefined

Environment Variables

KLAVIYO_API_KEY=your_klaviyo_api_key
STOREFRONT_URL=https://your-storefront-url.com  # Optional: Your actual storefront URL (where customers view products). Used for product feed links. Defaults to http://localhost:8000

Usage

Once installed and configured, the plugin will automatically track the following events:

Automatic Event Tracking

The plugin uses Medusa event subscribers to listen for events and send them to Klaviyo:

Customer Events

  • customer.created - Syncs new customers to Klaviyo
  • customer.updated - Updates customer profiles in Klaviyo

Order Events

  • order.placed - Sends "Placed Order" event with order details
  • order.fulfillment_created - Sends "Order Shipped" event with tracking info
  • order.canceled - Sends "Order Canceled" event with refund details
  • order.completed - Sends "Order Delivered" event

Return Events

  • order.return_requested - Sends "Return Requested" event
  • order.return_received - Sends "Return Received" event

Cart Events

  • cart.updated - Tracks "Added to Cart" and "Started Checkout" events

All events are tracked server-side for reliability and include complete data from your Medusa database. These events can be used in Klaviyo to:

  • Build automated email flows (abandoned cart, shipping notifications, etc.)
  • Segment customers based on behavior
  • Track customer lifetime value
  • Create product recommendations

Client-Side Integration

To properly manage marketing consent for Klaviyo in your storefront, you should set consent settings in the customer's metadata. This ensures compliance with privacy regulations by only subscribing customers who have explicitly given consent.

When collecting customer information (during registration, newsletter signup, or checkout), update the customer metadata with Klaviyo consent flags:

// Example implementation in your storefront
const updateCustomerConsent = async (
  customerId: string,
  consentSettings: {
    email?: boolean;
    sms?: boolean;
    transactional_sms?: boolean;
    consented_at?: string; // ISO date string (optional)
  }
) => {
  // Call your store API endpoint that updates customer metadata
  await fetch("/store/customers/me", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    credentials: "include",
    body: JSON.stringify({
      metadata: {
        klaviyo: consentSettings, // Can be object or JSON string
      },
    }),
  });
};

// Usage example
updateCustomerConsent("cus_123", {
  email: true,
  sms: false,
  transactional_sms: true,
});

The plugin checks for these consent settings when syncing customer data to Klaviyo:

  • metadata.klaviyo.email: Set to true to opt the customer into email marketing
  • metadata.klaviyo.sms: Set to true to opt the customer into SMS marketing
  • metadata.klaviyo.transactional_sms: Set to true to opt the customer into transactional SMS
  • metadata.klaviyo.consented_at: Optional ISO date string for consent timestamp

The metadata.klaviyo value can be either:

  • A JSON object with the consent properties
  • A JSON string that will be parsed automatically

Product Feed

The plugin provides a Klaviyo-compatible product feed API that allows you to sync your entire product catalog with Klaviyo. This enables product recommendations, abandoned cart emails with product details, and more.

To use the product feed in Klaviyo:

  1. Access your product feed at: https://your-medusa-url.com/feeds/products/{currencyCode}

    • Replace {currencyCode} with your store's currency code (e.g., usd, eur)
  2. In your Klaviyo account:

    • Go to "Content" → "Products"
    • Click "Manage Custome Catalog Sources"
    • Click "Add new source"
    • Enter your product feed URL
    • Configure sync settings according to your needs

The product feed includes comprehensive product data:

  • Product ID
  • Title
  • Description
  • Handle/Slug
  • SKU
  • Thumbnail and Images
  • Brand information
  • Pricing information (min/max prices, compare-at-price)
  • Currency
  • Product URL
  • Categories
  • Inventory quantity and availability status
  • Product variants with calculated prices

Extending the Plugin

You can extend the plugin by:

  1. Creating custom workflows in your Medusa server that utilize the Klaviyo service
  2. Adding additional event subscribers to sync more data types
  3. Enhancing the data structure sent to Klaviyo

Example of using the Klaviyo service in your own code:

// Access the Klaviyo service
const klaviyoService = container.resolve("klaviyoService");

// Create an event
await klaviyoService.createEvent({
  metric: {
    name: "Custom Event",
  },
  profile: {
    email: "[email protected]",
  },
  properties: {
    // Your custom properties
  },
});

// Upsert a customer profile
await klaviyoService.upsertProfile({
  email: "[email protected]",
  phone_number: "+1234567890",
  // ... other profile attributes
});

// Bulk subscribe profiles
await klaviyoService.bulkSubscribeProfiles([
  {
    type: "profile",
    id: "profile-id",
    attributes: {
      email: "[email protected]",
      subscriptions: {
        email: {
          marketing: {
            consent: "SUBSCRIBED",
          },
        },
      },
    },
  },
]);

Local Development

# Clone the repository
git clone https://github.com/eancarr/klaviyo-medusa.git

# Install dependencies
cd klaviyo-medusa
yarn

# Start development server
yarn dev

License

MIT

版本与发布记录

@eancarr/klaviyo-medusa 共发布 15 个版本,首次发布于 2025/10/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.5latest2025/11/011 年前
0.2.42025/10/261 年前
0.2.32025/10/261 年前
0.2.22025/10/261 年前
0.2.12025/10/241.1 年前
0.2.02025/10/241.1 年前

关于 @eancarr/klaviyo-medusa 的常见问题

@eancarr/klaviyo-medusa 是什么?

A Klaviyo plugin for Medusa.

如何安装 @eancarr/klaviyo-medusa?

在项目目录运行 npm install @eancarr/klaviyo-medusa,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@medusajs/admin-sdk(2.10.3)、@medusajs/cli(2.10.3)、@medusajs/framework(2.10.3)、@medusajs/icons(2.10.3)、@medusajs/medusa(2.10.3)、@medusajs/test-utils(2.10.3)、@medusajs/ui(^4.0.3)、@mikro-orm/cli(6.4.3)、@mikro-orm/core(6.4.3)、@mikro-orm/knex(6.4.3)、@mikro-orm/migrations(6.4.3)、@mikro-orm/postgresql(6.4.3)、awilix(^8.0.1)、pg(^8.13.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@eancarr/klaviyo-medusa 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/eancarr__klaviyo-medusa 是否存在。

@eancarr/klaviyo-medusa 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@eancarr/klaviyo-medusa 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@eancarr/klaviyo-medusa 可以商用吗?

@eancarr/klaviyo-medusa 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@eancarr/klaviyo-medusa 的下载量有多少?

根据 npm 官方下载统计,@eancarr/klaviyo-medusa 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,938 次,日均约 98 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eancarr/klaviyo-medusa 还在维护吗?

最新版本 0.2.5 发布于 2025/11/01(1 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/10/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。