---
title: "Mixpanel"
description: "Kết nối Mixpanel với Adapty để phân tích gói đăng ký mạnh mẽ hơn."
---

[Mixpanel](https://mixpanel.com/home/) là một dịch vụ phân tích sản phẩm mạnh mẽ. Giải pháp theo dõi dựa trên sự kiện của nó giúp các nhóm sản phẩm có được những hiểu biết sâu sắc về chiến lược thu hút người dùng, chuyển đổi và giữ chân người dùng tối ưu trên các nền tảng khác nhau.

Tích hợp này cho phép bạn đưa tất cả các sự kiện Adapty vào Mixpanel. Nhờ đó, bạn sẽ có cái nhìn toàn diện hơn về hoạt động kinh doanh gói đăng ký và hành động của khách hàng. Adapty cung cấp bộ dữ liệu đầy đủ giúp bạn theo dõi [các sự kiện gói đăng ký](events) từ các cửa hàng ở một nơi. Với Adapty, bạn có thể dễ dàng thấy người dùng của mình đang hành xử như thế nào, tìm hiểu những gì họ thích và sử dụng thông tin đó để giao tiếp với họ theo cách có mục tiêu và hiệu quả.

## Cách thiết lập tích hợp Mixpanel \{#how-to-set-up-mixpanel-integration\}

1. Mở trang [Integrations -> Mixpanel](https://app.adapty.io/integrations/mixpanel) trong Adapty Dashboard.
2. Bật toggle và nhập **Mixpanel Token** của bạn. Bạn có thể chỉ định một token cho tất cả các nền tảng hoặc giới hạn cho các nền tảng cụ thể nếu bạn chỉ muốn nhận dữ liệu từ một số nền tảng nhất định.
3. Đặt **Mixpanel Data Residency** để khớp với dự án Mixpanel của bạn. Trường này bắt buộc và mặc định là **US**. Chọn **US** cho endpoint `api.mixpanel.com` hoặc **Europe** cho `api-eu.mixpanel.com`.

:::warning
Nếu dự án Mixpanel của bạn sử dụng data residency tại EU, bạn phải đặt **Mixpanel Data Residency** thành **Europe**. Mixpanel sẽ từ chối các sự kiện được gửi đến endpoint US từ các dự án EU.
:::

  <img src="/assets/shared/img/mixpanel.webp"
  style={{
    border: '1px solid #727272', /* border width and color */
    width: '700px', /* image width */
    display: 'block', /* for alignment */
    margin: '0 auto' /* center alignment */
  }}
/>

### Tìm Mixpanel Token của bạn \{#finding-your-mixpanel-token\}

Để lấy **Mixpanel Token** của bạn:

1. Đăng nhập vào [Mixpanel Dashboard](https://mixpanel.com/settings/project/) của bạn.
2. Mở **Settings** và chọn **Organization Settings**.

  <img src="/assets/shared/img/mixpanel-settings.webp"
  style={{
    border: '1px solid #727272', /* border width and color */
    width: '700px', /* image width */
    display: 'block', /* for alignment */
    margin: '0 auto' /* center alignment */
  }}
/>

3. Từ thanh bên trái, đi đến **Projects** và chọn dự án của bạn.

  <img src="/assets/shared/img/mixpanel-project-id.webp"
  style={{
    border: '1px solid #727272', /* border width and color */
    width: '700px', /* image width */
    display: 'block', /* for alignment */
    margin: '0 auto' /* center alignment */
  }}
/>

## Cách tích hợp hoạt động \{#how-the-integration-works\}

Adapty tự động ánh xạ các thuộc tính sự kiện liên quan—như ID người dùng và doanh thu—sang [các thuộc tính gốc của Mixpanel](https://docs.mixpanel.com/docs/data-structure/user-profiles). Điều này đảm bảo việc theo dõi và báo cáo chính xác các sự kiện liên quan đến gói đăng ký.

Ngoài ra, Adapty tích lũy dữ liệu doanh thu theo từng người dùng và cập nhật [User Profile Properties](https://docs.mixpanel.com/docs/data-structure/user-profiles) của họ, bao gồm `subscription state` và `subscription product ID`. Sau khi nhận được một sự kiện, Mixpanel sẽ cập nhật các trường tương ứng theo thời gian thực.

## Sự kiện và thẻ \{#events-and-tags\}

Bên dưới phần thông tin xác thực, có ba nhóm sự kiện bạn có thể gửi đến Mixpanel từ Adapty. Chỉ cần bật những sự kiện bạn cần. Xem danh sách đầy đủ các sự kiện mà Adapty cung cấp [tại đây](events).

  <img src="/assets/shared/img/mixpanel-events.webp"
  style={{
    border: '1px solid #727272', /* border width and color */
    width: '700px', /* image width */
    display: 'block', /* for alignment */
    margin: '0 auto' /* center alignment */
  }}
/>

Chúng tôi khuyến nghị sử dụng tên sự kiện mặc định do Adapty cung cấp. Tuy nhiên, bạn có thể thay đổi tên sự kiện theo nhu cầu của mình.

## Cấu hình SDK \{#sdk-configuration\}

Sử dụng phương thức `.setIntegrationIdentifier()` để đặt `mixpanelUserId`. Nếu không đặt, Adapty sẽ sử dụng ID người dùng của bạn (`customerUserId`) hoặc nếu giá trị đó là null thì dùng Adapty ID. Hãy đảm bảo rằng ID người dùng bạn dùng để gửi dữ liệu đến Mixpanel từ ứng dụng của mình trùng với ID bạn gửi đến Adapty.

---
no_index: true
---

import Callout from '../../../components/Callout.astro';

<Callout type="note">
Các SDK bên thứ ba tạo user ID theo cách bất đồng bộ. ID có thể chưa sẵn sàng khi `Adapty.activate()` chạy. Nếu **Customer User ID** của bạn đến từ một trong các SDK này, hãy gọi `Adapty.activate()` mà không truyền ID đó. Khi ID đã có, gọi `setIntegrationIdentifier()`, sau đó gọi `identify()` với CUID.
</Callout>

<Tabs groupId="current-os" queryString>
<TabItem value="swift" label="iOS (Swift)" default>

```swift showLineNumbers

do {
    try await Adapty.setIntegrationIdentifier(
        key: "mixpanel_user_id",
        value: Mixpanel.mainInstance().distinctId
    )
} catch {
    // handle the error
}
```
</TabItem>
<TabItem value="swift-callback" label="iOS (Swift-Callback)" default>

```swift showLineNumbers

let builder = AdaptyProfileParameters.Builder()
            .with(mixpanelUserId: Mixpanel.mainInstance().distinctId)

Adapty.updateProfile(params: builder.build())
```
</TabItem>
<TabItem value="kotlin" label="Android (Kotlin)" default>

```kotlin showLineNumbers
Adapty.setIntegrationIdentifier("mixpanel_user_id", mixpanelAPI.distinctId) { error ->
    if (error != null) {
        // handle the error
    }
}
```
</TabItem>
<TabItem value="flutter" label="Flutter (Dart)" default>

```javascript showLineNumbers

final mixpanel = await Mixpanel.init("Your Token", trackAutomaticEvents: true);
final distinctId = await mixpanel.getDistinctId();

try {
    await Adapty().setIntegrationIdentifier(
        key: "mixpanel_user_id",
        value: distinctId,
    );
} on AdaptyError catch (adaptyError) {
    // handle the error
} catch (e) {
    // handle the error
}
```
</TabItem>
<TabItem value="unity" label="Unity (C#)" default>

```csharp showLineNumbers
using AdaptySDK;

var distinctId = Mixpanel.DistinctId;

if (distinctId != null) {
  Adapty.SetIntegrationIdentifier(
    "mixpanel_user_id",
    distinctId,
    (error) => {
    // handle the error
  });
}
```
</TabItem>
<TabItem value="rn" label="React Native (TS)" default>

```typescript showLineNumbers

// If you already have a shared Mixpanel instance in your app, use that instance instead.
const trackAutomaticEvents = true;
const mixpanel = new Mixpanel('YOUR_PROJECT_TOKEN', trackAutomaticEvents);
await mixpanel.init();

// This is Mixpanel's current distinct_id (auto-generated, or set via mixpanel.identify(...))
const mixpanelUserId = await mixpanel.getDistinctId();

try {
    await adapty.setIntegrationIdentifier("mixpanel_user_id", mixpanelUserId);
} catch (error) {
    // handle `AdaptyError`
}
```
</TabItem>
</Tabs>

## Cấu trúc sự kiện Mixpanel \{#mixpanel-event-structure\}

Adapty gửi sự kiện đến Mixpanel bằng phương thức `track`. Các thuộc tính sự kiện được cấu trúc như sau:

```json
{
  "event": "subscription_renewed",
  "properties": {
    "ip": 0,
    "time": 1709294400,
    "$insert_id": "123e4567-e89b-12d3-a456-426614174000",
    "vendor_product_id": "yearly.premium.6999",
    "original_transaction_id": "GPA.3383...",
    "currency": "USD",
    "environment": "Production",
    "store": "app_store",
    "purchase_date": "2024-03-01T12:00:00.000000+0000"
  }
}
```

Trong đó:

| Tham số                              | Kiểu    | Mô tả                                                          |
|:-------------------------------------|:--------|:---------------------------------------------------------------|
| `event`                              | String  | Tên sự kiện (được ánh xạ từ sự kiện Adapty).                   |
| `properties`                         | Object  | Thuộc tính sự kiện.                                            |
| `properties.ip`                      | Integer | Địa chỉ IP (gửi là 0 khi dùng server-to-server).              |
| `properties.time`                    | Long    | Timestamp UNIX của sự kiện tính bằng giây.                     |
| `properties.$insert_id`              | String  | ID sự kiện duy nhất (UUID) để loại trùng.                      |
| `properties.vendor_product_id`       | String  | ID sản phẩm từ cửa hàng.                                       |
| `properties.original_transaction_id` | String  | ID giao dịch gốc.                                              |
| `properties.currency`                | String  | Mã tiền tệ.                                                    |
| `properties.store`                   | String  | Tên cửa hàng (ví dụ: "app_store").                             |
| `properties.environment`             | String  | Môi trường ("Sandbox" hoặc "Production").                      |

### Cập nhật hồ sơ người dùng \{#user-profile-updates\}

Adapty cũng cập nhật User Profile của Mixpanel bằng `people_set` với các thuộc tính sau:

| Tham số                   | Kiểu   | Mô tả                                                              |
|:--------------------------|:-------|:-------------------------------------------------------------------|
| `subscription_state`      | String | Trạng thái gói đăng ký hiện tại (ví dụ: "subscribed").            |
| `subscription_product_id` | String | ID của sản phẩm gói đăng ký đang hoạt động.                       |