> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dodopayments.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Segment

> Gửi các sự kiện Dodo Payments đến Segment để phân tích và tích hợp nền tảng dữ liệu khách hàng.

## Giới thiệu

Theo dõi các sự kiện thanh toán trong Segment để hỗ trợ phân tích, tự động hóa tiếp thị và nền tảng dữ liệu khách hàng của bạn. Gửi các sự kiện thanh toán, đăng ký và vòng đời khách hàng đến hơn 300 công cụ hạ nguồn một cách tự động.

<Info>
  Tích hợp này yêu cầu một Segment Write Key từ workspace Segment của bạn.
</Info>

## Bắt đầu

<Steps>
  <Step title="Open the Webhook Section">
    Trong bảng điều khiển Dodo Payments của bạn, điều hướng đến <b>Webhooks → + Thêm Endpoint</b> và mở rộng danh sách tích hợp.

    <Frame>
      <img src="https://mintcdn.com/dodopayments/slbAEdrLLwKHfaRf/images/integrations/segment.png?fit=max&auto=format&n=slbAEdrLLwKHfaRf&q=85&s=024e80f2cafa4245742eac33f445ebe5" alt="Thêm Endpoint và danh sách tích hợp" style={{ maxHeight: '500px', width: 'auto' }} width="1644" height="948" data-path="images/integrations/segment.png" />
    </Frame>
  </Step>

  <Step title="Select Segment">
    Chọn thẻ tích hợp <b>Segment</b>.
  </Step>

  <Step title="Enter Write Key">
    Cung cấp Segment Write Key của bạn trong phần cấu hình.
  </Step>

  <Step title="Configure Transformation">
    Chỉnh sửa mã biến đổi để định dạng sự kiện cho API Track của Segment.
  </Step>

  <Step title="Test & Create">
    Kiểm tra bằng các payload mẫu và nhấp <b>Create</b> để kích hoạt đồng bộ.
  </Step>

  <Step title="Done!">
    🎉 Các sự kiện thanh toán giờ sẽ được theo dõi trong Segment và gửi tới các công cụ kết nối của bạn.
  </Step>
</Steps>

## Ví dụ Mã Biến đổi

### Theo dõi Sự kiện Thanh toán

```javascript track_payments.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.succeeded") {
    const p = webhook.payload.data;
    webhook.url = "https://api.segment.io/v1/track";
    webhook.payload = {
      userId: p.customer.customer_id,
      event: "Payment Completed",
      properties: {
        amount: (p.total_amount / 100).toFixed(2),
        currency: p.currency || "USD",
        payment_method: p.payment_method || "unknown",
        payment_id: p.payment_id,
        customer_email: p.customer.email,
        customer_name: p.customer.name
      },
      timestamp: webhook.payload.timestamp
    };
  }
  return webhook;
}
```

### Theo dõi Vòng đời Đăng ký

```javascript track_subscriptions.js icon="js" expandable theme={null}
function handler(webhook) {
  const s = webhook.payload.data;
  switch (webhook.eventType) {
    case "subscription.active":
      webhook.url = "https://api.segment.io/v1/track";
      webhook.payload = {
        userId: s.customer.customer_id,
        event: "Subscription Started",
        properties: {
          subscription_id: s.subscription_id,
          product_id: s.product_id,
          amount: (s.recurring_pre_tax_amount / 100).toFixed(2),
          frequency: s.payment_frequency_interval,
          next_billing: s.next_billing_date,
          customer_email: s.customer.email
        },
        timestamp: webhook.payload.timestamp
      };
      break;
    case "subscription.cancelled":
      webhook.url = "https://api.segment.io/v1/track";
      webhook.payload = {
        userId: s.customer.customer_id,
        event: "Subscription Cancelled",
        properties: {
          subscription_id: s.subscription_id,
          product_id: s.product_id,
          cancelled_at: s.cancelled_at,
          cancel_at_next_billing: s.cancel_at_next_billing_date,
          customer_email: s.customer.email
        },
        timestamp: webhook.payload.timestamp
      };
      break;
  }
  return webhook;
}
```

### Xác định Thuộc tính Khách hàng

```javascript identify_customer.js icon="js" expandable theme={null}
function handler(webhook) {
  if (webhook.eventType === "payment.succeeded") {
    const p = webhook.payload.data;
    webhook.url = "https://api.segment.io/v1/identify";
    webhook.payload = {
      userId: p.customer.customer_id,
      traits: {
        email: p.customer.email,
        name: p.customer.name,
        total_spent: (p.total_amount / 100).toFixed(2),
        payment_method: p.payment_method || "unknown",
        last_payment_date: webhook.payload.timestamp,
        customer_since: webhook.payload.timestamp
      }
    };
  }
  return webhook;
}
```

## Mẹo

* Sử dụng tên sự kiện nhất quán trong toàn bộ tích hợp của bạn
* Bao gồm các thuộc tính liên quan cho phân tích và phân khúc
* Đặt dấu thời gian chính xác để theo dõi sự kiện chính xác
* Sử dụng ID khách hàng làm userId để xác định người dùng chính xác

## Khắc phục sự cố

<AccordionGroup>
  <Accordion title="Events not appearing in Segment">
    * Xác minh Write Key là đúng và đang hoạt động
    * Kiểm tra tên sự kiện có tuân theo quy ước đặt tên của Segment không
    * Đảm bảo userId được thiết lập đúng để xác định người dùng
    * Xem lại giới hạn tỷ lệ API của Segment
  </Accordion>

  <Accordion title="Transformation errors">
    * Xác nhận cấu trúc JSON khớp định dạng API của Segment
    * Kiểm tra tất cả các trường bắt buộc đã có mặt
    * Đảm bảo tên sự kiện là chuỗi, không phải đối tượng
  </Accordion>
</AccordionGroup>
