diff --git a/Runtime/GuruConsent/Runtime/Script/Consent/GoogleDMAHelper.cs b/Runtime/GuruConsent/Runtime/Script/Consent/GoogleDMAHelper.cs index 9ed2d97..3dcb5b3 100644 --- a/Runtime/GuruConsent/Runtime/Script/Consent/GoogleDMAHelper.cs +++ b/Runtime/GuruConsent/Runtime/Script/Consent/GoogleDMAHelper.cs @@ -1,4 +1,6 @@ +using System; + namespace Guru { using System.Collections.Generic; @@ -15,6 +17,15 @@ namespace Guru public class GoogleDMAHelper { public static readonly bool UsingDelayAppMeasurement = false; + + public static List PurposesRules = new List + { + "0", // 1 + "", // * + "2,3", // 3&4 + "0,6" // 1&7 + }; + /// /// Set DMA status @@ -49,27 +60,7 @@ namespace Guru } // build an dict for record consent data - Dictionary consentData = new Dictionary() - { - { ConsentType.AdStorage, ConsentStatus.Denied }, - { ConsentType.AnalyticsStorage, ConsentStatus.Denied }, - { ConsentType.AdPersonalization, ConsentStatus.Denied }, - { ConsentType.AdUserData, ConsentStatus.Denied }, - }; - - // purpose 2,3,4,5,9 granted => adStorage, adPersonalization, adUserData granted - if (purposes[1] && purposes[2] && purposes[3] && purposes[4] && purposes[8]) - { - consentData[ConsentType.AdStorage] = ConsentStatus.Granted; - consentData[ConsentType.AdPersonalization] = ConsentStatus.Granted; - consentData[ConsentType.AdUserData] = ConsentStatus.Granted; - } - - // purpose 1, 7 granted => analyticsStorage granted - if (purposes[0] && purposes[6]) - { - consentData[ConsentType.AnalyticsStorage] = ConsentStatus.Granted; - } + Dictionary consentData = ApplyPurposesRules(purposes); // build result data for guru analytics string result = "0000"; @@ -140,5 +131,59 @@ namespace Guru return result; } + + private static Dictionary ApplyPurposesRules(bool[] purposes) + { + Dictionary consentData = new Dictionary() + { + { ConsentType.AdStorage, ConsentStatus.Denied }, + { ConsentType.AnalyticsStorage, ConsentStatus.Denied }, + { ConsentType.AdPersonalization, ConsentStatus.Denied }, + { ConsentType.AdUserData, ConsentStatus.Denied }, + }; + + for (int i = 0; i < PurposesRules.Count; i++) + { + ConsentType type = ConsentType.AdStorage; + switch (i) + { + default: type = ConsentType.AdStorage; break; + case 1: type = ConsentType.AnalyticsStorage; break; + case 2: type = ConsentType.AdPersonalization; break; + case 3: type = ConsentType.AdUserData; break; + } + + bool granted = true; + if (string.IsNullOrEmpty(PurposesRules[i])) + { + // null pass directly. + } + else + { + // parse all the single rule + int[] rule = Array.ConvertAll(PurposesRules[i].Split(','), s => + { + if(string.IsNullOrEmpty(s)) return -1; + int.TryParse(s, out var v); + return v; + }); + for (int j = 0; j < rule.Length; j++) + { + var idx = rule[j]; + if (idx < 0 || idx >= purposes.Length || !purposes[rule[j]]) + { + granted = false; + break; + } + } + } + consentData[type] = granted ? ConsentStatus.Granted : ConsentStatus.Denied; + } + + return consentData; + } + + + } } \ No newline at end of file