Skip to content

Applying Consent

Now that you have collected consent, let's make sure to respect the user's choices by applying consent to each SDK.

First, we need a way to match the declared Data Protection Services (DPS) in your configuration, with the SDKs running in the App.

How to match a service with an SDK?

Every service (DPS) available in our database, such as:

  • Google Firebase
  • Unity Ads
  • Adjust
  • AppLovin
  • Vungle

will have a unique templateID to identify it. This ID can be found under Service Settings > Data Processing Services > Service Extended Information.

TemplateID

We will be using this ID to match every declared service with it's SDK.

It is a given that consent will only change if the banner is shown to the user and an action is taken. When using the UsercentricsUI, the callback will return the new values, and you will need to use it to apply consent:

let banner = UsercentricsBanner()
banner.showFirstLayer(hostView: self, layout: <UsercentricsLayout>) { userResponse in
    self.applyConsent(with: userResponse.consents)
}
val banner = UsercentricsBanner(<Context>)
banner.showFirstLayer(layout = <UsercentricsLayout>) { userResponse ->
    applyConsent(userResponse?.consents)
}
final userResponse = await Usercentrics.showFirstLayer(layout: <UsercentricsLayout>);
applyConsent(userResponse?.consents);
const options: FirstLayerOptions = { 
    layout: <UsercentricsLayout> // .full, .sheet, .popup
};

const userResponse = await Usercentrics.showFirstLayer(options);
applyConsent(userResponse.consents);

In a similar manner, if you are building your own UI, you will need to apply consent after any consent storage action is triggered:

let consents = UsercentricsCore.shared.acceptAll(consentType: .explicit_) // also applies for "deny" and "save"
self.applyConsent(with: consents)
val consents = Usercentrics.instance.acceptAll(consentType = UsercentricsConsentType.EXPLICIT) // also applies for "deny" and "save"
applyConsent(consents)
final consents = Usercentrics.acceptAll(consentType: UsercentricsConsentType.explicit); // also applies for "deny" and "save"
applyConsent(consents);
const consents = Usercentrics.acceptAll(UsercentricsConsentType.explicit); // also applies for "deny" and "save"
applyConsent(consents);

After a new app launch

The first time you launch the app with the Usercentrics SDK, no consent will have been collected, and the status returned in isReady will let you know if you should collect consent.

However, in any future initialization after the first one, you will already have a consent status, and you will only need to apply it.

UsercentricsCore.isReady { status in
    if status.shouldCollectConsent {
        // Collect Consent
    } else {
        self.applyConsent(with: status.consents)
    }
} onFailure: { error in
    // Handle non-localized error
}
Usercentrics.isReady({ status ->
    if (status.shouldCollectConsent) {
        // Collect Consent
    } else {
        applyConsent(status.consents)
    }
},{ error ->
    // Handle non-localized error
})
try {
  final status = await Usercentrics.status;

  if (status.shouldCollectConsent) {
    //Collect Consent
  } else {
    applyConsent(status.consents);
  }
} catch (error) {
  // Handle non-localized error
}
import { Usercentrics } from '@usercentrics/react-native-sdk';

try {
  const status = await Usercentrics.status();

  if (status.shouldCollectConsent) {
    //Collect Consent
  } else {
    applyConsent(status.consents);
  }
} catch (error) {
  // Handle non-localized error
}

Getting consent on demand

You may also call UsercentricsCore.shared.getConsents() to get the consent directly, but make sure to do so after the isReady callback has been triggered.

SDKs that access and process user and/or device data, are only allowed to do so, if the user has given explicit consent. In order to be compliant, please review every SDK in your App for the following cases:

SDKs that support data privacy compliance, will provide an API to set the user's consent status. The API and it's behaviour will be documented by the SDK's provider.

e.g.

Regulation dedicated APIs

Please note that most APIs are dedicated for a specific regulation. e.g. GDPR for Europe, CCPA for US/California, COPPA for children protection, etc. Make sure to review the documentation and apply the consent for the correct regulation.

To apply consent to these SDKs, identify the target SDK and implement the consent API as documented:

func applyConsent(with consents: [UsercentricsServiceConsent]) {
    for service in consents {
        switch service.templateId {
        case "diWdt4yLB": // Google Analytics for Firebase Template ID

            // Google Firebase Consent Mode API
            let firebaseConsentStatus = service.status ? .granted : .denied
            Analytics.setConsent([
                .analyticsStorage: firebaseConsentStatus
                .adStorage: firebaseConsentStatus
            ])
            initializeFirebase()

        case "x-XXXxXx": // Other Service Template ID
            // Pass consent to framework with service.status
        default:
            // Log a warning if a service was not caught or do nothing
        }
    }
}
fun applyConsent(consents: List<UsercentricsServiceConsent>?) {
    consents?.forEach { service ->
        when (service.templateId) {
            "diWdt4yLB" -> { // Google Analytics for Firebase Template ID

                // Google Firebase Consent Mode API
                val firebaseConsentStatus = if (service.status) ConsentStatus.GRANTED else ConsentStatus.DENIED
                Firebase.analytics.setConsent {
                    analyticsStorage(firebaseConsentStatus)
                    adStorage(firebaseConsentStatus)
                }
                initializeFirebase()    

            }
            // Other Service Template ID
            "x-XXXxXx" -> {
                // Initialize or pass consent to framework with service.status
            }
            else -> {
                // Log a warning if a service was not caught or do nothing
            }
        }
    }
}
void applyConsent(List<UsercentricsServiceConsent>? consents) {
  consents?.forEach((service) {
    switch (service.templateId) {
      case "diWdt4yLB": // Google Analytics for Firebase Template ID

        // Google Firebase Consent Mode API
        applyConsentToFirebase(service.status) // See iOS and Android examples
        initializeFirebase()    

        break;
      case "x-XXXxXx": // Other Service Template ID
        // Initialize or pass consent to framework with service.status
        break;
      default:
        // Log a warning if a service was not caught or do nothing
    }
  });
}
import { UsercentricsServiceConsent } from '@usercentrics/react-native-sdk';

const applyConsents = (consents: [UsercentricsServiceConsent]) => {
    consents.forEach(service => {
        switch (service.templateId) {
            case "diWdt4yLB": // Google Analytics for Firebase Template ID

                // Google Firebase Consent Mode API
                applyConsentToFirebase(service.status) // See iOS and Android examples
                initializeFirebase()
                break;

            // Other Service Template ID
            case "x-XXXxXx":
                // Initialize or pass consent to framework with service.status
                break;

            default:
                // Log a warning if a service was not caught or do nothing
        }
    });
}

Not all APIs are created equal

Please make sure you read through each documentation, as many providers might require other steps to fully be complaint.

For SDKs that track user/device data and do not offer a consent API, the only solution is to not initialize those SDKs, when a user did not provide consent.

func applyConsent(with consents: [UsercentricsServiceConsent]) {
    for service in consents {
        switch service.templateId {
        case "x-XXXxXx": // Template ID

            // Only initialize an SDK if consent has been given
            if service.status { initializeSDK() }

        case "x-xXX-Xx": // Other Service Template ID
            //Initialize framework based on service.status
        default:
            // Log a warning if a service was not caught or do nothing
        }
    }
}
fun applyConsent(consents: List<UsercentricsServiceConsent>?) {
    consents?.forEach { service ->
        when (service.templateId) {
            "x-XXXxXx" -> { // Template ID
                // Only initialize an SDK if consent has been given
                if (service.status) {
                    initializeSDK()
                }
            }
            "x-xXX-Xx" -> { // Other Template ID
                //Initialize framework based on service.status
            }
            else -> {
                // Log a warning if a service was not caught or do nothing
            }
        }
    }
}
void applyConsent(List<UsercentricsServiceConsent>? consents) {
  consents?.forEach((service) {
    switch (service.templateId) {
      case "x-XXXxXx": // Template ID
        // Only initialize an SDK if consent has been given
        if (service.status) {
          initializeFirebase();
        }
        break;

      case "x-xXX-Xx": // Other Template ID
        // Initialize framework based on service.status
        break;

      default:
        // Log a warning if a service was not caught or do nothing
    }
  });
}
import { UsercentricsServiceConsent } from '@usercentrics/react-native-sdk';

const applyConsents = (consents: [UsercentricsServiceConsent]) => {
    consents.forEach(service => {
        switch (service.templateId) {
            case "x-XXXxXx": // Template ID
                // Only initialize an SDK if consent has been given
                if (service.status) {
                    // initializeSDK();
                }
                break;

            // Other Service Template ID
            case "x-xXX-Xx":
                // Initialize framework based on service.status
                break;

            default:
                // Log a warning if a service was not caught or do nothing
        }
    });
}

IAB Vendors (TCF 2.0)

When using the TCF 2.0 framework consent will be applied automatically to all IAB Vendors. Consent for Non-IAB vendors will need to be applied programmatically.

TC String

As specified by the IAB, the collected consent for IAB Vendors will be encoded into a TCString and stored locally in NSUserDefaults(iOS) or SharedPreferences(Android). You may access these values with their specific Keys.

let defaults = UserDefaults.standard
let tcString = defaults.string(forKey: "IABTCF_TCString")
let cmpsdkID = defaults.integer(forKey: "IABTCF_CmpSdkID")
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val tcString = preferences.getString("IABTCF_TCString")
val cmpsdkID = preferences.getInt("IABTCF_CmpSdkID")

Alternatively, we have also enabled the method:

UsercentricsCore.shared.getTCFData() { tcfData in
    let tcString = tcfData.tcString
}
Usercentrics.instance.getTCFData { tcfData ->
    val tcString = tcfData.tcString
}
Usercentrics.getTCString();
import { Usercentrics } from '@usercentrics/react-native-sdk';

const tcString = await Usercentrics.getTCFString();