ÿÿ Onboardings | Kotlin Multiplatform SDK | Tài liệu Adapty

Xử lý dữ liệu từ onboarding trong Kotlin Multiplatform SDK

Onboarding đã bị deprecated trong SDK v4 và sẽ bị xóa trong các phiên bản tương lai. Chúng không còn nhận được các bản sá»­a lá»—i hay cải tiến nữa. Hãy sá»­ dụng flows thay thế: khác vá»›i onboarding chạy trong WebView, flows render trá»±c tiếp trên thiết bị — mang lại hiệu ứng animation mượt mà hÆ¡n, giao diện native nhất quán, thá»i gian tải nhanh hÆ¡n và không phụ thuá»™c vào WebView runtime. Xem Lấy flows & paywalls và Hiển thị flows & paywalls để bắt đầu.

Khi ngưá»i dùng trả lá»i câu há»i trong bài quiz hoặc nhập dữ liệu vào trưá»ng nhập liệu, phương thức onboardingViewOnStateUpdatedAction sẽ được gá»i. Bạn có thể lưu hoặc xá»­ lý loại trưá»ng đó trong code cá»§a mình.

Ví dụ:


class MyAdaptyUIOnboardingsEventsObserver : AdaptyUIOnboardingsEventsObserver {
    override fun onboardingViewOnStateUpdatedAction(
        view: AdaptyUIOnboardingView,
        meta: AdaptyUIOnboardingMeta,
        elementId: String,
        params: AdaptyOnboardingsStateUpdatedParams
    ) {
        // Store user preferences or responses
        when (params) {
            is AdaptyOnboardingsSelectParams -> {
                // Handle single selection
                val id = params.id
                val value = params.value
                val label = params.label
                AppLogger.d("Selected option: $label (id: $id, value: $value)")
            }
            is AdaptyOnboardingsMultiSelectParams -> {
                // Handle multiple selections
            }
            is AdaptyOnboardingsInputParams -> {
                // Handle text input
            }
            is AdaptyOnboardingsDatePickerParams -> {
                // Handle date selection
            }
        }
    }
}

// Set up the observer
AdaptyUI.setOnboardingsEventsObserver(MyAdaptyUIOnboardingsEventsObserver())
Ví dụ dữ liệu đã lưu (định dạng có thể khác trong phần triển khai của bạn)
    // Example of a saved select action
    {
        "id": "onboarding_on_state_updated_action",
        "view": { /* AdaptyUI.OnboardingView object */ },
        "meta": {
        "onboarding_id": "onboarding_123",
        "screen_cid": "preferences_screen",
        "screen_index": 1,
        "total_screens": 3
    },
        "action": {
        "element_id": "preference_selector",
        "element_type": "select",
        "value": {
        "id": "option_1",
        "value": "premium",
        "label": "Premium Plan"
    }
    }
    }

    // Example of a saved multi-select action
    {
        "id": "onboarding_on_state_updated_action",
        "view": { /* AdaptyUI.OnboardingView object */ },
        "meta": {
        "onboarding_id": "onboarding_123",
        "screen_cid": "interests_screen",
        "screen_index": 2,
        "total_screens": 3
    },
        "action": {
        "element_id": "interests_selector",
        "element_type": "multi_select",
        "value": [
    {
        "id": "interest_1",
        "value": "sports",
        "label": "Sports"
    },
    {
        "id": "interest_2",
        "value": "music",
        "label": "Music"
    }
        ]
    }
    }

    // Example of a saved input action
    {
        "id": "onboarding_on_state_updated_action",
        "view": { /* AdaptyUI.OnboardingView object */ },
        "meta": {
        "onboarding_id": "onboarding_123",
        "screen_cid": "profile_screen",
        "screen_index": 0,
        "total_screens": 3
    },
        "action": {
        "element_id": "name_input",
        "element_type": "input",
        "value": {
        "type": "text",
        "value": "John Doe"
    }
    }
    }

    // Example of a saved date picker action
    {
        "id": "onboarding_on_state_updated_action",
        "view": { /* AdaptyUI.OnboardingView object */ },
        "meta": {
        "onboarding_id": "onboarding_123",
        "screen_cid": "profile_screen",
        "screen_index": 0,
        "total_screens": 3
    },
        "action": {
        "element_id": "birthday_picker",
        "element_type": "date_picker",
        "value": {
        "day": 15,
        "month": 6,
        "year": 1990
    }
    }
    }

Các trưá»ng hợp sá»­ dụng

Làm phong phú hồ sÆ¡ ngưá»i dùng vá»›i dữ liệu

Nếu bạn muốn liên kết ngay dữ liệu đầu vào vá»›i hồ sÆ¡ ngưá»i dùng và tránh há»i há» hai lần cùng má»™t thông tin, bạn cần cập nhật hồ sÆ¡ ngưá»i dùng vá»›i dữ liệu đầu vào khi xá»­ lý action.

