Lumail allows you to track purchase activity and revenue associated with your subscribers. This enables you to:

- Calculate subscriber lifetime value (LTV)
- Segment subscribers by purchase behavior
- Measure ROI of your email campaigns
- Create targeting based on purchase history
- Build automation workflows triggered by purchases

## How Revenue Tracking Works

Lumail tracks revenue by recording payment events against individual subscribers. Each payment event increases the subscriber's `revenue` counter, providing a running total of their lifetime value.

These payment events must be manually tracked through the [Events API](/docs/api-reference/api-events-post), allowing you to integrate with your existing payment system or e-commerce platform.

## Setting Up Revenue Tracking

To track revenue for your subscribers, you'll need to send payment events to Lumail when purchases occur. Here's how to set it up:

### 1. Implement Event Tracking in Your Payment System

When a customer makes a purchase, you'll need to:

1. Identify the subscriber by email address
2. Record the purchase amount and details
3. Send a `SUBSCRIBER_PAYMENT` event to Lumail

### 2. Send Payment Events to the API

Use the [Events API](/docs/api-reference/api-events-post) to send payment information:

```javascript
const response = await fetch("https://lumail.io/api/v1/events", {
  method: "POST",
  headers: {
    Authorization: "Bearer YOUR_API_TOKEN",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    eventType: "SUBSCRIBER_PAYMENT",
    subscriber: "customer@example.com",
    data: {
      amount: 49.99,
      product: "Premium Subscription",
    },
  }),
});
```

### Required Payment Data Fields

| Field     | Type   | Description                              |
| --------- | ------ | ---------------------------------------- |
| `amount`  | number | The payment amount (required)            |
| `product` | string | Name of the product purchased (required) |

### 3. Track Refunds (Optional)

If you process refunds, you should also track these using the `SUBSCRIBER_REFUND` event:

```javascript
const response = await fetch("https://lumail.io/api/v1/events", {
  method: "POST",
  headers: {
    Authorization: "Bearer YOUR_API_TOKEN",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    eventType: "SUBSCRIBER_REFUND",
    subscriber: "customer@example.com",
    data: {
      amount: 49.99,
      product: "Premium Subscription",
    },
  }),
});
```

### 4. View Revenue Data

Once you've implemented revenue tracking, you can:

- See payment events in the subscriber timeline
- View total revenue per subscriber in the subscribers list
- Use revenue data for segmentation
- Create reports based on revenue metrics

## Integration Examples

### E-commerce Integration

If you run an e-commerce store, you can integrate revenue tracking on your order confirmation page:

```javascript
// Example code to run after successful checkout
async function trackOrderInLumail(order) {
  await fetch("https://lumail.io/api/v1/events", {
    method: "POST",
    headers: {
      Authorization: "Bearer YOUR_API_TOKEN",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      eventType: "SUBSCRIBER_PAYMENT",
      subscriber: order.customerEmail,
      data: {
        amount: order.total,
        product: order.items.map((item) => item.name).join(", "),
      },
    }),
  });
}
```

### Subscription System Integration

For subscription businesses, track recurring payments:

```javascript
// Example webhook handler for subscription payment events
app.post("/webhook/payment-succeeded", async (req, res) => {
  const event = req.body;

  // Track the payment in Lumail
  try {
    await fetch("https://lumail.io/api/v1/events", {
      method: "POST",
      headers: {
        Authorization: "Bearer YOUR_API_TOKEN",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        eventType: "SUBSCRIBER_PAYMENT",
        subscriber: event.customer.email,
        data: {
          amount: event.amount / 100, // Convert from cents
          product: event.product.name,
        },
      }),
    });

    res.status(200).send("Event processed");
  } catch (error) {
    console.error("Error tracking payment:", error);
    res.status(500).send("Error processing event");
  }
});
```

## Best Practices

1. **Be Consistent**: Use the same format for all payment tracking
2. **Track Immediately**: Send payment events as soon as the transaction is confirmed
3. **Include Product Info**: Add descriptive product names to help with analysis
4. **Track Refunds**: Always record refunds to keep revenue data accurate
5. **Test First**: Verify your integration with test purchases before full deployment

## Related Documentation

- [Subscriber Events Reference](/docs/features/subscriber-events) - Complete guide to all event types
- [Track Events API](/docs/api-reference/api-events-post) - Technical details for sending events
- [Get Subscriber Events API](/docs/api-reference/api-subscriber-events-get) - Retrieve payment event history
- [Subscribers API](/docs/api-reference/api-subscribers-get) - View and manage subscriber data
- [ClickFunnels Integration](/docs/integrations/clickfunnel-integration) - Track order events automatically
