[Android] 2. API Documentation
acceptAll
Accepts the consent layer and behaves the same when the user accepts the consent. This method consumes one pageview.
Parameters
Name | Type | Description |
onConsentReceivedCallback |
OnConsentReceivedCallback? | Callback for handling consent received event. |
Return
No return value.
Example
func acceptAll(onFinish: @escaping () -> Void) {
cmpManager?.acceptAll(onFinish: onFinish)
}
check
Checks if consent is required from the user, optionally returning cached results. This method consumes one pageview if isCached() parameter is TRUE and cached consent IS expired, one page view if isCached() parameters is FALSE, and no pageviews if the cached consent is not expired. The cached consent is expired and purged every 24 hours.
Parameters
Name | Type | Description |
onCheckIsConsentRequiredCallback |
OnCheckIsConsentRequired | fun isConsentRequired(isConsentRequired: Boolean) callback to determine if a consent is required or not |
isCached |
Boolean = false | if true the result is cached for one day |
Return
None.
Example
cmpManager.acceptAll {
coroutineScope.launch {
snackbarHostState.showSnackbar(
message = "All consents accepted",
duration = SnackbarDuration.Short
)
consentStatus.value = showConsentStatus(cmpManager)
}
}
checkAndOpenConsentLayer
Checks if user's consent is required and, if so, opens the consent layer UI. This method consumes one pageview.
Parameters
Name | Type | Description |
context |
Context | Application Context |
appInterface |
CmpLayerAppEventListenerInterface? = null |
Return
No return value
Example
context.getActivity()?.let { activity ->
cmpManager.checkAndOpenConsentLayer(activity)
}
calledThisDay
Checks if the consent layer was requested today. This method consumes no pageviews.
Parameters
None. |
Return
TRUE if the user's consent was already requested today.
FALSE otherwise.
Example
fun consentRequestedTodayAction() {
val calledToday = cmpManager?.calledThisDay()
if (calledToday != null) {
println("Consent requested today: $calledToday")
} else {
println("Failed to retrieve consent request status for today.")
}
}
disablePurposeList
Disables a list of purposes and updates the given consent. This method consumes one pageview.
Parameters
Name | Type | Description |
purposes |
List | List of purposes ids to disable |
updateVendor |
Boolean | flag if true also disable corresponding vendors |
onConsentReceivedCallback |
OnConsentReceivedCallback? |
Return
No return value
Example
fun onFinish() {
println("Notification: New consent received")
}
val purposes = listOf("1", "2")
// Option with callback
cmpManager?.disablePurposeList(purposes, ::onFinish)
// Option without callback
cmpManager?.disablePurposeList(purposes)
disableVendorList
Disables a list of vendors and updates the given consent. This method consumes one pageview.
Parameters
Name | Type | Description |
vendors |
List | List of vendor ids to enable. |
onConsentReceivedCallback |
OnConsentReceivedCallback? | Callback for handling consent received event. it will be called when consent is received and processed |
Return
No return value.
Example
fun onFinish() {
println("Notification: New consent received")
}
val vendors = listOf("1", "2")
// Option with callback
cmpManager?.disableVendorList(vendors, ::onFinish)
// Option without callback
cmpManager?.disableVendorList(vendors)
enableVendorList
Enables a list of purposes and updates the given consent. This method consumes one pageview.
Parameters
Name | Type | Description |
vendors |
List | List of purpose ids to enable. |
onConsentReceivedCallback |
function (optional) | Callback for handling consent received event. It will be called when consent is received and processed |
Return
No return value.
Example
fun onFinish() {
println("Notification: New consent received")
}
val vendors = listOf("1", "2")
// Option with callback
cmpManager?.enableVendorList(vendors, ::onFinish)
// Option without callback
cmpManager?.enableVendorList(vendors)
enablePurposeList
Enables a list of vendors and updates the given consent. This method consumes one pageview.
Parameters
Name | Type | Description |
purposes |
List | List of purpose ids to enable. |
updateVendor |
Boolean | flag if true also enable corresponding vendors |
onConsentReceivedCallback |
Function (optional) |
Return
No return value.
Example
fun onFinish() {
println("Notification: New consent received")
}
val purposes = listOf("1", "2")
// Option with callback
cmpManager?.enablePurposeList(purposes, ::onFinish)
// Option without callback
cmpManager?.enablePurposeList(purposes)
exportCmpString
Exports the current CMP string representing the user's consent preferences. This method consumes no pageviews.
Parameters
None. |
Return
The CMP consent string.
Example
fun exportConsentStringAction() {
val exportString = cmpManager?.exportCmpString()
println(exportString ?: "No string to export.")
}
getAllPurposes
Returns all purposes' IDs. This method consumes no pageviews.
Parameters
None. |
Return
A comma-separated string containing all purpose IDs.
Example
fun getAllPurposesAction() {
val allPurposes = cmpManager?.getAllPurposes()
if (allPurposes != null) {
println("All purposes: $allPurposes")
// Additional actions for getAllPurposes function can be added here
} else {
println("Failed to retrieve all purposes.")
}
}
getAllPurposeList
Returns a list of all purpose IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
List<String>
Example
fun getAllPurposeListAction() {
// Action for getAllPurposeList function
val allPurposes = cmpManager?.getAllPurposesList()
if (allPurposes != null) {
println("All purposes list: ${allPurposes.joinToString(", ")}")
} else {
println("Failed to retrieve all purposes list.")
}
}
getAllVendors
Returns all vendors' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A comma-separated string containing all vendors IDs.
Example
fun getAllVendorsAction() {
// Action for getAllVendors function
val allVendors = cmpManager?.getAllVendors()
if (allVendors != null) {
println("Printing all Vendors: $allVendors")
} else {
println("Failed to retrieve all vendors.")
}
}
getAllVendorsList
Returns a list of all vendors' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A List<String> containing all vendor IDs.
Example
fun getAllVendorsListAction() {
val allVendors = cmpManager?.getAllVendorsList()
if (allVendors != null) {
println("All vendors list: ${allVendors.joinToString(", ")}")
} else {
println("Failed to retrieve all vendors list.")
}
}
getConsentstring
Returns a string with the last given consent. This method consumes no pageviews.
Parameters
None. |
Return
A string with the last consent acknowledged by the user.
Example
fun getAllVendorsListAction() {
val allVendors = cmpManager?.getAllVendorsList()
if (allVendors != null) {
println("All vendors list: ${allVendors.joinToString(", ")}")
} else {
println("Failed to retrieve all vendors list.")
}
}
getDisabledPurposes
Returns all disabled purposes' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A comma-separated string containing all disabled purposes' IDs.
Example
fun getDisabledPurposesAction() {
val disabledPurposes = cmpManager?.getDisabledPurposes()
if (disabledPurposes != null) {
println("Disabled purposes: $disabledPurposes")
} else {
println("Failed to retrieve disabled purposes.")
}
}
getDisabledVendors
Returns all disabled vendors' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A comma-separated string containing all disabled vendors' IDs.
Example
fun getDisabledVendorsAction() {
val disabledVendors = cmpManager?.getDisabledVendors()
if (disabledVendors != null) {
println("Disabled vendors: $disabledVendors")
} else {
println("Failed to retrieve disabled vendors.")
}
}
getEnabledPurposes
Returns all enabled vendors' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A comma-separated string containing all enabled vendors' IDs.
Example
fun getEnabledPurposesAction() {
val enabledPurposes = cmpManager?.getEnabledPurposes()
if (enabledPurposes != null) {
println("Disabled purposes: $enabledPurposes")
} else {
println("Failed to retrieve enabled purposes.")
}
}
getEnabledPurposeList
Returns a list all enabled purposes' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A List<String> containing all enabled purposes' IDs.
Example
fun getEnabledPurposesAction() {
val enabledPurposes = cmpManager?.getEnabledPurposes()
if (enabledPurposes != null) {
println("Enabled purposes: ${enabledPurposes.joinToString(", ")}")
} else {
println("Failed to retrieve enabled purposes list.")
}
}
getEnabledVendors
Returns all enabled vendors' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A comma-separated string containing all enabled vendors' IDs.
Example
fun getEnabledVendorsAction() {
val enabledVendors = cmpManager?.getEnabledVendors()
if (enabledVendors != null) {
println("Enabled vendors: $enabledVendors")
} else {
println("Failed to retrieve enabled vendors.")
}
}
getEnabledVendorList
Returns a list with all enabled vendors' IDs for a given CMP. This method consumes no pageviews.
Parameters
None. |
Return
A List<String> containing all enabled vendors' IDs.
Example
fun getEnabledVendorsAction() {
val enabledVendors = cmpManager?.getEnabledVendors()
if (enabledVendors != null) {
println("Enabled vendors: ${enabledVendors.joinToString(", ")}")
} else {
println("Failed to retrieve enabled vendors.")
}
}
getGoogleACString
Retrieves the Google additional consent ID (`addtlConsent`) string that was set by the consent manager.
For further information, check the Google Additional Consent technical specification.
This method consumes no pageviews.
Parameters
None. |
Return
A List<String> containing all enabled vendors' IDs.
Example
fun getGoogleConsentStringAction() {
val googleACString = cmpManager?.getGoogleACString()
if (googleACString != null) {
println("Google Additional Consent String: $googleACString")
} else {
println("Failed to retrieve Google Additional Consent String.")
}
}
getUSPrivacyString
Retrieves the US Privacy String. This U.S. privacy string is a cookie that stores information about disclosures made and choices selected by the website visitor regarding their consumer rights. This method consumes no pageviews.
Parameters
None. |
Return
A String with the US Privacy data set by consentmanager.
Example
fun getUSPrivacyStringAction() {
val usPrivacyString = cmpManager?.getUSPrivacyString()
if (usPrivacyString != null) {
println("US Privacy string contents: $usPrivacyString")
} else {
println("Failed to retrieve US Privacy string.")
}
}
hasConsent
Checks if the user already provided his consent. The consent layer can have the option to just close the layer. In that case, the user did not provided a consent. This method consumes no pageviews.
Parameters
None. |
Return
A String with the US Privacy data set by consentmanager.
Example
fun getHasConsentAction() {
val hasConsent = cmpManager?.hasConsent()
if (hasConsent != null) {
val consentStatus = if (hasConsent) "Yes" else "No"
println("Consent provided by the user? $consentStatus")
} else {
println("Failed to retrieve user consent.")
}
}
hasPurpose (from version 2.5.1 on)
hasPurposeConsent (deprecated)
Checks if the purpose ID is enabled based on the user consent. This method consumes no pageviews.
Parameters
Name | Type | Description |
purposeId |
String | purpose id |
defaultReturn |
Boolean | Value that is returned by default by the method, in case there is no consent yet. See warning below. |
Return
TRUE if the user has given consent to the specified purpose.
FALSE otherwise.
Note that if, for any reason related to poor or no internet connection on the device or in the case of absence of a consent, the value that was set via the defaultReturn
parameter will be returned.
The defaultReturn
parameter do not exist in versions older than 2.5.1.
Example
fun getHasPurposeConsentAction() {
val purposeId = "purposeID" // Replace with actual purpose ID
val hasPurposeConsent = cmpManager?.hasPurpose(purposeId, false)
if (hasPurposeConsent != null) {
val consentStatus = if (hasPurposeConsent) "Yes" else "No"
println("Purpose has consent provided by the user? $consentStatus")
} else {
println("Failed to retrieve user purpose consent.")
}
}
hasVendor (from version 2.5.1 on)
hasVendorConsent (deprecated)
Checks if the vendor ID is enabled based on the user consent. This method consumes no pageviews.
Parameters
Name | Type | Description |
vendorId |
String | vendor id |
defaultReturn |
Boolean | Value that is returned by default by the method, in case there is no consent yet. |
Return
TRUE if the user has given consent to the specified vendor.
FALSE otherwise.
Note that if, for any reason related to poor or no internet connection on the device, the consent for the purpose could not be retrieved, the value that was set via the defaultReturn
parameter will be returned.
The defaultReturn
parameter do not exist in versions older than 2.5.1.
Example
fun getHasVendorConsentAction() {
val vendorId = "vendorID" // Replace with actual vendor ID
val hasVendorConsent = cmpManager?.hasVendor(vendorId, false)
if (hasVendorConsent != null) {
val consentStatus = if (hasVendorConsent) "Yes" else "No"
println("Vendor has consent provided by the user? $consentStatus")
} else {
println("Failed to retrieve user vendor consent.")
}
}
importCmpString
Imports a CMP string representing the user's consent preferences. The string must be informed AS-IS, when retrieved via the exportCmpString() method. This method consumes one pageview.
Parameters
Name | Type | Description |
consentString | String | String representing the consent |
completionHandler | function (optional) | Callback completion handler to the method. |
Return
None.
Example
val cmpString = "YourConsentStringHere"
typealias CmpImportCompletionHandler = (Error?) -> Unit
// Define the completion handler function
val completionHandler: CmpImportCompletionHandler = { error ->
if (error != null) {
println("Failed to import CMP string: ${error.message}")
} else {
println("CMP string imported successfully")
}
}
cmpManager?.importCmpString(cmpString, completion = completionHandler)
initialize
Initialize function will initialize the consent layer and checks automatically if the user needs a consent or not. This function will eventually open the consent layer and Will interact wit the Apple Tracking Transparency API to ask the user for Consent. This method consumes one pageview.
Parameters
Name | Type | Description |
context |
Context | App Context |
cmpConfig |
CmpConfig | The CmpConfig object is passed with the info regarding the appId, Domain, appName and language (optional) |
Return
The CMPManager object.
Example
private fun initializeCmpManager() {
CmpConfig.apply {
id = cmpId
domain = CMP_DOMAIN
appName = cmpAppName
language = cmpLanguage
domainWhitelist = cmpDomainWhitelist
timeout = 2000
isDebugMode = true
}
CmpLog.setLogLevel(Log.VERBOSE)
cmpManager = CmpManager.createInstance(this, CmpConfig)
}
openConsentLayer
Forcefully opens the CMP consent layer webview. This method consumes one pageview.
Parameters
Name | Type | Description |
context |
Context | App context. |
Return
No return value.
Example
ActionButtonsRow("Open", "Check", "Open?") { action ->
when (action) {
"Open" -> context.getActivity()?.let {
cmpManager.openConsentLayer(it.applicationContext)
}
.
.
.
}
needsAcceptance
Checks if consent needs to be provided by the current app user. This method consumes no pageviews.
Parameters
None. |
Return
TRUE if the user needs to give a consent.
FALSE otherwise.
fun showConsentStatus(cmpManager: CmpManager?): String {
return buildString {
append("Cmp ID: ${CmpConfig.id}\n")
append("Need Acceptance: ${cmpManager?.needsAcceptance()}\n") // <=========
append("ConsentString: ${cmpManager?.getConsentString()}\n")
append("Has Consent: ${cmpManager?.hasConsent()}\n")
}
}
rejectAll
Rejects the consent layer and behaves the same when the user did not accept the consent. This method consumes one pageview.
Parameters
Name | Type | Description |
onFinish |
function | Optional. Callback for handling consent received event. it will be called when consent is received and processed. |
Return
No return value.
Example
// Define the onFinish function
fun onFinish() {
println("Notification: New consent has been received with a Reject All response.")
}
cmpManager?.rejectAll(::onFinish)
setCallbacks
Configures the callbacks available.
Parameters
Name | Type | Description |
openListener |
OnOpenCallback |
The callback to be invoked when the consent layer is opened. |
closeListener |
OnCloseCallback |
The callback to be invoked when the consent layer is closed. |
cmpNotOpenedCallback |
OnCMPNotOpenedCallback |
The callback to be invoked when the consent layer could not be opened. |
onErrorCallback |
OnErrorCallback |
The callback to be invoked when an error. |
onButtonClickedCallback |
OnButtonClickedCallback |
The callback to be invoked to get information about which button was pressed to close the consent layer. |
Return
No return value
Example
val existingInstance = instance?.get()
existingInstance.setCallbacks(
openListener = openListener,
closeListener = closeListener,
cmpNotOpenedCallback = cmpNotOpenedCallback,
onErrorCallback = errorCallback,
onButtonClickedCallback = cmpButtonClickedCallback
)
withGoogleAnalyticsCallback
Set callback for retrieving Google Consentmode values.
Parameters
Name | Type | Description |
analyticsListener |
CmpGoogleAnalyticsInterface | The interface implementation to receive updates about Google Analytics consent mode. |
Return
The current instance of CmpManagerInterface for chaining.
Example
private fun initializeCmpManager() {
cmpManager = CmpManager.createInstance(this, CmpConfig).apply {
withOpenListener(this@ConsentActivity)
withCloseListener(this@ConsentActivity)
withNotOpenedCallback(this@ConsentActivity)
withErrorCallback(this@ConsentActivity)
withButtonClickedCallback(this@ConsentActivity)
withOnClickLinkCallback(this@ConsentActivity)
withGoogleAnalyticsCallback(this@ConsentActivity) // <=============
initialize(this@ConsentActivity)
}
}