Ví dụ: bạn yêu cầu ngưá»i dùng nhập tên vào trưá»ng văn bản có ID là name, và bạn muốn đặt giá trị cá»§a trưá»ng này làm tên cá»§a ngưá»i dùng. Ngoài ra, bạn yêu cầu há» nhập email vào trưá»ng email. Trong code cá»§a ứng dụng, nó có thể trông như thế này:


class MyAdaptyUIOnboardingsEventsObserver : AdaptyUIOnboardingsEventsObserver {
    override fun onboardingViewOnStateUpdatedAction(
        view: AdaptyUIOnboardingView,
        meta: AdaptyUIOnboardingMeta,
        elementId: String,
        params: AdaptyOnboardingsStateUpdatedParams
    ) {
        // Store user preferences or responses
        when (params) {
            is AdaptyOnboardingsInputParams -> {
                // Handle text input
                val builder = AdaptyProfileParameters.Builder()

                // Map elementId to appropriate profile field
                when (elementId) {
                    "name" -> {
                        when (val input = params.input) {
                            is AdaptyOnboardingsTextInput -> {
                                builder.withFirstName(input.value)
                            }
                        }
                    }
                    "email" -> {
                        when (val input = params.input) {
                            is AdaptyOnboardingsEmailInput -> {
                                builder.withEmail(input.value)
                            }
                        }
                    }
                }

                // Update profile asynchronously
                mainUiScope.launch {
                    val profileParams = builder.build()
                    val result = Adapty.updateProfile(profileParams)
                    result.onSuccess { profile ->
                        // Profile updated successfully
                        AppLogger.d("Profile updated: ${profile.email}")
                    }.onError { error ->
                        // Handle the error
                        AppLogger.e("Failed to update profile: ${error.message}")
                    }
                }
            }
        }
    }
}

// Set up the observer
AdaptyUI.setOnboardingsEventsObserver(MyAdaptyUIOnboardingsEventsObserver())

Tùy chỉnh paywall dá»±a trên câu trả lá»i

Bằng cách sá»­ dụng các bài quiz trong onboarding, bạn cÅ©ng có thể tùy chỉnh paywall hiển thị cho ngưá»i dùng sau khi há» hoàn thành onboarding.

Ví dụ, bạn có thể há»i ngưá»i dùng vá» kinh nghiệm thể thao cá»§a há» và hiển thị các CTA và sản phẩm khác nhau cho các nhóm ngưá»i dùng khác nhau.

  1. Thêm má»™t bài quiz trong trình tạo onboarding và gán ID có ý nghÄ©a cho các tùy chá»n cá»§a nó.
experience.webp
  1. Xá»­ lý các phản hồi bài quiz dá»±a trên ID cá»§a chúng và đặt thuá»™c tính tùy chỉnh cho ngưá»i dùng.

class MyAdaptyUIOnboardingsEventsObserver : AdaptyUIOnboardingsEventsObserver {
    override fun onboardingViewOnStateUpdatedAction(
        view: AdaptyUIOnboardingView,
        meta: AdaptyUIOnboardingMeta,
        elementId: String,
        params: AdaptyOnboardingsStateUpdatedParams
    ) {
        // Handle quiz responses and set custom attributes
        when (params) {
            is AdaptyOnboardingsSelectParams -> {
                // Handle quiz selection
                val builder = AdaptyProfileParameters.Builder()

                // Map quiz responses to custom attributes
                when (elementId) {
                    "experience" -> {
                        // Set custom attribute 'experience' with the selected value (beginner, amateur, pro)
                        builder.withCustomAttribute("experience", params.value)
                    }
                }

                // Update profile asynchronously
                mainUiScope.launch {
                    val profileParams = builder.build()
                    val result = Adapty.updateProfile(profileParams)
                    result.onSuccess { profile ->
                        // Profile updated successfully
                        AppLogger.d("Custom attribute 'experience' set to: ${params.value}")
                    }.onError { error ->
                        // Handle the error
                        AppLogger.e("Failed to update profile: ${error.message}")
                    }
                }
            }
        }
    }
}

// Set up the observer
AdaptyUI.setOnboardingsEventsObserver(MyAdaptyUIOnboardingsEventsObserver())
  1. Tạo phân khúc cho mỗi giá trị thuộc tính tùy chỉnh.
  2. Tạo một placement và thêm đối tượng cho mỗi phân khúc bạn đã tạo.
  3. Hiển thị paywall cho placement trong code của ứng dụng. Nếu onboarding của bạn có nút mở paywall, hãy triển khai code paywall như một phản hồi cho hành động của nút này.
ÿÿÿÿ