Sync customers, orders, and products directly into Klaviyo
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.
yarn add @eancarr/klaviyo-medusaOr with npm:
npm install @eancarr/klaviyo-medusaThen add the plugin to your file:
1const plugins = [2 // ...3 {4 resolve: "@eancarr/klaviyo-medusa",5 options: {6 apiKey: process.env.KLAVIYO_API_KEY,7 },8 },9];| Option | Type | Description | Default |
|---|---|---|---|
| Your Klaviyo API key |
1KLAVIYO_API_KEY=your_klaviyo_api_key2STOREFRONT_URL=https://your-storefront-url.com # Optional: Your actual storefront URL (where customers view products). Used for product feed links. Defaults to http://localhost:8000Once installed and configured, the plugin will automatically track the following events:
The plugin uses Medusa event subscribers to listen for events and send them to Klaviyo:
All events are tracked server-side for reliability and include complete data from your Medusa database. These events can be used in Klaviyo to:
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:
1// Example implementation in your storefront2const updateCustomerConsent = async (3 customerId: string,4 consentSettings: {5 email?: boolean;6 sms?: boolean;7 transactional_sms?: boolean;8 consented_at?: string; // ISO date string (optional)9 }10) => {11 // Call your store API endpoint that updates customer metadata12 await fetch("/store/customers/me", {13 method: "POST",14 headers: {15 "Content-Type": "application/json",16 },17 credentials: "include",18 body: JSON.stringify({19 metadata: {20 klaviyo: consentSettings, // Can be object or JSON string21 },22 }),23 });24};25
26// Usage example27updateCustomerConsent("cus_123", {28 email: true,29 sms: false,30 transactional_sms: true,31});The plugin checks for these consent settings when syncing customer data to Klaviyo:
The value can be either:
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:
Access your product feed at:
In your Klaviyo account:
The product feed includes comprehensive product data:
You can extend the plugin by:
Example of using the Klaviyo service in your own code:
1// Access the Klaviyo service2const klaviyoService = container.resolve("klaviyoService");3
4// Create an event5await klaviyoService.createEvent({6 metric: {7 name: "Custom Event",8 },9 profile: {10 email: "customer@example.com",11 },12 properties: {13 // Your custom properties14 },15});16
17// Upsert a customer profile18await klaviyoService.upsertProfile({19 email: "customer@example.com",20 phone_number: "+1234567890",21 // ... other profile attributes22});23
24// Bulk subscribe profiles25await klaviyoService.bulkSubscribeProfiles([26 {27 type: "profile",28 id: "profile-id",29 attributes: {30 email: "customer@example.com",31 subscriptions: {32 email: {33 marketing: {34 consent: "SUBSCRIBED",35 },36 },37 },38 },39 },40]);1# Clone the repository2git clone https://github.com/eancarr/klaviyo-medusa.git3
4# Install dependencies5cd klaviyo-medusa6yarn7
8# Start development server9yarn devMIT