| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Guru | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-04 08:40:43 +00:00
										 |  |  |     using System.Linq; | 
					
						
							| 
									
										
										
										
											2024-02-01 05:10:49 +00:00
										 |  |  |     using System; | 
					
						
							|  |  |  |     using System.Collections.Generic; | 
					
						
							|  |  |  |     using Firebase.RemoteConfig; | 
					
						
							| 
									
										
										
										
											2024-02-04 08:40:43 +00:00
										 |  |  |     using Newtonsoft.Json; | 
					
						
							| 
									
										
										
										
											2024-02-01 05:10:49 +00:00
										 |  |  |     using UnityEngine; | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |      | 
					
						
							|  |  |  |     /// <summary> | 
					
						
							|  |  |  |     /// ABTEST 管理器 | 
					
						
							|  |  |  |     /// </summary> | 
					
						
							|  |  |  |     public class ABTestManager : Singleton<ABTestManager> | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-04 08:40:43 +00:00
										 |  |  |         public const string Version = "1.0.2"; | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |         private FirebaseRemoteConfig _remoteConfig; | 
					
						
							|  |  |  |         private List<ABParamData> _params; | 
					
						
							| 
									
										
										
										
											2024-02-01 05:10:49 +00:00
										 |  |  |          | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         internal static void LogCrashlytics(string msg) => Analytics.LogCrashlytics(msg); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         internal static void LogCrashlytics(Exception ex) => Analytics.LogCrashlytics(ex); | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         #region 初始化 | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         /// <summary> | 
					
						
							|  |  |  |         ///  初始化 | 
					
						
							|  |  |  |         /// </summary> | 
					
						
							|  |  |  |         public static void Init() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             try | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Instance.Setup(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             catch (Exception e) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2024-02-01 05:10:49 +00:00
										 |  |  |                 LogCrashlytics(e); | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |                 Debug.LogError(e); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         /// <summary> | 
					
						
							|  |  |  |         /// 安装服务 | 
					
						
							|  |  |  |         /// </summary> | 
					
						
							|  |  |  |         private void Setup() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             Debug.Log($"[AB] --- <color=#88ff00>ABTest Init</color>"); | 
					
						
							|  |  |  |             _params = new List<ABParamData>(); | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             _remoteConfig = FirebaseRemoteConfig.DefaultInstance; | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2024-02-04 08:40:43 +00:00
										 |  |  |             Debug.Log($"[AB] --- remoteConfig Counts: {_remoteConfig.Keys.Count()}"); | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |             string strValue; | 
					
						
							|  |  |  |             foreach (var key in _remoteConfig.Keys) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 strValue = _remoteConfig.GetValue(key).StringValue; | 
					
						
							| 
									
										
										
										
											2024-02-04 08:40:43 +00:00
										 |  |  |                 Debug.Log($"[AB] --- raw config: [{key}] : {strValue}"); | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |                 AddParam(strValue); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             // ------- ABTest ----------- | 
					
						
							|  |  |  |             // Debug.Log($"<color=orange> --- start parse test string --- </color>"); | 
					
						
							|  |  |  |             // var testStr = @"{""enabled"":true,""value"":2,""id"":""B"",""guru_ab_23100715"":""B""}"; | 
					
						
							|  |  |  |             // AddParam(testStr); | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             if (_params.Count > 0) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < _params.Count; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     // 上报实验AB属性 | 
					
						
							|  |  |  |                     GuruAnalytics.SetUserProperty(_params[i].id, _params[i].group); | 
					
						
							|  |  |  | #if UNITY_EDITOR | 
					
						
							|  |  |  |                     Debug.Log($"[AB] --- Add AB Param <color=cyan>{_params[i].ToString()}</color>"); | 
					
						
							|  |  |  | #else | 
					
						
							|  |  |  |                     Debug.Log($"[AB] --- Add AB Param {_params[i].ToString()}"); | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         #endregion | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         #region 添加AB参数 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /// <summary> | 
					
						
							|  |  |  |         /// 添加AB参数 | 
					
						
							|  |  |  |         /// </summary> | 
					
						
							|  |  |  |         /// <param name="value"></param> | 
					
						
							|  |  |  |         private void AddParam(string value) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (!string.IsNullOrEmpty(value) && value.Contains("guru_ab_")) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |                 var p = ABParamData.Parse(value); | 
					
						
							|  |  |  |                 if(p != null) _params.Add(p); // 添加参数 | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         #endregion | 
					
						
							| 
									
										
										
										
											2024-01-30 06:59:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #region 单元测试 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public static void TestConfig(string json) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var p = ABParamData.Parse(json); | 
					
						
							|  |  |  |             if (p == null) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Debug.LogError($"Could not parse config: {json}"); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!string.IsNullOrEmpty(p.group)) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Debug.Log($"ID: <color=#88ff00>{p.id}</color>"); | 
					
						
							|  |  |  |                 Debug.Log($"Group: <color=#88ff00>{p.group}</color>"); | 
					
						
							|  |  |  |                 Debug.Log($"Value: <color=#88ff00>{p.value}</color>"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         #endregion | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     [Serializable] | 
					
						
							|  |  |  |     internal class ABParamData | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         private const int PARAM_NAME_LENGTH = 23; // 从开始"ab_" 计算, 往后20个字符 | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         public string id; | 
					
						
							|  |  |  |         public string group; | 
					
						
							|  |  |  |         public string value; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         public static ABParamData Parse(string value) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             Debug.Log($"--- ABParamData.Parse: {value}"); | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |             try | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |                 // 发现Guru AB测试标志位 | 
					
						
							| 
									
										
										
										
											2024-02-04 08:40:43 +00:00
										 |  |  |                 // var dict = JsonMapper.ToObject<Dictionary<string, JsonData>>(value); | 
					
						
							|  |  |  |                 var dict =  JsonConvert.DeserializeObject<Dictionary<string, object>>(value); | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |                 if (null != dict) | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |                 { | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |                     foreach (var k in dict.Keys) | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |                     { | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |                         if (k.StartsWith("guru_ab")) | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             return new ABParamData() | 
					
						
							|  |  |  |                             { | 
					
						
							|  |  |  |                                 id = GetItemKey(k), | 
					
						
							|  |  |  |                                 group = dict[k].ToString(), | 
					
						
							|  |  |  |                                 value = value | 
					
						
							|  |  |  |                             }; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |             catch (Exception e) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 string msg = $"[AB] --- Parse AB Param Error -> Value: {value}\n{e.Message}"; | 
					
						
							| 
									
										
										
										
											2024-02-01 05:10:49 +00:00
										 |  |  |                 ABTestManager.LogCrashlytics(msg); | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |                 Debug.Log(msg); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         private static string GetItemKey(string raw) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-01-30 07:41:53 +00:00
										 |  |  |             int ln = "guru_".Length; | 
					
						
							|  |  |  |             var key = raw.Substring(ln, Mathf.Min(PARAM_NAME_LENGTH, raw.Length - ln)); // 最大长度23 | 
					
						
							| 
									
										
										
										
											2023-12-26 04:22:19 +00:00
										 |  |  |             return key;  | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /// <summary> | 
					
						
							|  |  |  |         /// 输出字符串 | 
					
						
							|  |  |  |         /// </summary> | 
					
						
							|  |  |  |         /// <returns></returns> | 
					
						
							|  |  |  |         public override string ToString() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return $"{id} : {group}"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |