---
title: "Capacitor SDKでオンボーディングのデータを処理する"
description: "Adapty SDKを使ってCapacitorアプリのオンボーディングからデータを保存・活用する方法。"
---

ユーザーがクイズに回答したり入力フィールドにデータを入力したりすると、`onStateUpdatedAction` メソッドが呼び出されます。コード内でフィールドの種類を保存したり処理したりできます。

例：

```typescript
view.setEventHandlers({
  onStateUpdated(action, meta) {
    // Process data
  },
});
```

アクションのフォーマットは[こちら](https://capacitor.adapty.io/types/onboardingstateupdatedaction)をご覧ください。

<Details>
<summary>保存データの例（実装によってフォーマットが異なる場合があります）</summary>

```javascript
// Example of a saved select action
{
    "elementId": "preference_selector",
    "meta": {
        "onboardingId": "onboarding_123",
        "screenClientId": "preferences_screen",
        "screenIndex": 1,
        "screensTotal": 3
    },
    "params": {
        "type": "select",
        "value": {
            "id": "option_1",
            "value": "premium",
            "label": "Premium Plan"
        }
    }
}

// Example of a saved multi-select action
{
    "elementId": "interests_selector",
    "meta": {
        "onboardingId": "onboarding_123",
        "screenClientId": "interests_screen",
        "screenIndex": 2,
        "screensTotal": 3
    },
    "params": {
        "type": "multiSelect",
        "value": [
            {
                "id": "interest_1",
                "value": "sports",
                "label": "Sports"
            },
            {
                "id": "interest_2",
                "value": "music",
                "label": "Music"
            }
        ]
    }
}

// Example of a saved input action
{
    "elementId": "name_input",
    "meta": {
        "onboardingId": "onboarding_123",
        "screenClientId": "profile_screen",
        "screenIndex": 0,
        "screensTotal": 3
    },
    "params": {
        "type": "input",
        "value": {
            "type": "text",
            "value": "John Doe"
        }
    }
}

// Example of a saved date picker action
{
    "elementId": "birthday_picker",
    "meta": {
        "onboardingId": "onboarding_123",
        "screenClientId": "profile_screen",
        "screenIndex": 0,
        "screensTotal": 3
    },
"params": {
    "type": "datePicker",
    "value": {
        "day": 15,
        "month": 6,
        "year": 1990
        }
    }
}
```
</Details>

## ユースケース \{#use-cases\}

### ユーザープロファイルをデータで補完する \{#enrich-user-profiles-with-data\}

入力データをすぐにユーザープロファイルと紐付けて、同じ情報を二度聞かないようにするには、アクション処理時に入力データで[ユーザープロファイルを更新](capacitor-setting-user-attributes)する必要があります。

たとえば、ユーザーに `name` というIDのテキストフィールドに名前を入力してもらい、その値をユーザーの名として設定したい場合や、`email` フィールドにメールアドレスを入力してもらう場合、アプリのコードは次のようになります：

```typescript showLineNumbers
view.setEventHandlers({
  onStateUpdated(action, meta) {
    // Store user preferences or responses
    if (action.elementType === 'input') {
      const profileParams: any = {};
      
      // Map elementId to appropriate profile field
      switch (action.elementId) {
        case 'name':
          if (action.value.type === 'text') {
            profileParams.firstName = action.value.value;
          }
          break;
        case 'email':
          if (action.value.type === 'email') {
            profileParams.email = action.value.value;
          }
          break;
      }
      
      // Update profile if we have data to update
      if (Object.keys(profileParams).length > 0) {
        adapty.updateProfile({ params: profileParams }).catch((error) => {
          // handle the error
        });
      }
    }
  },
});
```

### 回答に基づいてペイウォールをカスタマイズする \{#customize-paywalls-based-on-answers\}

オンボーディングのクイズを活用して、オンボーディング完了後にユーザーに表示するペイウォールをカスタマイズすることもできます。

たとえば、スポーツの経験について質問し、ユーザーグループごとに異なるCTAやプロダクトを表示できます。

1. オンボーディングビルダーで[クイズを追加](onboarding-quizzes)し、選択肢にわかりやすいIDを割り当てます。

2. クイズの回答をIDで処理し、ユーザーに[カスタム属性を設定](capacitor-setting-user-attributes)します。

```typescript showLineNumbers
view.setEventHandlers({
  onStateUpdated(action, meta) {
    // Handle quiz responses and set custom attributes
    if (action.elementType === 'select') {
      const profileParams: any = {};
      
      // Map quiz responses to custom attributes
      switch (action.elementId) {
        case 'experience':
          // Set custom attribute 'experience' with the selected value (beginner, amateur, pro)
          profileParams.codableCustomAttributes = {
            experience: action.value.value
          };
          break;
      }
      
      // Update profile if we have data to update
      if (Object.keys(profileParams).length > 0) {
        adapty.updateProfile({ params: profileParams }).catch((error) => {
          // handle the error
        });
      }
    }
  },
});
```

3. カスタム属性の値ごとに[セグメントを作成](segments)します。
4. [プレースメント](placements)を作成し、作成した各セグメントに[オーディエンス](audience)を追加します。
5. アプリのコードでそのプレースメントの[ペイウォールを表示](capacitor-paywalls)します。オンボーディングにペイウォールを開くボタンがある場合は、[そのボタンのアクションへのレスポンス](capacitor-handling-onboarding-events#opening-a-paywall)としてペイウォールのコードを実装してください。