150 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			150 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using Firebase.RemoteConfig;
 | |
| using Guru;
 | |
| using Guru.LitJson;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace Guru
 | |
| {
 | |
|     
 | |
|     
 | |
|     /// <summary>
 | |
|     /// ABTEST 管理器
 | |
|     /// </summary>
 | |
|     public class ABTestManager : Singleton<ABTestManager>
 | |
|     {
 | |
| 
 | |
|         private FirebaseRemoteConfig _remoteConfig;
 | |
| 
 | |
|         private List<ABParamData> _params;
 | |
| 
 | |
|         
 | |
|         #region 初始化
 | |
|         
 | |
|         /// <summary>
 | |
|         ///  初始化
 | |
|         /// </summary>
 | |
|         public static void Init()
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 Instance.Setup();
 | |
|             }
 | |
|             catch (Exception e)
 | |
|             {
 | |
|                 Debug.LogError(e);
 | |
|             }
 | |
|             
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 安装服务
 | |
|         /// </summary>
 | |
|         private void Setup()
 | |
|         {
 | |
|             Debug.Log($"[AB] --- <color=#88ff00>ABTest Init</color>");
 | |
|             _params = new List<ABParamData>();
 | |
|             
 | |
|             _remoteConfig = FirebaseRemoteConfig.DefaultInstance;
 | |
|             
 | |
|             string strValue;
 | |
|             foreach (var key in _remoteConfig.Keys)
 | |
|             {
 | |
|                 strValue = _remoteConfig.GetValue(key).StringValue;
 | |
|                 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_"))
 | |
|             {
 | |
|                 _params.Add(ABParamData.Parse(value)); // 添加参数
 | |
|             }
 | |
|         }
 | |
|         
 | |
|         #endregion
 | |
|     }
 | |
|     
 | |
|     [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}");
 | |
|             var p = new ABParamData();
 | |
|             p.value = value;
 | |
|             
 | |
|             // 发现Guru AB测试标志位
 | |
|             var dict = JsonMapper.ToObject<Dictionary<string, object>>(value);
 | |
|             if (null != dict)
 | |
|             {
 | |
|                 foreach (var k in dict.Keys)
 | |
|                 {
 | |
|                     if (k.StartsWith("guru_ab"))
 | |
|                     {
 | |
|                         p.id = GetItemKey(k);
 | |
|                         p.group = dict[k].ToString();
 | |
|                         // Debug.Log($"[AB] add property {k}: {dict[k]}");
 | |
|                         break;
 | |
|                     }
 | |
|                 }
 | |
|             }
 | |
|             
 | |
|             return p;
 | |
|         }
 | |
| 
 | |
|         private static string GetItemKey(string raw)
 | |
|         {
 | |
|             var h = raw.Replace("guru_", "");
 | |
|             var key = h.Substring(0, Mathf.Min(PARAM_NAME_LENGTH, h.Length)); // 最大长度23
 | |
|             return key; 
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 输出字符串
 | |
|         /// </summary>
 | |
|         /// <returns></returns>
 | |
|         public override string ToString()
 | |
|         {
 | |
|             return $"{id} : {group}";
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
| } |