Documentation Index

Fetch the complete documentation index at: https://usercentrics.document360.io/llms.txt

Use this file to discover all available pages before exploring further.

Location & Webview consent sharing

Prev Next

Location consent sharing

Consent choice persistence means that you preserve consent preferences provided in one location, and apply them when the user moves to another region where they have previously given consent.

This means that once users consent to data collection in one city or country, they won't be prompted to provide consent again if they travel to another area covered by their initial consent.

This feature enhances user convenience by eliminating repetitive consent requests, and also ensures that users' preferences are consistently respected, regardless their location.

Implementation

To preserve preferences that users provide in one location, and apply them when they are applicable, set up geolocation rulesets in Usercentrics’ Admin Interface, on the Geolocation Rulesets page. For detailed instructions, read the documentation of geolocation rulesets.

Next step

After setting up geolocation rulesets, make sure you are following the guidelines on how to display the banner.

Webview consent continuity

If your app uses WebViews, you can inject a user session so that the CMP on the WebView is not shown every time the user opens it.

To implement this feature, use the method getUserSessionData to return a String (JSON) with the user session.

The user session data must be injected using a JavaScriptInterface with a method getSessionData.

webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true

webView.addJavascriptInterface(SampleJavascriptInterface(userSessionData), "ucMobileSdk")
webView.loadUrl("https://<some_url>")
class SampleJavascriptInterface(private val userSessionData: UserSessionData) {

    @JavascriptInterface
    fun getUserSessionData(): String? {
        return userSessionData
    }
}

Inject the user session data into a global variable called UC_UI_USER_SESSION_DATA. Due to webview_flutter library limitations, you have to guarantee that the data is available at the time the Usercentrics script is executed in the WebView.

FutureBuilder<String>(
  future: Usercentrics.userSessionData,
  builder: (context, snapshot) {
    final userSessionData = snapshot.data;
    if (userSessionData == null) return const SizedBox();
    return WebView(
      onWebViewCreated: (WebViewController controller) async {
        this.controller = controller;
        await controller.loadFlutterAsset('assets/webview_index.html'); // loadUrl or whatever
      },
      onPageFinished: (String url) async {
        await controller?.runJavascript("""
        window.UC_UI_USER_SESSION_DATA = $userSessionData;
        window.dispatchEvent(new Event('Usercentrics_userSessionData_injected'));
        """);
      },
      javascriptMode: JavascriptMode.unrestricted,
      debuggingEnabled: true,
    );
  }),
)
function addUsercentricsScript() {
    var settingsId = 'Yi9N3aXia';
    var script = document.createElement('script');
    script.id = 'usercentrics-cmp';
    script.setAttribute('data-settings-id', settingsId);
    script.setAttribute('src', 'https://app.usercentrics.eu/browser-ui/latest/bundle_legacy.js');
    script.async = true;
    document.head.appendChild(script);
}
window.addEventListener("Usercentrics_userSessionData_injected", function() {
    addUsercentricsScript();
});

See more in this Flutter sample.

Inject the user session data into a global variable called UC_UI_USER_SESSION_DATA in your WebView injectedJavaScriptBeforeContentLoaded property.

<WebView
    javaScriptEnabled={true}
    domStorageEnabled={true}
    injectedJavaScriptBeforeContentLoaded={`window.UC_UI_USER_SESSION_DATA = ${userSessionData};`.toString()}
    source=... />

See more in this React Native sample.

The user session data must then be injected into a global variable called UC_UI_USER_SESSION_DATA in your WKWebView.

let sessionData = UsercentricsCore.shared.getUserSessionData()

let script = """
window.ucMobileSdk = {
    getUserSessionData: function() {
        return '\(sessionData)';
    }
}
"""

let userScript = WKUserScript(source: script, injectionTime: .atDocumentStart, forMainFrameOnly: true)
let contentController = WKUserContentController()
contentController.addUserScript(userScript)

let preferences = WKPreferences()
preferences.javaScriptEnabled = true

let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences = preferences
webConfiguration.userContentController = contentController

webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self

let myURL = URL(string:"https://<some_url>")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)

Multiple SettingsIDs not supported

For seamless integration and support, it is imperative that the SDK utilized within your application employs the same SettingsID as the one implemented within the WebView. The utilization of divergent SettingsIDs between the SDK and WebView are not supported by the SDK.

WebView banner and Native SDK

When displaying a WebView and adhering to the provided instructions, it's important to note that any changes to consent made by the user within the Web banner will not automatically be reflected in the Native SDK. This means that if a user alters their consent settings, these changes won't be communicated back to the native environment of your application.

To ensure that your application's functionality remains unaffected by these potential discrepancies, it's crucial to implement a mechanism that re-synchronizes consent statuses whenever the WebView is dismissed. This proactive approach will help maintain consistency across your application's consent management processes.

Compatibility

User session injection is only supported if your WebView is running version 1.4.0 or higher of the Usercentrics Browser SDK.