Info
Contenido

[Unidad] 1. consentmanager Integración SDK

Caracteristicas

  • Admite plataformas iOS y Android.
  • Proporciona un puente entre Unity y las funcionalidades CMP específicas de la plataforma nativa.
  • Le permite inicializar, administrar el consentimiento del usuario y manejar datos relacionados con la privacidad.

Cómo Empezar

Instalación

    • Descargue la última versión del complemento.
    • Importe el paquete a su proyecto de Unity usando Activos > Importar paquete > Paquete personalizado.

Uso

Siga estos pasos para comenzar a utilizar el complemento.

Inicialización: Para utilizar la funcionalidad CMP, inicialice la instancia de CMPManager.

CmpManager.Instance.Initialize(domain, codeId, appName, language);                

domain utilice el dominio-servidor que se encuentra en su consentmanager cuenta bajo Menú > CMP > Obtener códigos para aplicaciones. For codeID utilice el Código de identificación que se encuentra en la misma página de su cuenta de consentmanger. Los campos appName y language usted puede configurarlo.

CmpManager.Instance.OpenConsentLayer();

Verificar consentimiento: Comprobar si el usuario ha dado su consentimiento:

bool hasConsent = CmpManager.Instance.HasConsent();

Devoluciones de llamada: configure oyentes de devolución de llamadas para varios eventos:

CmpManager.Instance.AddEventListeners(OnOpen, OnClose, OnNotOpened, OnCmpButtonClicked, OnError);

Verificaciones de propósito y proveedores: Verifique el consentimiento relacionado con propósitos y proveedores específicos:

bool hasPurpose = CmpManager.Instance.HasPurpose(id);
bool hasVendor = CmpManager.Instance.HasVendor(id);

Exportación de datos: Exportar datos CMP:

string cmpString = CmpManager.Instance.ExportCmpString();

Configurar JSON

En CmpSdkConfig.json puede configurar la versión nativa del SDK para iOS y Android que se utilizará para el proceso de compilación: 

Encuentre las versiones nativas del SDK compatibles esta página

{
  "displayName": "Consentmanager SDK",
  "name": "CmpSdk",
  "androidBasePath": "net.consentmanager.sdk",
  "version": "1.0.0",
  "androidLibraryVersion": "x.xx.x",
  "iosLibraryVersion": "x.xx.x",
  "description": "Unity plugin helps you to use native Consentmanager functionality on Android and iOS."
}

Configuración de compilación

Para cambiar la configuración de compilación, vaya a Window -> CmpSdk 

pluginwindow-unity.png

Configuración de compilación de iOS

  • Habilite el script de compilación de iOS: Cambie esto para habilitar o deshabilitar el script de compilación responsable de integrar el SDK de iOS en el proyecto de Unity.
  • Ruta xcFramework: Especifique la ruta al directorio xcFramework. Esta ruta se puede editar directamente o navegar usando el botón adjunto.
  • Incluir etiqueta de versión: Cuando está habilitado, agrega la versión del SDK de iOS a la ruta xcFramework, lo que permite configuraciones específicas de la versión.
  • Ruta del marco resultante: Muestra la ruta completamente resuelta a xcFramework, incluida la versión del SDK si se incluye la etiqueta de versión.
  • Habilite la transparencia del seguimiento de aplicaciones: Cambie esto para habilitar la función Transparencia de seguimiento de aplicaciones para iOS, que es necesaria para obtener el consentimiento del usuario según las pautas de privacidad de iOS.
  • Mensaje de consentimiento de transparencia de seguimiento de aplicaciones: Un campo de texto para ingresar el mensaje personalizado que se muestra a los usuarios cuando solicitan consentimiento para el seguimiento. Este mensaje debe ser claro y conciso, explicando por qué se necesita el consentimiento.

