update: 再次对齐 Purposes 规则

deeplink
胡宇飞 2024-02-23 17:01:10 +08:00
parent 18c7876f8f
commit 82328c1555
1 changed files with 66 additions and 21 deletions

View File

@ -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<string> PurposesRules = new List<string>
{
"0", // 1
"", // *
"2,3", // 3&4
"0,6" // 1&7
};
/// <summary>
/// Set DMA status
@ -49,27 +60,7 @@ namespace Guru
}
// build an dict<type, status> for record consent data
Dictionary<ConsentType, ConsentStatus> consentData = new Dictionary<ConsentType, ConsentStatus>()
{
{ 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<ConsentType, ConsentStatus> consentData = ApplyPurposesRules(purposes);
// build result data for guru analytics
string result = "0000";
@ -140,5 +131,59 @@ namespace Guru
return result;
}
private static Dictionary<ConsentType, ConsentStatus> ApplyPurposesRules(bool[] purposes)
{
Dictionary<ConsentType, ConsentStatus> consentData = new Dictionary<ConsentType, ConsentStatus>()
{
{ 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;
}
}
}