Skip to content

Applying Consent

Now that you have collected your user's consent, it is essential that you enforce these choices to your third-party frameworks.

In order to match the services provided in the Admin Interface and the frameworks running on your app, we provide a templateID.

TemplateID

This templateID is unique and can be used to apply consent in a function like this:

func updateServices() {
    guard let services = self.usercentrics.getServices() else { return }
    for service in services {
        switch service.id { // TemplateID in Admin Interface
        case "S1_9Vsuj-Q": // Google Ads Template ID
            GoogleAdsFramework.enabled = service.consent.status
        case "XX-XxX-xx": // Service Template ID
            Framework.enabled = service.consent.status
        case "x-XXXxXx": // Service Template ID
            Framework.enabled = service.consent.status
        default: break
        }
    }
}
fun updateServices(services: List<UCServiceConsentStatus>) {
    for (service in services) {
        if(service.id == "XxxXxXx") { // Template ID for "Google Ads" in Admin Interface
            GoogleAdsFramework.enabled = service.consentStatus
        } else if(service.id == "XxXxxXXx") { // Template ID for Service in Admin Interface
            Framework.enabled = service.consentStatus
        } else if(service.id == "XxXxxXXx") { // Template ID for Service in Admin Interface
            Framework.enabled = service.consentStatus
        }
    }
}
fun updateServices() {
    val services = usercentrics.getServices()
    for (service in services) {
        if(service.id == "XxxXxXx") { // Template ID for "Google Ads" in Admin Interface
            GoogleAdsFramework.enabled = service.consent.status
        } else if(service.id == "XxXxxXXx") { // Template ID for Service in Admin Interface
            Framework.enabled = service.consent.status
        } else if(service.id == "XxXxxXXx") { // Template ID for Service in Admin Interface
            Framework.enabled = service.consent.status
        }
    }
}
public static void UpdateServices()
{
    foreach (var service in Usercentrics.Instance.GetServices())
    {
        switch (service.id)
        {
            case "XxxXXxXxX": // Template ID for "Google Ads" in Admin Interface
                GoogleAdsFramework.Enabled = service.consent.status;
                break;
            case "XxXxxXXx": // Template ID for Analytics Service in Admin Interface
                AnalyticsFramework.Enabled = service.consent.status;
                break;
            default:
                break;
        }
    }
}

This function needs to be called immediately after dismissing the CMP.

predefinedUI = usercentrics.getPredefinedUI(settings: nil) {
    self.predefinedUI?.dismiss(animated: true, completion: nil)

    updateServices()
}

Process the result in the callback of registerForActivityResult:

private val usercentricsActivityLauncher = registerForActivityResult(UsercentricsActivityContract()) { services ->
    updateServices(services)
}

What to do in projects without AndroidX and old Android API levels?

You have to invoke manually startActivityForResult and process the result in the onActivityResult. You can still use UsercentricsActivityContract for that purpose:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == UsercentricsActivity.REQUEST_CODE) {
        val services = UsercentricsActivityContract().parseResult(resultCode, data)
        updateServices(services)
    }
}
predefinedUI = usercentrics.getPredefinedUI(viewContext = this) {
    layout.removeView(this.predefinedUI)
    predefinedUI = null

    updateServices()
}
Usercentrics.Instance.ShowPredefinedUI(() =>
{
    UpdateServices();
});

Always apply consent first

On every App Launches, once consent has been collected, you need to directly call this function after initializing the SDK. See Updating Consent.

Applying Consent for TCF 2.0

As defined by the IAB, consent in TCF 2.0 is stored in a TCString. Please see Applying IAB consent (TCString).