Configuración de compilación de Android

  • Habilite el script de compilación de Android: Cambie para habilitar o deshabilitar el script de compilación para integrar el SDK de Android en el proyecto de Unity. 
  • Integrar diseño personalizado: Cuando está habilitado, esto permite el uso de un diseño personalizado para la capa de consentimiento para el fragmento UIView.

    Si está interesado en utilizar un diseño personalizado con fragmentos, asegúrese de que su proyecto Unity agregue la dependencia appcompat. Agregar una plantilla principal personalizada
    Activos/Complementos/Android/mainTemplate.gradle
    y agregue la dependencia:
    implementación 'androidx.appcompat:appcompat:1.xx'

Oyente de eventos

Evento de devolución de llamada Descripción Parámetros pasados
Onopen Se activa cuando se abre la herramienta de consentimiento de CMP. Ninguna
Onclose Se activa cuando se cierra la herramienta de consentimiento de CMP. Ninguna
EnNoAbierto Se activa si la herramienta de consentimiento de CMP no se abre. Ninguna
En el botón Cmp se hizo clic Se activa cuando se hace clic en un botón dentro de la herramienta de consentimiento de CMP. CmpButtonEvent buttonEvent
OnError Se activa cuando ocurre un error dentro de la herramienta de consentimiento de CMP. CmpErrorType errorType, string message
OnGoogleConsentActualizado Se activa cuando se actualiza el estado del modo de consentimiento de Google. CmpGoogleConsentModeStatus status
OnCmpATTrackingStatusCambió (solo iOS) Se activa cuando cambia el estado de Transparencia de seguimiento de aplicaciones. ATTrackingManagerAuthorizationStatus oldStatus, ATTrackingManagerAuthorizationStatus newStatus, double lastUpdated

 

Diseño personalizado

Unity admite diferentes diseños personalizados: 

        public enum ScreenConfig
        {
            FullScreen,
            HalfScreenBottom,
            HalfScreenTop,
            CenterScreen,
            SmallCenterScreen,
            LargeTopScreen,
            LargeBottomScreen,
        }

Ejemplo de uso: 

            _cmpConfig = new CmpConfig(CodeId, Domain, AppName, Language)
            {
                Debug = true,
                Timeout = 8000
            };

                _cmpConfig.UIConfig.screenConfig = (CmpUIConfig.ScreenConfig) Enum.Parse(typeof(CmpUIConfig.ScreenConfig), s);  
                _cmpManager.SetUIConfig(_cmpConfig.UIConfig); 

Documentación de referencia: Comience con Google Firebase para Unity

Integración : Configuración de la unidad

Para utilizar el modo de consentimiento de Google, CMP Unity SDK admite una interfaz para configurar el estado de consentimiento de Google: 

// public class CmpSampleScript : MonoBehaviour, IOnCmpGoogleConsentUpdatedCallback { ... 
// Make sure to implement the Interface IOnCmpGoogleConsentUpdatedCallback
public void OnGoogleConsentUpdated(CmpGoogleConsentModeStatus status)
{
// Convert CmpGoogleConsentModeStatus to Firebase compatible dictionary
var firebaseConsentDict = new Dictionary<ConsentType, ConsentStatus>();

foreach (var consent in status.ConsentDictionary)
{
// Convert GoogleConsentType to Firebase ConsentType
var firebaseConsentType = ConvertToFirebaseConsentType(consent.Key);

// Convert GoogleConsentStatus to Firebase ConsentStatus
var firebaseConsentStatus = ConvertToFirebaseConsentStatus(consent.Value);

firebaseConsentDict[firebaseConsentType] = firebaseConsentStatus;
}

// Apply the consent settings to Firebase Analytics
FirebaseAnalytics.SetConsent(firebaseConsentDict);
AppendLog($"Google Consent Mode: {firebaseConsentDict}");
}

private static ConsentType ConvertToFirebaseConsentType(GoogleConsentType googleConsentType)
{
return googleConsentType switch
{
GoogleConsentType.AnalyticsStorage => ConsentType.AnalyticsStorage,
GoogleConsentType.AdStorage => ConsentType.AdStorage,
GoogleConsentType.AdUserData => ConsentType.AdUserData,
GoogleConsentType.AdPersonalization => ConsentType.AdPersonalization,
_ => throw new InvalidEnumArgumentException($"Unknown GoogleConsentType: {googleConsentType}")
};
}

