---
title: "Amplitude"
description: "Tích hợp Amplitude với Adapty để hiểu rõ hơn về hành vi người dùng."
---

[Amplitude](https://amplitude.com/) là một dịch vụ phân tích di động mạnh mẽ. Với Adapty, bạn có thể dễ dàng gửi sự kiện đến Amplitude, xem cách người dùng tương tác, và đưa ra các quyết định thông minh.

Adapty cung cấp một bộ dữ liệu đầy đủ cho phé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à gửi chúng đến tài khoản Amplitude của bạn. Điều này giúp bạn kết hợp hành vi người dùng với lịch sử thanh toán trong Amplitude, từ đó đưa ra các quyết định sản phẩm tốt hơn.

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

Trong Adapty, bạn có thể thiết lập các flow riêng biệt cho **sự kiện production** và **sự kiện test** từ môi trường sandbox của Apple hoặc Stripe, hoặc tài khoản test của Google.

- Đối với sự kiện production, nhập các API key **Production** từ dashboard Amplitude, với một API key riêng cho từng nền tảng: iOS, Android và Stripe.
- Đối với sự kiện test, sử dụng các trường **Sandbox** khi cần.

Để thiết lập tích hợp Amplitude:

1. Mở [**Integrations** -> **Amplitude**](https://app.adapty.io/integrations/amplitude) trong Adapty Dashboard của bạn.

  <img src="/assets/shared/img/3b50552-CleanShot_2023-08-15_at_16.47.102x.webp"
  style={{
    border: '1px solid #727272', /* border width and color */
    width: '700px', /* image width */
    display: 'block', /* for alignment */
    margin: '0 auto' /* center alignment */
  }}
/>

2. Bật **Amplitude integration** để kích hoạt.

3. Điền vào các trường tích hợp:

    | Trường                                     | Mô tả                                                        |
    | ------------------------------------------ | ------------------------------------------------------------ |
    | **Amplitude iOS/ Android/ Stripe API key** | Nhập **API Key** của Amplitude cho iOS/ Android/ Stripe vào Adapty. Tìm thấy nó trong **Project settings** trên Amplitude. Để được hỗ trợ, xem [tài liệu Amplitude](https://amplitude.com/docs/apis/authentication). Bắt đầu với các key **Sandbox** để thử nghiệm, sau đó chuyển sang key **Production** sau khi thử nghiệm thành công. |

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

4. Các cài đặt tùy chọn để tùy chỉnh thêm:

   | Tham số                                 | Mô tả                                                        |
   | --------------------------------------- | ------------------------------------------------------------ |
   | **How the revenue data should be sent** | Chọn gửi doanh thu gộp hay doanh thu sau thuế và hoa hồng. Xem [Hoa hồng cửa hàng và thuế](controls-filters-grouping-compare-proceeds#display-gross-or-net-revenue) để biết thêm chi tiết. |
   | **Exclude historical events**           | Chọn để loại trừ các sự kiện trước khi cài đặt Adapty SDK, tránh dữ liệu bị trùng lặp. Ví dụ: nếu người dùng đăng ký vào ngày 10 tháng 1 nhưng cài đặt Adapty SDK vào ngày 6 tháng 3, Adapty chỉ gửi các sự kiện từ ngày 6 tháng 3 trở đi. |
   | **Send User Attributes**                | Chọn tùy chọn này để gửi các thuộc tính người dùng như tùy chọn ngôn ngữ. |
   | **Always populate user_id**             | Adapty tự động gửi `device_id` dưới dạng `amplitudeDeviceId`. Đối với `user_id`, cài đặt này xác định hành vi: <ul><li>**ON**: Gửi `profile_id` của Adapty nếu `amplitudeUserId` hoặc `customer_user_id` không có sẵn.</li><li>**OFF**: Để trống `user_id` nếu không có ID nào khả dụng.</li></ul> |

5. Chọn các sự kiện bạn muốn nhận và [ánh xạ tên của chúng](amplitude#events-and-tags).

6. Nhấn **Save** để xác nhận thay đổi.

Sau khi nhấn **Save**, Adapty sẽ bắt đầu gửi sự kiện đến Amplitude.

Ngoài các sự kiện, Adapty còn gửi [trạng thái gói đăng ký](subscription-status) và ID sản phẩm gói đăng ký đến [thuộc tính người dùng Amplitude](https://amplitude.com/docs/data/user-properties-and-events).

### Sự kiện và tags \{#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 Amplitude 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/da67694-CleanShot_2023-08-15_at_16.52.352x.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. Adapty sẽ gửi các sự kiện gói đăng ký đến Amplitude thông qua tích hợp server-to-server, cho phép bạn xem tất cả sự kiện gói đăng ký trong dashboard Amplitude của mình.

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

Sử dụng phương thức `setIntegrationIdentifier()` để thiết lập tham số `amplitude_device_id`. Đây là bước bắt buộc để thiết lập tích hợp.

Nếu bạn có đăng ký người dùng, bạn cũng có thể truyền `amplitude_user_id`.

---
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>

**Setting amplitudeDeviceId**

```swift showLineNumbers

do {
    try await Adapty.setIntegrationIdentifier(
        key: "amplitude_device_id", 
        value: Amplitude.instance().deviceId
    )
} catch {
    // handle the error
}
```

**Setting amplitudeUserId**

```swift showLineNumbers

do {
    try await Adapty.setIntegrationIdentifier(
        key: "amplitude_user_id", 
        value: "YOUR_AMPLITUDE_USER_ID"
    )
} catch {
    // handle the error
}
```

</TabItem>
<TabItem value="kotlin" label="Android (Kotlin)" default>

**Setting amplitudeDeviceId**

```kotlin showLineNumbers 
//for Amplitude maintenance SDK (obsolete)
val amplitude = Amplitude.getInstance()
val amplitudeDeviceId = amplitude.getDeviceId()
val amplitudeUserId = amplitude.getUserId()

//for actual Amplitude Kotlin SDK
val amplitude = Amplitude(
    Configuration(
        apiKey = AMPLITUDE_API_KEY,
        context = applicationContext
    )
)
val amplitudeDeviceId = amplitude.store.deviceId

//

Adapty.setIntegrationIdentifier("amplitude_device_id", amplitudeDeviceId) { error ->
    if (error != null) {
        // handle the error
    }
}
```

**Setting amplitudeUserId**

```kotlin showLineNumbers
//for Amplitude maintenance SDK (obsolete)
val amplitude = Amplitude.getInstance()
val amplitudeDeviceId = amplitude.getDeviceId()
val amplitudeUserId = amplitude.getUserId()

//for actual Amplitude Kotlin SDK
val amplitude = Amplitude(
    Configuration(
        apiKey = AMPLITUDE_API_KEY,
        context = applicationContext
    )
)
val amplitudeUserId = amplitude.store.userId

//

Adapty.setIntegrationIdentifier("amplitude_user_id", amplitudeUserId) { error ->
    if (error != null) {
        // handle the error
    }
}
```

</TabItem>
<TabItem value="Flutter" label="Flutter (Dart)" default>

**Setting amplitudeDeviceId**

```javascript showLineNumbers

final Amplitude amplitude = Amplitude.getInstance(instanceName: "YOUR_INSTANCE_NAME");

try {
    await Adapty().setIntegrationIdentifier(
        key: "amplitude_device_id", 
        value: amplitude.getDeviceId(),
    );
} on AdaptyError catch (adaptyError) {
    // handle the error
} catch (e) {
    // handle the error
}
```

**Setting amplitudeUserId**

```javascript showLineNumbers

final Amplitude amplitude = Amplitude.getInstance(instanceName: "YOUR_INSTANCE_NAME");

try {
    await Adapty().setIntegrationIdentifier(
        key: "amplitude_user_id", 
        value: "YOUR_AMPLITUDE_USER_ID",
    );
} on AdaptyError catch (adaptyError) {
    // handle the error
} catch (e) {
    // handle the error
}
```

</TabItem>
<TabItem value="Unity" label="Unity (C#)" default>

**Setting amplitudeDeviceId**

```csharp showLineNumbers
using AdaptySDK;

Adapty.SetIntegrationIdentifier(
  "amplitude_device_id", 
  amplitude.getDeviceId(), 
  (error) => {
  // handle the error
});
```

**Setting amplitudeUserId**

```csharp showLineNumbers
using AdaptySDK;

Adapty.SetIntegrationIdentifier(
  "amplitude_user_id", 
  "YOUR_AMPLITUDE_USER_ID", 
  (error) => {
  // handle the error
});
```

</TabItem>
<TabItem value="rn" label="React Native (TS)" default>

**Setting amplitudeDeviceId**

```typescript showLineNumbers

try {
  await adapty.setIntegrationIdentifier("amplitude_device_id", deviceId);
} catch (error) {
  // handle `AdaptyError`
}
```

**Setting amplitudeUserId**

```typescript showLineNumbers

try {
  await adapty.setIntegrationIdentifier("amplitude_user_id", userId);
} catch (error) {
  // handle `AdaptyError`
}
```

</TabItem>
</Tabs>

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

Adapty gửi sự kiện đến Amplitude qua HTTP API v2. Mỗi sự kiện có cấu trúc như sau:

```json
{
  "api_key": "your_amplitude_api_key",
  "events": [
    {
      "partner_id": "adapty",
      "event_type": "subscription_renewed",
      "time": 1709294400000,
      "insert_id": "123e4567-e89b-12d3-a456-426614174000",
      "user_id": "user_12345",
      "device_id": "device_12345",
      "platform": "iOS",
      "os_name": "iOS",
      "productId": "yearly.premium.6999",
      "revenue": 9.99,
      "event_properties": {
        "vendor_product_id": "yearly.premium.6999",
        "original_transaction_id": "GPA.3383...",
        "currency": "USD",
        "environment": "Production",
        "store": "app_store"
      },
      "user_properties": {
        "subscription_state": "subscribed",
        "subscription_product": "yearly.premium.6999"
      }
    }
  ]
}
```

Trong đó:

| Tham số                     | Kiểu   | Mô tả                                                                |
|:----------------------------|:-------|:---------------------------------------------------------------------|
| `api_key`                   | String | API Key Amplitude của bạn.                                           |
| `events`                    | Array  | Danh sách các đối tượng sự kiện (Adapty gửi từng cái một).           |
| `events[].partner_id`       | String | Luôn là "adapty".                                                    |
| `events[].event_type`       | String | Tên sự kiện (được ánh xạ từ sự kiện Adapty).                         |
| `events[].time`             | Long   | Dấu thời gian của sự kiện tính bằng mili giây.                       |
| `events[].insert_id`        | String | ID sự kiện duy nhất (UUID).                                          |
| `events[].user_id`          | String | Amplitude User ID hoặc Customer User ID.                             |
| `events[].device_id`        | String | Amplitude Device ID.                                                 |
| `events[].platform`         | String | Nền tảng (ví dụ: "iOS", "Android").                                  |
| `events[].os_name`          | String | Tên hệ điều hành.                                                    |
| `events[].productId`        | String | ID sản phẩm từ cửa hàng.                                             |
| `events[].revenue`          | Float  | Số tiền doanh thu.                                                   |
| `events[].event_properties` | Object | Thuộc tính sự kiện chi tiết (chứa tất cả [trường sự kiện](webhook-event-types-and-fields#for-most-event-types) hiện có). |
| `events[].user_properties`  | Object | Thuộc tính người dùng như trạng thái gói đăng ký.                    |