實作同意

瞭解如何在行動應用程式中實施同意。

Adobe Experience Platform同意行動擴充功能可讓您在使用Adobe Experience Platform Mobile SDK和Edge Network擴充功能時,從行動應用程式收集同意偏好設定。 在檔案中進一步瞭解同意延伸

先決條件

  • 成功建立並執行應用程式,且已安裝並設定SDK。

學習目標

在本課程中,您將會:

  • 提示使用者同意。
  • 根據使用者回應更新擴充功能。
  • 瞭解如何取得目前的同意狀態。

要求同意

如果您從頭開始按照教學課程進行,您可能會記得您已將「同意」擴充功能中的預設同意設定為​ 擱置中 — 佇列事件(在使用者提供同意偏好設定之前發生)。

若要開始收集資料,您必須取得使用者的同意。 在真實世界應用程式中,您會想要諮詢您所在地區的同意最佳實務。 在本教學課程中,您只需透過警報要求使用者同意:

iOS
  1. 您只想要求使用者同意一次。 您結合行動SDK同意與使用Apple的應用程式追蹤透明度架構進行追蹤所需的授權。 在此應用程式中,您假設當使用者授權追蹤時,他們同意收集事件。

  2. 導覽至Xcode專案導覽器中的​ Luma > Luma > Utils > MobileSDK

    將此程式碼新增至updateConsent函式。

    code language-swift
    // Update consent
    let collectConsent = ["collect": ["val": value]]
    let currentConsents = ["consents": collectConsent]
    Consent.update(with: currentConsents)
    MobileCore.updateConfigurationWith(configDict: currentConsents)
    
  3. 導覽至Xcode專案導覽器中的​ Luma > Luma > Views > General > 免責宣告檢視。 Xode的專案導覽器是在安裝或重新安裝應用程式並首次啟動應用程式後顯示的檢視。 系統會根據Apple的應用程式追蹤透明度架構,提示使用者授權追蹤。 如果使用者授權,您也會更新同意。

    將下列程式碼新增至ATTrackingManager.requestTrackingAuthorization { status in結尾。

    code language-swift
    // Add consent based on authorization
    if status == .authorized {
       // Set consent to yes
       MobileSDK.shared.updateConsent(value: "y")
    }
    else {
       // Set consent to yes
       MobileSDK.shared.updateConsent(value: "n")
    }
    
Android
  1. 您只想要求使用者同意一次。 在此應用程式中,您假設當使用者授權追蹤時,他們同意收集事件。

  2. 在Android Studio導覽器中,導覽至​ 應用程式 > kotlin+java > com.adobe.luma.tutorial.android > 模型 > MobileSDK

    將此程式碼新增至updateConsent(value: String)函式。

    code language-kotlin
    // Update consent
    val collectConsent = mapOf("collect" to mapOf("val" to value))
    val currentConsents = mapOf("consents" to collectConsent)
    Consent.update(currentConsents)
    MobileCore.updateConfiguration(currentConsents)
    
  3. 在Android Studio導覽器中,導覽至​ 應用程式 > kotlin+java > com.adobe.luma.tutorial.android > 檢視 > DisclaimerView.kt

    將下列程式碼新增至DisclaimerView(navController: NavController)// Set content to yes下方的// Set content to no函式。

    code language-kotlin
    // Add consent based on authorization
    if (status) {
       showPersonalizationWarning = false
    
       // Set consent to yes
       MobileSDK.shared.updateTrackingStatus(TrackingStatus.AUTHORIZED)
       MobileSDK.shared.updateConsent("y")
    } else {
       Toast.makeText(
             context,
             "You will not receive offers and location tracking will be disabled.",
             Toast.LENGTH_LONG
       ).show()
       showPersonalizationWarning = true
    
       // Set consent to no
       MobileSDK.shared.updateTrackingStatus(TrackingStatus.DENIED)
       MobileSDK.shared.updateConsent("n")
    }
    

取得目前的同意狀態

同意行動擴充功能會根據目前的同意值自動隱藏/擱置/允許追蹤。 您也可以自行存取目前的同意狀態:

iOS
  1. 導覽至Xcode專案導覽器中的​ Luma > Luma > Utils > MobileSDK

    將下列程式碼新增至getConsents函式:

    code language-swift
    // Get consents
    Consent.getConsents { consents, error in
       guard error == nil, let consents = consents else { return }
       guard let jsonData = try? JSONSerialization.data(withJSONObject: consents, options: .prettyPrinted) else { return }
       guard let jsonStr = String(data: jsonData, encoding: .utf8) else { return }
       Logger.aepMobileSDK.info("Consent getConsents: \(jsonStr)")
    }
    
  2. 導覽至Xcode專案導覽器中的​ Luma > Luma > Views > General > HomeView

    將下列程式碼新增至.task修飾元:

    code language-swift
    // Ask status of consents
    MobileSDK.shared.getConsents()
    

在上述範例中,您只是將同意狀態記錄到Xcode中的主控台。 在真實情境中,您可以使用它來修改要向使用者顯示哪些功能表或選項。

Android
  1. 在Android Studio導覽器中,導覽至​ 應用程式 > kotlin+java > com.adobe.luma.tutorial.android > 模型 > MobileSDK

    將下列程式碼新增至getConsents()函式:

    code language-kotlin
    // Get consents
    Consent.getConsents { callback ->
       if (callback != null) {
             val jsonStr = JSONObject(callback).toString(4)
             Log.i("MobileSDK", "Consent getConsents: $jsonStr")
       }
    }
    
  2. 在Android Studio導覽器中,導覽至​ 應用程式 > kotlin+java > com.adobe.luma.tutorial.android > 檢視 > HomeView.kt

    將下列程式碼新增至LaunchedEffect(unit)

    code language-kotlin
    // Ask status of consents
    MobileSDK.shared.getConsents()
    

在上述範例中,您只是將同意狀態記錄到Android Studio中的主控台。 在真實情境中,您可以使用它來修改要向使用者顯示哪些功能表或選項。

使用保證進行驗證

  1. 從您的裝置或模擬器刪除應用程式,以正確重設和初始化追蹤和同意。

  2. 若要將模擬器或裝置連線至Assurance,請檢閱設定指示區段。

  3. 將應用程式從​ 首頁 ​畫面移至​ 產品 ​畫面並返回​ 首頁 ​畫面時,您應該會在Assurance UI中看到​ 取得同意回應 ​事件。

    驗證同意 {modal="regular"}

SUCCESS
您現在已啟用應用程式,在安裝(或重新安裝)後最初啟動時提示使用者,以同意使用Adobe Experience Platform Mobile SDK。
感謝您花時間學習Adobe Experience Platform Mobile SDK。 如果您有疑問、想分享一般意見或有關於未來內容的建議,請在這篇Experience League社群討論貼文上分享

下一個: 收集生命週期資料

recommendation-more-help
9fed61f5-c338-47ad-8005-0b89a5f4af8b