private static ConsentStatus ConvertToFirebaseConsentStatus(GoogleConsentStatus googleConsentStatus)
{
return googleConsentStatus switch
{
GoogleConsentStatus.Granted => ConsentStatus.Granted,
GoogleConsentStatus.Denied => ConsentStatus.Denied,
_ => throw new InvalidEnumArgumentException($"Unknown GoogleConsentStatus: {googleConsentStatus}")
};
}

en este ejemplo, se llama a la devolución de llamada OnGoogleConsentUpdate cuando el usuario da su consentimiento. 

FirebaseAnalytics.SetConsent(firebaseConsentDict);

En esta línea, el estado de consentimiento se establece en Firebase Analytics. Las otras dos funciones asignan el estado de consentimiento de CMP a los tipos y estados de consentimiento de Google.

Hoja de ruta para funciones futuras

  • desactivarVendorList, habilitarVendorList
  • desactivarPurposeList, enablePurposeList

Compatibilidad

  • Unidad 20XX.XX o posterior
  • iOS (a través de DllImport)
  • Android (a través de JNI)

Soporte

Para informes de errores, solicitudes de funciones o consultas generales, por favor abrir un problema en el repositorio.

Créditos

Creado y mantenido por Skander Ben Abdelmalak.

Script de ejemplo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using CmpSdk.Callbacks;
#if UNITY_IOS
using CmpSdk.Delegates;
#endif
using CmpSdk.Models;
using Firebase.Analytics;
using Firebase.Extensions;
using UnityEngine;
using FirebaseApp = Firebase.FirebaseApp;

namespace CmpSdk.Samples.Scripts
{
    // V prefix for Vendor
    public static class Vendors
    {
        public const string GoogleAnalytics = "S26";
        public const string AmazonAD = "793";
        public const string Facebook = "S7";
        public const string S1 = "S1";
    }

    // P prefix for Purpose
    public static class Purposes
    {
        public const string P1 = "1";
        public const string Marketing = "C2";
        public const string Technical = "S2";
        public const string Security = "S1";
    }


