Info
Content

Google Consent Mode v2 Support for inApp SDKs

Looking for a CMP that supports Google Consent Mode? See our Google Consent Mode v2 product page.

Since Version 2.1.0 in Android and Version 1.99.3 in iOS

This guide provides instructions on how to integrate Google Consent Mode with the custom ConsentManager in your Android or iOS application. It assumes that you have Firebase Analytics already set up in your project.

Prerequisites

  1. Ensure Consent Mode is enabled (Menu > CMPs > Integrations > Google Consent Mode)
  2. Ensure Google Analytics, Google Ads or the other Google services are in your vendorlist

iOS

Prerequisites

  • A Firebase project with Google Analytics enabled.
  • Firebase SDK integrated into your iOS project.
  • CMPConsentTool set up in your project.

Step 1: Configure CMPConsentTool

Set up CMPConsentTool with your specific configuration. This tool will manage user consent interactions:

import CmpSdk

var cmpManager: CMPConsentTool?

func configureCMPConsentTool() {
let cmpConfig = CMPConfig(...) // Configure as per your requirements

cmpManager = CMPConsentTool(cmpConfig: cmpConfig)
.withUpdateGoogleConsent(onCmpUpdateGoogleConsent)
}

Step 2: Handle Consent Updates

Implement the callback function to handle updates to the consent status. This function will be triggered when there is a change in the user's consent.

    func onCmpUpdateGoogleConsent(consentMap: [String: String]?) -> Void {
        guard let consentMap = consentMap else { return }

        var firebaseConsentMap: [ConsentType: ConsentStatus] = [:]

        consentMap.forEach { key, value in
            if let consentType = convertToFirebaseConsentType(from: key),
               let consentStatus = convertToFirebaseConsentStatus(from: value) {
                firebaseConsentMap[consentType] = consentStatus
            }
        }

        Analytics.setConsent(firebaseConsentMap)
    }
    
    func convertToFirebaseConsentType(from key: String) -> ConsentType? {
        switch key {
        case "analyticsStorage":
            return .analyticsStorage
        case "adStorage":
            return .adStorage
        case "adUserData":
            return .adUserData
        case "adPersonalization":
            return .adPersonalization
        default:
            return nil
        }
    }

    func convertToFirebaseConsentStatus(from value: String) -> ConsentStatus? {
        switch value {
        case "granted":
            return .granted
        case "denied":
            return .denied
        default:
            return nil
        }
    }

The onCmpUpdateGoogleConsent function updates the consent in Google Analytics using the Firebase SDK.

  • The function translates the user's consent from the CMPConsentTool into a format that Firebase Analytics can understand.
  • It then updates Google Analytics with the user's current consent status.

Android

Prerequisites

  • Android application with Firebase Analytics integrated.
  • CmpManager class implemented in your application.

Step 1: Setup Firebase Analytics

If you haven't already, add Firebase Analytics to your Android project. Follow the official Firebase documentation to set it up.

Step 2: Implement the Google Firebase Analytics Callback

// add the AnalyticsInterface
class ConsentActivity() : FragmentActivity(), CmpGoogleAnalyticsInterface {
  
// Set the Callback
cmpManager.setGoogleAnalyticsCallback(this)

// Define Callback
override fun updateGoogleConsent(consentMap: Map<ConsentType, ConsentStatus>) {
	val firebaseConsentMap = consentMap.entries.associate { entry ->
		val firebaseConsentType = when (entry.key) {
			ConsentType.ANALYTICS_STORAGE -> FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE
			ConsentType.AD_STORAGE -> FirebaseAnalytics.ConsentType.AD_STORAGE
			ConsentType.AD_USER_DATA -> FirebaseAnalytics.ConsentType.AD_USER_DATA
			ConsentType.AD_PERSONALIZATION -> FirebaseAnalytics.ConsentType.AD_PERSONALIZATION
		}
	
		val firebaseConsentStatus = when (entry.value) {
			ConsentStatus.GRANTED -> FirebaseAnalytics.ConsentStatus.GRANTED
			ConsentStatus.DENIED -> FirebaseAnalytics.ConsentStatus.DENIED
		}
		
		firebaseConsentType to firebaseConsentStatus
	}
	
	FirebaseAnalytics.getInstance(applicationContext).setConsent(firebaseConsentMap)
}

 

Back to top