Implement consent

Learn how to implement consent in a mobile app.

The Adobe Experience Platform Consent mobile extension enables consent preferences collection from your mobile app when using the Adobe Experience Platform Mobile SDK and the Edge Network extension. Learn more about the Consent extension in the documentation.

Prerequisites

  • Successfully built and run app with SDKs installed and configured.

Learning objectives

In this lesson, you will:

  • Prompt the user for consent.
  • Update the extension based on the user response.
  • Learn how to get the current consent state.

If you followed the tutorial from the beginning, you might remember that you have set the default consent in the Consent extension to Pending - Queue events that occur before the user provides consent preferences.

To begin collecting data, you must get consent from the user. In a real-world app, you would want to consult consent best practices for your region. In this tutorial, you get consent from the user by simply asking for it with an alert:

  1. You only want to ask the user once for consent. You can do this by combining the Mobile SDK consent with the required authorization for tracking using Apple’s App Tracking Transparency framework. In this app, you assume that when the user authorizes tracking they consent to collecting events.

  2. Navigate to Luma > Luma > Utils > MobileSDK in the Xcode Project navigator.

    Add this code to the updateConsent function.

    code language-swift
    // Update consent
    let collectConsent = ["collect": ["val": value]]
    let currentConsents = ["consents": collectConsent]
    Consent.update(with: currentConsents)
    MobileCore.updateConfigurationWith(configDict: currentConsents)
    
  3. Navigate to Luma > Luma > Views > General > DisclaimerView in Xcode’s Project navigator, which is the view that is shown after installing or reinstalling the application and starting the app for the first time. The user is prompted to authorize tracking per Apple’s App Tracking Transparency framework. If the user authorizes, you also update the consent.

    Add the following code to the ATTrackingManager.requestTrackingAuthorization { status in closure.

    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")
    }
    

The Consent mobile extension automatically suppresses / pends / allows tracking based on the current consent value. You can also access the current consent state yourself:

  1. Navigate to Luma > Luma > Utils > MobileSDK in Xcode’s Project navigator.

    Add the following code to the getConsents function:

    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. Navigate to Luma > Luma > Views > General > HomeView in Xcode’s Project navigator.

    Add the following code to the .task modifier:

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

In the above example, you are simply logging the consent status to the console in Xcode. In a real-world scenario, you might use it to modify what menus or options are shown to the user.

Validate with Assurance

  1. Delete the application from your device or simulator to properly reset and initialize the tracking and consent.
  2. To connect your simulator or device to Assurance, review the setup instructions section.
  3. When moving in the app from Home screen to Products screen and back to Home screen, you should see a Get Consents Response event in the Assurance UI.
    validate consent
SUCCESS
You have now enabled your app to prompt the user at its initial start after installation (or reinstallation) to consent using the Adobe Experience Platform Mobile SDK.
Thank you for investing your time in learning about Adobe Experience Platform Mobile SDK. If you have questions, want to share general feedback, or have suggestions on future content, share them on this Experience League Community discussion post

Next: Collect lifecycle data

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