    public class CmpSampleScript : MonoBehaviour, IOnOpenCallback, IOnCloseCallback, IOnCmpNotOpenedCallback,
        IOnCmpButtonClickedCallback, IOnErrorCallback, IOnCmpGoogleConsentUpdatedCallback
    {
        readonly List<string> _purposeList = new() { Purposes.P1, Purposes.Marketing, Purposes.Technical, Purposes.Security };

        readonly List<string> _vendorList = new() { Vendors.S1, Vendors.GoogleAnalytics, Vendors.AmazonAD, Vendors.Facebook };

        // UI elements
        private string _idPurposeOrVendorInputField;
        private string _importStringInputField;
        [SerializeField] private CmpUIManager uiManager;

        // CmpManager Instance
        private CmpConfig _cmpConfig;
        private CmpManager _cmpManager;
        private Thread _mainThread;

        // Configuration constants
        private const string CodeId = "TOOD Your CMP Code ID";
        private const string Domain = "delivery.consentmanager.net";
        private const string AppName = "UnityExample";
        private const string Language = "DE";
        private FirebaseApp _app; 
        
        private void Awake()
        {
            if (!Application.isPlaying)
            {
                Debug.Log("Application is not playing.");
                return;
            }
            
            FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
                var dependencyStatus = task.Result;
                if (dependencyStatus == Firebase.DependencyStatus.Available) {
                    // Create and hold a reference to your FirebaseApp,
                    // where app is a Firebase.FirebaseApp property of your application class.
                    _app = FirebaseApp.DefaultInstance;

                    // Set a flag here to indicate whether Firebase is ready to use by your app.
                } else {
                    Debug.LogError($"Could not resolve all Firebase dependencies: {dependencyStatus}");
                    // Firebase Unity SDK is not safe to use here.
                }
            });
            _cmpManager = CmpManager.Instance;
        }
        
        private void Start()
        {
            _cmpConfig = new CmpConfig(CodeId, Domain, AppName, Language)
            {
                Debug = true,
                Timeout = 8000
            };
#if UNITY_ANDROID
            _cmpConfig.UIConfig.isOutsideTouchable = true;
            _cmpConfig.UIConfig.SetAndroidUiType(AndroidUiType.Dialog);
#endif
            // Initialize Consent Manager
            InitializeCmpManager();
            // Initialize UI buttons
            InitializeUIButtons();

            // Launch Consent Manager
            _cmpManager.Launch();
        }

        private void InitializeUIButtons()
        {
            uiManager.CreateButtons(
                new ButtonData("Open", OnClickOpenConsentLayer),
                new ButtonData("Check", OnClickOpenConsentLayerOnCheck),
                new ButtonData("Check?", OnclickCheckConsentRequired),
                new ButtonData("ATT?", OnClickRequestATTrackingStatus),
                new ButtonData("Get Status", OnClickDebugConsentStatus),
                new ButtonData("Initialize", OnClickInitialize),
                new ButtonData("Accept All", OnClickAcceptAll),
                new ButtonData("Reject All", OnClickRejectAll),
                new ButtonData("Reset", OnClickResetConsentData),
                new ButtonData("Import", OnClickImportCmpString),
                new ButtonData("Export", OnClickExportCmpString)
            );
            uiManager.CreateDropdown("Screen Config", GetScreenConfigOptions(), s =>
            {
                AppendLog($"Set Screen ${s}");
                _cmpConfig.UIConfig.screenConfig = (CmpUIConfig.ScreenConfig) Enum.Parse(typeof(CmpUIConfig.ScreenConfig), s);  
                _cmpManager.SetUIConfig(_cmpConfig.UIConfig); 
            });
            uiManager.CreateDropdown("Purposes", _purposeList, s =>
            {
                _idPurposeOrVendorInputField = s;
                CheckHasPurpose(s);
            });
            uiManager.CreateDropdown("Vendors", _vendorList, s =>
            {
                _idPurposeOrVendorInputField = s; 
                CheckHasVendor(s);
            }); 
        }

        private void InitializeCmpManager()
        {
            _mainThread = Thread.CurrentThread;
            AppendLog("Consentmanager SampleScene started");

#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
        _cmpManager.Initialize(_cmpConfig);
        _cmpManager.AddGoogleConsentModeListener(OnGoogleConsentUpdated);
        _cmpManager.AddEventListeners(OnOpen, OnClose, OnNotOpened, OnCmpButtonClicked, OnError);
#endif

#if UNITY_IOS
        CmpATTrackingManager.EnableAutomaticATTracking();
        CmpATTrackingManager.Instance.RegisterOnATTrackingStatusChangedListener(OnCmpATTrackingStatusChanged);
#endif
        }
        
        #region Button Events

        private void OnClickInitialize()
        {
            AppendLog("Initialize");
            _cmpManager.Launch();
        }

        private async void OnClickRejectAll()
        {
            AppendLog("Calling Reject All"); 
            await _cmpManager.RejectAll();
            AppendLog("Rejected All");
        }

        private async void OnClickAcceptAll()
        {
            AppendLog("Calling Accept All");
            await _cmpManager.AcceptAll();
            AppendLog("Accepted All");
        }

        private void OnClickRequestATTrackingStatus()
        {
            AppendLog("Request AT Tracking Status");
            _ = _cmpManager.RequestATTrackingPermission();
        }

        private async void OnclickCheckConsentRequired()
        {
            AppendLog("Calling Check Consent Required");
            var isRequired = await _cmpManager.CheckConsentIsRequired();
            AppendLog($"Is consent required: {isRequired}");
        }

        private void OnClickOpenConsentLayer()
        {
            AppendLog("Open Consent Layer");
            _cmpManager.OpenConsentLayer();
        }

        private void OnClickOpenConsentLayerOnCheck()
        {
            AppendLog("Open Consent Layer on Check");
            _cmpManager.OpenConsentLayerOnCheck();
        }

        private void OnClickImportCmpString()
        {
            AppendLog("Click Import");
            ImportCmpString();
        }

        private void OnClickResetConsentData()
        {
            _cmpManager.Reset();
            AppendLog("Reset");
        }

        private void OnClickDebugConsentStatus()
        {
            DebugConsentStatus();
        }

        private void OnClickExportCmpString()
        {
            var cmpString = _cmpManager.ExportCmpString();
            AppendLog($"Exported CMP String: {cmpString}");
        }

        private void CheckHasPurpose(string purposeId)
        {
            var hasPurpose = _cmpManager.HasPurpose(purposeId);
            AppendLog($"Has Purpose ({purposeId}): {hasPurpose}");
        }

        private void CheckHasVendor(string vendorId)
        {
            var hasVendor = _cmpManager.HasVendor(vendorId);
            AppendLog($"Has Vendor ({vendorId}): {hasVendor}");
        }

        private async void ImportCmpString()
        {
            var cmpString = _importStringInputField;
            CmpImportResult result;

            if (!string.IsNullOrEmpty(cmpString))
            {
                AppendLog($"Importing CMP String from input field: {cmpString}");
                result = await _cmpManager.ImportCmpString(cmpString);
            }
            else
            {
                AppendLog($"Importing CMP String from sample string: {cmpString}");
                result = await _cmpManager.ImportCmpString(cmpString);
            }

            AppendLog($"Unity import result: {result.IsSuccess} with message: {result.Message}");
        }

        #endregion

        #region Callbacks

        public void OnGoogleConsentUpdated(CmpGoogleConsentModeStatus status)
        {
            // Convert CmpGoogleConsentModeStatus to Firebase compatible dictionary
            var firebaseConsentDict = new Dictionary<ConsentType, ConsentStatus>();

            foreach (var consent in status.ConsentDictionary)
            {
                // Convert GoogleConsentType to Firebase ConsentType
                var firebaseConsentType = ConvertToFirebaseConsentType(consent.Key);

                // Convert GoogleConsentStatus to Firebase ConsentStatus
                var firebaseConsentStatus = ConvertToFirebaseConsentStatus(consent.Value);

                firebaseConsentDict[firebaseConsentType] = firebaseConsentStatus;
            }
            
            // Apply the consent settings to Firebase Analytics
            FirebaseAnalytics.SetConsent(firebaseConsentDict);
            AppendLog($"Google Consent Mode: {firebaseConsentDict}");
        }

        private static ConsentType ConvertToFirebaseConsentType(GoogleConsentType googleConsentType)
        {
            return googleConsentType switch
            {
                GoogleConsentType.AnalyticsStorage => ConsentType.AnalyticsStorage,
                GoogleConsentType.AdStorage => ConsentType.AdStorage,
                GoogleConsentType.AdUserData => ConsentType.AdUserData,
                GoogleConsentType.AdPersonalization => ConsentType.AdPersonalization,
                _ => throw new InvalidEnumArgumentException($"Unknown GoogleConsentType: {googleConsentType}")
            };
        }

        private static ConsentStatus ConvertToFirebaseConsentStatus(GoogleConsentStatus googleConsentStatus)
        {
            return googleConsentStatus switch
            {
                GoogleConsentStatus.Granted => ConsentStatus.Granted,
                GoogleConsentStatus.Denied => ConsentStatus.Denied,
                _ => throw new InvalidEnumArgumentException($"Unknown GoogleConsentStatus: {googleConsentStatus}")
            };
        }

        public void OnClose()
        {
            LogThreadContext("OnClose");
            AppendLog("CMPConsentTool closed");
        }

        public void OnCmpButtonClicked(CmpButtonEvent buttonEvent)
        {
            LogThreadContext("OnCmpButtonClicked");
            AppendLog($"CMPButton clicked. Event: {buttonEvent}");
        }

        public void OnNotOpened()
        {
            LogThreadContext("OnNotOpened");
            AppendLog("CMPConsentTool not opened");
        }

        public void OnError(CmpErrorType errorType, string message)
        {
            LogThreadContext("OnError");
            AppendLog($"Error: {errorType}, {message}");
        }

        public void OnOpen()
        {
            LogThreadContext("OnOpen");
            AppendLog("CMPConsentTool opened");
        }
