Capacitor SDKでオンボーディングのデータを処理する

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

例:

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

アクションのフォーマットはこちらをご覧ください。

保存データの例(実装によってフォーマットが異なる場合があります)
// 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
        }
    }
}

ユースケース

ユーザープロファイルをデータで補完する

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

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

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
        });
      }
    }
  },
});

回答に基づいてペイウォールをカスタマイズする

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

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

  1. オンボーディングビルダーでクイズを追加し、選択肢にわかりやすいIDを割り当てます。
experience.webp
  1. クイズの回答をIDで処理し、ユーザーにカスタム属性を設定します。
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
        });
      }
    }
  },
});
  1. カスタム属性の値ごとにセグメントを作成します。
  2. プレースメントを作成し、作成した各セグメントにオーディエンスを追加します。
  3. アプリのコードでそのプレースメントのペイウォールを表示します。オンボーディングにペイウォールを開くボタンがある場合は、そのボタンのアクションへのレスポンスとしてペイウォールのコードを実装してください。