Informacia
Treść

Obsługa trybu zgody Google w wersji 2 dla pakietów SDK inApp

Szukasz CMP obsługującego tryb zgody Google? Zobacz nasze Tryb zgody Google, wersja 2 strona produktu.

Ten przewodnik zawiera instrukcje dotyczące integracji trybu zgody Google z trybem niestandardowym ConsentManager w aplikacji na Androida lub iOS.

Wymagania wstępne

  • Upewnij się, że tryb zgody jest włączony (Menu > CMP > Integracje > Tryb zgody Google)
  • Upewnij się, że Google Analytics, Google Ads lub inne usługi Google znajdują się na Twojej liście dostawców
  • Projekt Firebase z włączoną obsługą Google Analytics.
  • Pakiet SDK Firebase zintegrowany z Twoim projektem na iOS.
  • CMPManager skonfigurowane w Twoim projekcie.

Zakładka Podgląd

Poniżej znajdziesz metody pomocnicze, które pozwolą Ci pobrać i zarządzać statusem zgody Google przy użyciu naszego pakietu CMP SDK. 

iOS

/// Synchronizes the consent status from CMP to Firebase Analytics
func syncConsentToFirebase() {
    let cmpManager = CMPManager.shared
    guard let googleConsentModeStatus = cmpManager.getGoogleConsentModeStatus() else {
        print("Google Consent Mode status not available")
        return
    }

    // Define all expected consent types
    let consentTypes = [
        "analytics_storage",
        "ad_storage",
        "ad_user_data",
        "ad_personalization"
    ]

    // Build Firebase settings with proper defaults
    var firebaseConsentSettings = [String: String]()

    // Set defaults for all expected types (denied)
    for consentType in consentTypes {
      firebaseConsentSettings[consentType] = "denied"
    }

    // Override with actual values from CMP
    for (key, value) in googleConsentModeStatus {
        if consentTypes.contains(key) {
            firebaseConsentSettings[key] = value
        }
    }

    // Set the consent in Firebase
    Analytics.setConsent(firebaseConsentSettings)
    print("Firebase consent settings updated: \(firebaseConsentSettings)")
}

Android

/// Synchronizes the consent status from CMP to Firebase Analytics
fun updateFirebaseConsent(firebaseAnalytics: FirebaseAnalytics, cmpManager: CMPManager) {
    // Get consent settings from CMP SDK
    val cmpConsentSettings = cmpManager.getGoogleConsentModeStatus()
    
    // Convert to Firebase's types
    val firebaseConsentSettings = mutableMapOf<ConsentType, ConsentStatus>()
    
    cmpConsentSettings.forEach { (key, value) ->
        try {
            val consentType = ConsentType.valueOf(key.uppercase())
            val consentStatus = if (value == "granted") 
                ConsentStatus.GRANTED else ConsentStatus.DENIED
            
            firebaseConsentSettings[consentType] = consentStatus
        } catch (e: IllegalArgumentException) {
            Log.w("ConsentManager", "Unknown consent type: $key")
        }
    }
    
    // Update Firebase consent
    firebaseAnalytics.setConsent(firebaseConsentSettings)
    Log.d("ConsentManager", "Updated Firebase consent: $firebaseConsentSettings")
}

 

Powrót do góry