#if UNITY_IOS 
        private void OnCmpATTrackingStatusChanged(ATTrackingManagerAuthorizationStatus oldStatus,
            ATTrackingManagerAuthorizationStatus newStatus, double lastUpdated)
        {
            var unixTime = lastUpdated;
            var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            dtDateTime = dtDateTime.AddSeconds(unixTime).ToLocalTime();
            AppendLog("OnCmpATTrackingStatusChanged: " + newStatus + " lastUpdated: " + dtDateTime);
        }
#endif
        
        #endregion

        private void DebugConsentStatus()
        {
            var hasConsent = _cmpManager.HasConsent();
            var allPurposes = _cmpManager.GetAllPurposes();
            var disabledPurposes = _cmpManager.GetAllPurposes();
            var enabledPurposes = _cmpManager.GetEnabledPurposes();
            var allVendors = _cmpManager.GetAllVendors();
            var disabledVendors = _cmpManager.GetDisabledVendors();
            var enabledVendors = _cmpManager.GetEnabledVendors();
            var exportCmp = _cmpManager.ExportCmpString();
            
            AppendLog("-----------------");
            AppendLog($"Unity All Purposes: {string.Join(", ", allPurposes)}");
            AppendLog($"Unity Disabled Purposes: {string.Join(", ", disabledPurposes)}");
            AppendLog($"Unity Enabled Purposes: {string.Join(", ", enabledPurposes)}");
            AppendLog($"Unity All Vendors: {string.Join(", ", allVendors)}");
            AppendLog($"Unity Disabled Vendors: {string.Join(", ", disabledVendors)}");
            AppendLog($"Unity Enabled Vendors: {string.Join(", ", enabledVendors)}");
            AppendLog($"Unity Exported CMP String: {exportCmp}");
            AppendLog($"Unity Has Consent: {hasConsent}");
            AppendLog($"Unity US Privacy String: {_cmpManager.GetUsPrivacyString()}");
            AppendLog($"Unity Google Ac String: {_cmpManager.GetGoogleAcString()}");
            AppendLog($"Unity Has Purpose C1: {_cmpManager.HasPurpose("c1")}");
            AppendLog($"Unity Has Vendor 10: {_cmpManager.HasVendor("628")}");
            AppendLog($"Unity Google Consent Mode Status: {_cmpManager.GetGoogleConsentModeStatus()}");
            AppendLog("-----------------");
        }

        #region Helper

        private void LogThreadContext(string callbackName)
        {
            var onMainThread = IsMainThread();
            var threadId = Thread.CurrentThread.ManagedThreadId;
            AppendLog($"{callbackName} called. Is main thread: {onMainThread} ID: {threadId}");
        }

        private bool IsMainThread()
        {
            return _mainThread.Equals(Thread.CurrentThread);
        }

        private void AppendLog(string message)
        {
            Debug.Log(message); 

            if (uiManager != null)
            {
                uiManager.AddLogText(message);
            }
        }
                
        private List<string> GetScreenConfigOptions()
        {
            var options = new List<string>();
            foreach (var config in Enum.GetValues(typeof(CmpUIConfig.ScreenConfig)))
            {
                options.Add(config.ToString());
            }

            return options;
        }

        #endregion
    }
}

 

Volver