---
title: "在 Capacitor SDK 中设置用户属性"
description: "了解如何使用 Adapty SDK 更新 Capacitor 应用中的用户属性和用户画像数据。"
---

您可以为应用用户设置可选属性，例如电子邮件、电话号码等。之后，您可以使用这些属性创建用户[市场细分](segments)，或直接在 CRM 中查看。

### 设置用户属性 \{#setting-user-attributes\}

要设置用户属性，请调用 `.updateProfile()` 方法：

```typescript showLineNumbers

const params = {
  email: 'email@email.com',
  phoneNumber: '+18888888888',
  firstName: 'John',
  lastName: 'Appleseed',
  gender: 'other',
  birthday: new Date().toISOString(),
};

try {
  await adapty.updateProfile(params);
  console.log('Profile updated successfully');
} catch (error) {
  console.error('Failed to update profile:', error);
}
```

请注意，您之前通过 `updateProfile` 方法设置的属性不会被重置。

:::tip

想了解 Adapty SDK 如何集成到移动应用中的真实示例？请查看我们的[示例应用](sample-apps)，其中展示了完整的配置过程，包括显示付费墙、完成购买以及其他基本功能。

:::

### 允许的键列表 \{#the-allowed-keys-list\}

`AdaptyProfileParameters` 允许的键及其值如下所示：

| 键 | 值 |
|---|-----|
| **email** | 字符串 |
| **phoneNumber** | 字符串 |
| **firstName** | 字符串 |
| **lastName** | 字符串 |
| **gender** | 枚举，允许的值为：`'female'`、`'male'`、`'other'` |
| **birthday** | ISO 格式的日期字符串 |

### 自定义用户属性 \{#custom-user-attributes\}

您可以设置自定义属性。这些属性通常与您的应用使用情况相关。例如，对于健身应用，可能是每周锻炼次数；对于语言学习应用，可能是用户的知识水平等。您可以在市场细分中使用这些属性来创建有针对性的付费墙和优惠，也可以在分析中用它们来找出哪些产品指标对收益影响最大。

```typescript showLineNumbers

try {
  await adapty.updateProfile({
    codableCustomAttributes: {
      key_1: 'value_1',
      key_2: 2,
    },
  });
  console.log('Custom attributes updated successfully');
} catch (error) {
  console.error('Failed to update custom attributes:', error);
}
```

要删除已有的键，请将其值设置为 `null`：

```typescript showLineNumbers

try {
  // to remove keys, pass null as their values
  await adapty.updateProfile({
    codableCustomAttributes: {
      key_1: null,
      key_2: null,
    },
  });
  console.log('Custom attributes removed successfully');
} catch (error) {
  console.error('Failed to remove custom attributes:', error);
}
```

有时您需要了解之前已设置了哪些自定义属性。为此，请使用 `AdaptyProfile` 对象的 `customAttributes` 字段。

:::warning
请注意，`customAttributes` 的值可能并非最新，因为用户属性可以随时从不同设备发送，所以服务器上的属性可能在最后一次同步后已发生变化。
:::

### 限制 \{#limits\}

- 每位用户最多 30 个自定义属性
- 键名最长 30 个字符，键名可包含字母数字字符及以下任意字符：`_`、`-`、`.`
- 值可以是字符串或浮点数，最多 50 个字符。