> For the complete documentation index, see [llms.txt](https://developer.cscmobicorp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.cscmobicorp.com/gsm/gsm-sdk/iii.-gsm-api/gsm-users.md).

# GSM Users

Trong GSM Users có các chức năng sau:

1. [Set Properties](#1.-set-properties): Dùng để cập nhật thông tin user.
2. [Get Properties](#id-2.-get-properties): Dùng để lấy các thông tin user đã được lưu trữ.
3. [Chức năng Invite](#id-3.-chuc-nang-invite): Dùng để user A đi chia sẻ game cho user B cài đặt và nhận thưởng.

## 1. Set Properties

* Dùng để cập nhật thông tin user, phục vụ cho việc phân tích hành vi user.
* Tùy từng logic của game mà sẽ cập nhật các property tùy theo các trường hợp. Ví dụ: Khi có thay đổi về avatar, name, userType thì sẽ cập nhật luôn thông tin user.

```csharp
 void SetUserProperties(params Property[] properties)
```

**Các tham số**

* **`properties`**: Mảng các thuộc tính của user

Hoặc:

```csharp
void SetUserProperties(Dictionary<string, object> parameters)
```

**Các tham số**

* parameters: Là một dictionary chứa các thuộc tính của user

Code mẫu:

```csharp
GSM.Users.GSMUser.SetUserProperties(new GSM.Models.Property[]
{
    new GSM.Models.Property(GSM.Models.PropertyPreset.name, "Sample"), // Thường thay đổi khi user đổi tên (tùy chọn)
    new GSM.Models.Property(GSM.Models.PropertyPreset.avatar, "1"), // avatar id có sẵn trong game (tùy chọn)
    new GSM.Models.Property(GSM.Models.PropertyPreset.facebookId, ""), // Khi đăng nhập bằng facebook (tùy chọn)
    new GSM.Models.Property(GSM.Models.PropertyPreset.email, "tuyennv@cscmobi.com"), // Khi đăng nhập bằng email (tùy chọn)
    new GSM.Models.Property(GSM.Models.PropertyPreset.firebaseId, "TslbnCoZ0DRzqEjxNRwSpqkFp0B2"), // Khi đồng bộ tiến trình game lên realtimeDatabase/Firestore của Firebase (nếu có tính năng lưu tiến trình thì bắt buộc)
    new GSM.Models.Property(GSM.Models.PropertyPreset.lang, "vi"), // Thường thay đổi trong phần đổi ngôn ngữ 
    new GSM.Models.Property(GSM.Models.PropertyPreset.userType, "2"), //Khi thay đổi userType (tùy chọn)
    new GSM.Models.Property(GSM.Models.PropertyPreset.scene, "Splash"), //Track màn hình user đang tương tác (bắt buộc)
    new GSM.Models.Property(GSM.Models.PropertyPreset.deviceName, "Iphone 12 Pro Max")
});

```

### 1.1 Set FCM Token

* FCM Token là token được firebase trả lại khi tích hợp Firebase Cloud Messaging
* Mục đích để server sẽ gửi remote notification về device thông qua token này.
* Thông thường chỉ khi lần đầu tiên mở game, hoặc có thay đổi về token thì sẽ thực hiện chức năng này.

Code mẫu:

```csharp
Firebase.Messaging.FirebaseMessaging.GetTokenAsync().ContinueWithOnMainThread(task =>
{
    //Lấy fcmToken từ Firebase
    string fcmToken=task.Result;
    //Update property fcmToken lên GSM
    GSM.Users.GSMUser.SetUserProperties(new GSM.Models.Property[]
    {
        new GSM.Models.Property(GSM.Models.PropertyPreset.fcmToken, fcmToken)
    });
});
```

### 1.2 Set UserType

* Dùng để phân loại user, tùy từng game mà userType có các mức phân loại user khác nhau.

Code mẫu:

```csharp
GSM.Users.GSMUser.SetUserProperties(new GSM.Models.Property[]
{
    new GSM.Models.Property(GSM.Models.PropertyPreset.userType, <loại user>)
});
```

### 1.3 Set Language

* Dùng để đồng bộ thông tin ngôn ngữ của user trên GSM và Game

Code mẫu:

```csharp
GSM.Users.GSMUser.SetUserProperties(new GSM.Models.Property[]
{
    new GSM.Models.Property(GSM.Models.PropertyPreset.lang, <mã ngôn ngữ>)
});
```

## 2. Get Properties

* Dùng để lấy các thuộc tính đã set của user
* Có thể lấy 1 hoặc nhiều thuộc tính tùy ý.

```csharp
void GetProperties(PropertyPreset[] fields, Action<string> onSuccess, Action<long, string> errorCallBack = null)
```

**Các tham số**

* **`fields`**: Là danh sách các thuộc tính muốn lấy, nếu là null thì sẽ kết quả trả về tất cả các thuộc tính đang có.
* **`onSuccess`**: Hàm được thực hiện khi server trả kết quả thành công.
* **`errorCallBack`** : Hàm được thực hiện khi có lỗi xảy ra

Code mẫu 1: Lấy theo thuộc tính chỉ định

```csharp
GSMUser.GetProperties(new PropertyPreset[]
{
    PropertyPreset.name,
    PropertyPreset.data
}, (json) =>
{
    Debug.Log($"User Properties: {json}");
    //Biến đổi từ json sang Dictionary để xử lý
    var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
    string name = dict["name"].ToString();
    Debug.Log($"name: {name}");
});

```

Code mẫu 2: Lấy toàn bộ các thuộc tính

```csharp
GSMUser.GetProperties(null, (json) =>
{
    Debug.Log($"User Properties: {json}");
});

```

**Hoặc**

```csharp
void GetProperties(Action<UserProperties> onSuccess, Action<long, string> errorCallBack = null)
```

**Các tham số**

* **`onSuccess`**: Hàm được thực hiện khi server trả kết quả thành công.
* **`onFail`**: Hàm được thực hiện khi có lỗi xảy ra

Code mẫu:

```csharp
GSMUser.GetProperties(data =>
{
    Debug.Log($"User Name: {data.name}");
    Debug.Log($"User Avatar: {data.avatar}");
    Debug.Log($"iapCount: {data.iapCount}");
    Debug.Log($"iapRevenue: {data.iapRevenue}");
    Debug.Log($"maxIAP: {data.maxIAP}");
    Debug.Log($"avgIAP: {data.avgIAP}");

    if (data.data != null)
    {
        string jsonData = JsonConvert.SerializeObject(data.data);
    }
}, (statusCode, error) =>
{
    Debug.LogError($"GSMUser.GetProperties error: {error}");
});
```

## 3. Set Custom Data

* Mục đích để lưu thêm các thông tin về user, và có thể tùy biến theo từng game

```csharp
void SetCustomData(Dictionary<string, object> parameters)
```

**Các tham số**

* **`parameters`**: Mảng các thuộc key và value

```csharp
GSMUser.SetCustomData(
    new Dictionary<string, object>() {
        { "level", 1},
        { "userType", "free" }
    }
)
```

## 4. Get Custom Data

* Mục đích lấy các data tùy biến của user đã được set trước đó.

```csharp
void GetCustomData(List<string> keys, Action<string> onSuccess, Action<long, string> errorCallBack = null)
```

**Các tham số**

* **`keys`**: Mảng các thuộc key cần lấy
* **`onSuccess`**: Là action để xử lý kết quả là 1 json.
* **`errorCallBack`** : Action xử lý khi có lỗi xảy ra

```csharp
GSMUser.GetCustomData(new List<string>() { "level", "userType" }, (result) =>
{
    Debug.Log($"Result: {result}");
});
```

## 5. Chức năng Invite

* Chức năng này dùng khi user A chia sẻ cho user B cài đặt game/app qua link chia sẻ, hoặc có thể nhập mã chia sẻ.
* **Quy trình:** <mark style="color:red;">(1. User A Lấy mã chia sẻ hoặc link chia sẻ)</mark>==> <mark style="color:green;">(2. User B cài đặt từ link chia sẻ hoặc nhập mã chia sẻ của user A)</mark>
* Sau khi thực hiện quy trình trên, thì server sẽ ghi nhận user B được chia sẻ từ user A.

### 5.1 Lấy mã chia sẻ (Dành cho user A)

* Dùng để lấy ra mã chia sẻ của user, và tạo ra link để user chia sẻ game cho user khác

Code mẫu:

```csharp
GSMUser.GetInviteCode(inviteCode =>
{
    Debug.Log($"InviteCode: {inviteCode}");
});
```

### 5.2 Lấy link chia sẻ (Dành cho user A)

* Đây là link sử dụng link của Adjust, để có thể tạo link thì trên GSM cần phải cấu hình link vào InviteConfig trên GSM

<figure><img src="/files/CxsW2kuOOAbkFHThg74o" alt=""><figcaption></figcaption></figure>

> Code mẫu:

```csharp
GSMUser.GetInviteLink(link =>
{
    //link chia se
});
```
