fix: 优化实验策略,数据读取逻辑
--story=1021087 --user=yufei.hu 【中台】【自打点】升级自打点原生库至 1.1.1,并在 Water1 中落地验证 https://www.tapd.cn/33527076/s/1159023 Signed-off-by: huyufei <yufei.hu@castbox.fm>hotfix/v1.0.12.2
							parent
							
								
									6dd2574763
								
							
						
					
					
						commit
						9de443249d
					
				| 
						 | 
					@ -58,7 +58,7 @@ namespace Guru
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 默认的本地配置
 | 
					        /// 默认的本地配置
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
        public static string DEFAULT_GURU_ANALYTICS_EXP = @"{
 | 
					        private static string DEFAULT_GURU_ANALYTICS_EXP = @"{
 | 
				
			||||||
	""enable"": true,
 | 
						""enable"": true,
 | 
				
			||||||
	""exps"": [{
 | 
						""exps"": [{
 | 
				
			||||||
		""groupId"": ""B"",
 | 
							""groupId"": ""B"",
 | 
				
			||||||
| 
						 | 
					@ -94,10 +94,9 @@ namespace Guru
 | 
				
			||||||
            string localGroup = "";
 | 
					            string localGroup = "";
 | 
				
			||||||
            string remoteGroup = "";
 | 
					            string remoteGroup = "";
 | 
				
			||||||
            GuruAnalyticsExpData expData = null;
 | 
					            GuruAnalyticsExpData expData = null;
 | 
				
			||||||
            bool usingLocalData = false;
 | 
					            GuruAnalyticsExpConfig config = null;
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
            var gid = SavedGuruAnalyticsExpGroupId;
 | 
					            if(IsDebug) Debug.LogWarning($"{Tag} --- #0 Analytics EXP saved groupId :{SavedGuruAnalyticsExpGroupId}");
 | 
				
			||||||
            if(IsDebug) Debug.LogWarning($"{Tag} --- #0 Analytics EXP saved groupId :{gid}");
 | 
					 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            // 拉取云控数据
 | 
					            // 拉取云控数据
 | 
				
			||||||
            var json = "";
 | 
					            var json = "";
 | 
				
			||||||
| 
						 | 
					@ -106,48 +105,69 @@ namespace Guru
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
            if (string.IsNullOrEmpty(json))
 | 
					            if (string.IsNullOrEmpty(json))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
 | 
					                // 没有云控值,走本地的数据配置,随机取值
 | 
				
			||||||
                if(IsDebug) Debug.LogWarning($"{Tag} --- #1 Analytics EXP json is Null -> using DefaultData");
 | 
					                if(IsDebug) Debug.LogWarning($"{Tag} --- #1 Analytics EXP json is Null -> using DefaultData");
 | 
				
			||||||
                expData = DefaultData;
 | 
					                config = GetDefaultGuruAnalyticsExpConfig();
 | 
				
			||||||
                usingLocalData = true;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else
 | 
					            else
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if(IsDebug) Debug.LogWarning($"{Tag} --- #2 Analytics EXP Try to get remote json -> {json}");
 | 
					                if(IsDebug) Debug.LogWarning($"{Tag} --- #2 Analytics EXP Try to get remote json -> {json}");
 | 
				
			||||||
                expData = Parse(json);
 | 
					                expData = Parse(json);
 | 
				
			||||||
                usingLocalData = false;
 | 
					                if (expData == null)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    if(IsDebug) Debug.LogWarning($"{Tag} --- #2.1 Analytics EXP Parse failed -> using DefaultData");
 | 
				
			||||||
 | 
					                    config = GetDefaultGuruAnalyticsExpConfig();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    if (!expData.enable)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        Debug.LogWarning($"{Tag} --- #2.2 Analytics EXP Disabled -> using DefaultData");
 | 
				
			||||||
 | 
					                        expData = DefaultData;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    config = expData.GetFirstConfig();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            GuruAnalyticsExpConfig config = null;
 | 
					            
 | 
				
			||||||
            if (string.IsNullOrEmpty(gid))
 | 
					            if (config == null) {
 | 
				
			||||||
 | 
					                config = DefaultData.GetFirstConfig(); // 默认是 B 组
 | 
				
			||||||
 | 
					                if(IsDebug) Debug.LogWarning($"{Tag} --- #3 Try get config is Null -> using Default config");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (config != null)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                config = expData.GetRandomConfig();
 | 
					                baseUrl = config.baseUrl;
 | 
				
			||||||
                if(IsDebug) Debug.LogWarning($"{Tag} --- #3 no group Id, get random one:  id: {config?.groupId ?? "NULL"}");
 | 
					                groupId = config.groupId;
 | 
				
			||||||
 | 
					                uploadIpAddress = config.uploadIpAddress;
 | 
				
			||||||
 | 
					                SavedGuruAnalyticsExpGroupId = groupId;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else
 | 
					            else
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                config = expData.GetConfig(gid);
 | 
					                isEnable = false;
 | 
				
			||||||
                if (config == null)
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    config = expData.GetRandomConfig();
 | 
					 | 
				
			||||||
                    if(IsDebug) Debug.LogWarning($"{Tag} --- #4 Get old Config failed: {gid}, change to random:  id: {config?.groupId ?? "NULL"}");
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            if (config == null) {
 | 
					 | 
				
			||||||
                config = DefaultData.exps[0]; // 默认是 B 组
 | 
					 | 
				
			||||||
                if(IsDebug) Debug.LogWarning($"{Tag} --- #6 Get all config is Null -> using Default config B");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
			
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            baseUrl = config.baseUrl;
 | 
					 | 
				
			||||||
            groupId = config.groupId;
 | 
					 | 
				
			||||||
            uploadIpAddress = config.uploadIpAddress;
 | 
					 | 
				
			||||||
            SavedGuruAnalyticsExpGroupId = groupId;
 | 
					 | 
				
			||||||
            Debug.Log($"{Tag} --- Analytics EXP params::  groupId:{groupId}  baseUrl:{baseUrl}  uploadIpAddress:[{string.Join(",", uploadIpAddress)}]");
 | 
					            Debug.Log($"{Tag} --- Analytics EXP params::  groupId:{groupId}  baseUrl:{baseUrl}  uploadIpAddress:[{string.Join(",", uploadIpAddress)}]");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        private static GuruAnalyticsExpConfig GetDefaultGuruAnalyticsExpConfig()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            GuruAnalyticsExpConfig config = null; 
 | 
				
			||||||
 | 
					            if (!string.IsNullOrEmpty(SavedGuruAnalyticsExpGroupId))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                config = DefaultData.GetConfig(SavedGuruAnalyticsExpGroupId); // 非空则取值
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                config = DefaultData.GetRandomConfig();  // 随机获取本地的 Config
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if(IsDebug) Debug.LogWarning($"{Tag} --- #1.1 using Default GropId: {config?.groupId ?? "NULL"}");
 | 
				
			||||||
 | 
					            return config;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// 实验数据主题
 | 
					    /// 实验数据主题
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
| 
						 | 
					@ -182,6 +202,16 @@ namespace Guru
 | 
				
			||||||
            return null;
 | 
					            return null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// 获取首个配置
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <returns></returns>
 | 
				
			||||||
 | 
					        public GuruAnalyticsExpConfig GetFirstConfig()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (exps != null && exps.Length > 0) return exps[0];
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 分组是否存在
 | 
					        /// 分组是否存在
 | 
				
			||||||
| 
						 | 
					@ -202,7 +232,7 @@ namespace Guru
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// 实验配置
 | 
					    /// 实验配置
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public class GuruAnalyticsExpConfig
 | 
					    internal class GuruAnalyticsExpConfig
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public string groupId;
 | 
					        public string groupId;
 | 
				
			||||||
        public string baseUrl;
 | 
					        public string baseUrl;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					using System.Text.RegularExpressions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Guru
 | 
					namespace Guru
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	using System;
 | 
						using System;
 | 
				
			||||||
| 
						 | 
					@ -57,7 +59,6 @@ namespace Guru
 | 
				
			||||||
		/// </summary>
 | 
							/// </summary>
 | 
				
			||||||
		private static void InitializeFirebaseComp()
 | 
							private static void InitializeFirebaseComp()
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			InitGuruAnalyticsExpDefaultRemoteValue(); // 提前注入云控参数默认值
 | 
					 | 
				
			||||||
			InitCrashlytics();  // 老项目沿用此逻辑
 | 
								InitCrashlytics();  // 老项目沿用此逻辑
 | 
				
			||||||
			InitRemoteConfig();	// 老项目沿用此逻辑
 | 
								InitRemoteConfig();	// 老项目沿用此逻辑
 | 
				
			||||||
			InitAssetByFirebaseIdAsync(); // 获取到 FirebaseID 后异步执行逻辑
 | 
								InitAssetByFirebaseIdAsync(); // 获取到 FirebaseID 后异步执行逻辑
 | 
				
			||||||
| 
						 | 
					@ -153,7 +154,6 @@ namespace Guru
 | 
				
			||||||
		// Firebase 初始化才能调用此方法
 | 
							// Firebase 初始化才能调用此方法
 | 
				
			||||||
		private static void InitAnalytics()
 | 
							private static void InitAnalytics()
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
 | 
					 | 
				
			||||||
#if UNITY_ANDROID
 | 
					#if UNITY_ANDROID
 | 
				
			||||||
			// Android 平台开始
 | 
								// Android 平台开始
 | 
				
			||||||
			var fid = IPMConfig.FIREBASE_ID;// 获取 FirebaseID
 | 
								var fid = IPMConfig.FIREBASE_ID;// 获取 FirebaseID
 | 
				
			||||||
| 
						 | 
					@ -162,35 +162,18 @@ namespace Guru
 | 
				
			||||||
			// 随机抽取本地分组获取数据
 | 
								// 随机抽取本地分组获取数据
 | 
				
			||||||
			GuruAnalyticsExp.GetGuruAnalyticsExpParams(out string groupId, out string baseUrl, out string[] uploadIpAddress, out bool isEnable);
 | 
								GuruAnalyticsExp.GetGuruAnalyticsExpParams(out string groupId, out string baseUrl, out string[] uploadIpAddress, out bool isEnable);
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			// 自打点启动前,先上报一下分数数据
 | 
					 | 
				
			||||||
			GuruAnalytics.SetUserProperty(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, groupId); // 设置实验分组
 | 
					 | 
				
			||||||
			
 | 
					 | 
				
			||||||
			// 初始化打点和自打点
 | 
								// 初始化打点和自打点
 | 
				
			||||||
			Analytics.InitAnalytics(baseUrl, uploadIpAddress, isEnable, enableErrorLog, fid); // 打点提前初始化
 | 
								Analytics.InitAnalytics(baseUrl, uploadIpAddress, isEnable, enableErrorLog, fid); // 打点提前初始化
 | 
				
			||||||
 | 
								Analytics.SetUserProperty(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, groupId); // 上报分组数据
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// iOS 不参与分组
 | 
								// iOS 不参与分组
 | 
				
			||||||
			GuruAnalytics.SetUserProperty(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, "none");
 | 
					 | 
				
			||||||
			Analytics.InitAnalytics();
 | 
								Analytics.InitAnalytics();
 | 
				
			||||||
 | 
								Analytics.SetUserProperty(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, "none"); // 上报分组数据
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
#if UNITY_ANDROID	
 | 
					 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		// 添加默认的云控参数
 | 
					 | 
				
			||||||
		private static void InitGuruAnalyticsExpDefaultRemoteValue()
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			// AppendDefaultValue(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, GuruAnalyticsExp.DEFAULT_GURU_ANALYTICS_EXP);
 | 
					 | 
				
			||||||
			FirebaseRemoteConfig.DefaultInstance.SetDefaultsAsync(new Dictionary<string, object>()
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				{ GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, GuruAnalyticsExp.DEFAULT_GURU_ANALYTICS_EXP }
 | 
					 | 
				
			||||||
			});
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#endif			
 | 
					 | 
				
			||||||
		#endregion
 | 
							#endregion
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue