When Consent Changes
Overview
When a user updates their consent choices or interacts with the SDK UI, the application needs to become aware so it can control the next set of actions like:
- Allowing or Restricting processing of 3rd party SDKs based on their categorization
- Passing latest User consent values to 3rd Party SDKs
- Passing the IAB's encoded TC String to 3rd party SDKs
- Passing the IAB's USP String to 3rd Party SDKs
- Routing the User to another location in the App
- Displaying messages to the User of their changes
- Logging updates to internally owned APIs
- etc...
Notification Object
The notification object contains an integer that indicates the consent of the user.
- 1 = Consent is given (end user interacts and gives consent)
- 0 = Consent is not given (end user interacts and does not give consent)
- -1 = Consent has not been collected (SDK is not yet initialized)
Consent Storage
OneTrust stores SDK data and user consent in local storage. The methods below can be used to retrieve consent.
Querying Consent Status for a Category
Query the current consent status for any of the categories included in your application. This can be used to determine what privacy action is needed at app launch or anytime the consent status is needed. Simply pass in the Category ID (eg. C0004) and the method will return the current consent status.
Here are the default values as set by OneTrust:
Category Name | Category ID |
---|---|
Strictly Necessary | C0001 |
Performance | C0002 |
Functional | C0003 |
Targeting | C0004 |
Social Media | C0005 |
This will give you the consent data from all the categories at once instead of a single or specific category.
OneTrust.getPurposeConsentStatus();
You can query using this parameter to get the consent data from a specific category using a group ID.
OneTrust.getPurposeConsentStatus("C0004");
You can query using this parameter to get the consent data from multiple categories using multiple group IDs.
OneTrust.getPurposeConsentStatus(["C0004","C0002"]);
Returns
- 1 = Consent is given (end user interacts and gives consent)
- 0 = Consent is not given (end user interacts and does not give consent)
- -1 = Consent has not been collected (SDK is not yet initialized)
Events
When a user interacts with the OT SDK UI, it emits events you can watch for. OneTrust supports two events today, Checkbox Events and Button Events.
OneTrust UIs must be surfaced/active before you call the event methods or you'll receive an error.
To ensure the OneTrust UI is active, you can use this event: oneTrust:UiDialogStatusChange
window.addEventListener('oneTrust:UiDialogStatusChange', function(e){
console.log(e.detail);
});
Returns:
- Status
boolean
type. Returns true if the UI is active. Returns false if UI is inactive.
- Message
Checkbox Events
This method allows you to watch for the consent state of each category as the user toggles it on/off.
window.addEventListener('oneTrust:UiDialogStatusChange', function (e) {
if (e.detail.status) {
OneTrust.broadcastCheckboxEvents((details) => {
console.log(
`Checkbox with ID ${details.checkboxId} is ${details.checked ? 'checked' : 'unchecked'}`);
},
'C0004' //replace the ID with the category ID you're watching
);
}
});
Button Events
This method allows you to watch for which CTA was selected by the user to dismiss the UI.
window.addEventListener('oneTrust:UiDialogStatusChange', function(e){
if(e.detail.status){
OneTrust.broadcastButtonEvents(function(details){
console.log(`Button with ID ${details.buttonId} is clicked`);
});
}
});
Recommended Approach for Consent Actioning
OneTrust recommends you use the button events to determine when a UI has been dismissed and query for consent to determine your next set of actions.
window.addEventListener('oneTrust:UiDialogStatusChange', function(e){
if(!e.detail.status){
console.log(`Consent Status: ${JSON.stringify(OneTrust.getPurposeConsentStatus())}`)
}
});
//(OPTIONAL) To return consents before AND after
window.addEventListener('oneTrust:UiDialogStatusChange', function(e){
if(e.detail.status){
OneTrust.broadcastButtonEvents(function(details){
console.log(`before: ${JSON.stringify(OneTrust.getPurposeConsentStatus())}`)
});
}
if(!e.detail.status){
console.log(`after: ${JSON.stringify(OneTrust.getPurposeConsentStatus())}`)
}
});
Updated 4 days ago