Skip to content

Initializing Usercentrics

Let's get started with the integration:

  1. Import Usercentrics, configure your options and initialize the SDK:

    // On AppDelegate
    import Usercentrics
    
    let options = UsercentricsOptions(settingsId: <SettingsID>)
    UsercentricsCore.configure(options: options)
    
    // On Application
    import com.usercentrics.sdk.*
    
    val options = UsercentricsOptions(settingsId = <SettingsID>)
    Usercentrics.initialize(this, options)
    
    // e.g place this inside the [initState] of the Entry Point Widget
    import 'package:usercentrics_sdk/usercentrics_sdk.dart';
    
    Usercentrics.initialize(
      settingsId: <SettingsID>,
    );
    
    // On your App entrypoint
    import { Usercentrics, UsercentricsOptions } from '@usercentrics/react-native-sdk';
    
    // React hooks
    useEffect(() => {
       let options = new UsercentricsOptions(<SettingsID>);
       Usercentrics.configure(options);
    }, []);
    
    // Or via constructor
    constructor(props: any) {
       super(props)
    
       let options = new UsercentricsOptions(<SettingsID>)
       Usercentrics.configure(options)
    }
    

    First Init

    We recommend initializing the SDK in the background as soon as possible after app launch to avoid any loading delays. After the first init, the SDK will cache essential data and following initializations will be immediate.

    Resetting the SDK

    Use reset() to clean all local storage and release the initialized instance. You will need to initialize the SDK again after a reset. Make sure you validate the expected behaviour, before using reset in production.

    Switching SettingsIDs

    If you need to switch SettingsIDs during runtime, just reinitialize the SDK with the new SettingsID. This will automatically trigger a reset(), and initialize the new configuration.

  2. Use isReady to fetch the latest consent status. This status will let you know if you need to show the banner to collect consent or only apply the already collected consent.

    import Usercentrics
    
    UsercentricsCore.isReady { [weak self] status in
        guard let self = self else { return }
        if status.shouldCollectConsent {
            // Show banner to collect consent
        } else {
            // Apply consent with status.consents
        }
    } onFailure: { error in 
        // Handle non-localized error
    }
    
    import com.usercentrics.sdk.*
    
    Usercentrics.isReady({ status ->
        if (status.shouldCollectConsent) {
            // Show banner to collect consent
        } else {
            // Apply consent with status.consents
        }
    }, { error ->
        // Handle non-localized error
    })
    
    import 'package:usercentrics_sdk/usercentrics_sdk.dart';
    
    try {
        final status = await Usercentrics.status;
        if (status.shouldCollectConsent) {
            // Show banner to collect consent
        } else {
            // Apply consent with status.consents
        }
    } catch (error) {
        // Handle non-localized error
    }
    
    import { Usercentrics } from '@usercentrics/react-native-sdk';
    
    try {
        const status = await Usercentrics.status();
        if (status.shouldCollectConsent) {
            // Show banner to collect consent
        } else {
            // Apply consent with status.consents
        }
    } catch(error) {
        // Handle error
    }
    

    Wait for isReady

    It is required that you wait until isReady is called to use any SDK methods. Not doing so could lead to a crash, as methods called when the SDK has not finished initializing will return an exception.

  3. 🚀 You are now ready to collect and apply consent.