update: SetUserProperty 代码 review
--story=1021114 --user=yufei.hu 【中台】打点逻辑优化:初始化流程, 设置用户属性缓存 (QA 无需测试) https://www.tapd.cn/33527076/s/1160725 Signed-off-by: huyufei <yufei.hu@castbox.fm>
							parent
							
								
									7e499aa26c
								
							
						
					
					
						commit
						cd83fd8bab
					
				|  | @ -1,20 +1,20 @@ | |||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
|     using System; | ||||
|     using System.Collections; | ||||
|     using System.Collections.Generic; | ||||
|     using System.Globalization; | ||||
|     using System.Linq; | ||||
|     using Newtonsoft.Json; | ||||
|     using Newtonsoft.Json.Linq; | ||||
|     using UnityEngine; | ||||
| 
 | ||||
|     using System.Diagnostics.CodeAnalysis; | ||||
|      | ||||
|     public class GuruAnalytics | ||||
|     { | ||||
|         // Plugin Version | ||||
|         public const string Version = "1.11.1"; | ||||
|         private const string Version = "1.11.1"; | ||||
|          | ||||
|         public static readonly string Tag = "[ANU]"; | ||||
|         private static readonly string ActionName = "logger_error"; | ||||
|  | @ -36,6 +36,7 @@ namespace Guru | |||
| 
 | ||||
| 
 | ||||
|         private bool _isReady = false; | ||||
|          | ||||
|         public bool IsReady => _isReady; | ||||
|          | ||||
|         private IAnalyticsAgent _agent; | ||||
|  | @ -82,8 +83,8 @@ namespace Guru | |||
|         /// <summary> | ||||
|         /// 错误 code 表 | ||||
|         /// </summary> | ||||
|         public List<int> ErrorCodeList = new List<int>(); | ||||
|         private bool _enableErrorLog = false; | ||||
|         private readonly List<int> _errorCodeList = new List<int>(); | ||||
|         private bool _enableErrorLog; | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 启动日志错误上报 | ||||
|  | @ -149,6 +150,7 @@ namespace Guru | |||
|         { | ||||
|             if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return; | ||||
|             CacheUserProperty(key, value); // 添加用户属性 | ||||
|             // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|             Agent.SetUserProperty(key, value); | ||||
|         } | ||||
|         /// <summary> | ||||
|  | @ -226,6 +228,7 @@ namespace Guru | |||
|         /// <param name="eventName">事件名称</param> | ||||
|         /// <param name="data">INT类型的值</param> | ||||
|         /// <param name="priority"></param> | ||||
|         [SuppressMessage("ReSharper", "Unity.PerformanceCriticalCodeInvocation")] | ||||
|         public void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1) | ||||
|         { | ||||
|             string raw = ""; | ||||
|  | @ -252,7 +255,8 @@ namespace Guru | |||
|         } | ||||
|         */ | ||||
| 
 | ||||
|         private string BuildParamsJson(Dictionary<string, dynamic> data) | ||||
|         [SuppressMessage("ReSharper", "Unity.PerformanceCriticalCodeInvocation")] | ||||
|         private static string BuildParamsJson(Dictionary<string, dynamic> data) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|  | @ -368,7 +372,7 @@ namespace Guru | |||
|         /// <param name="errorInfo"></param> | ||||
|         private void OnLoggerErrorEvent(int code, string errorInfo = "") | ||||
|         { | ||||
|             // Debug.Log($"{Tag} --- OnLoggerErrorEvent: code:{code}\tinfo:{errorInfo}"); | ||||
|             // Debug.Log($"{Tag} --- OnLoggerErrorEvent: code:{code}\t info:{errorInfo}"); | ||||
|              | ||||
|             var codeString = ((AnalyticsCode)code).ToString(); | ||||
|             if (string.IsNullOrEmpty(codeString) || codeString == AnalyticsCode.Unknown.ToString()) codeString = $"ErrorCode:{code}"; | ||||
|  | @ -401,7 +405,7 @@ namespace Guru | |||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         private bool ParseWithJson(string json) | ||||
|         private void ParseWithJson(string json) | ||||
|         { | ||||
|             Debug.Log($"{Tag} ------ ParseWithJson: json:\n{json}"); | ||||
|              | ||||
|  | @ -432,9 +436,8 @@ namespace Guru | |||
|                     info = "no data property"; | ||||
|                 } | ||||
|                 ReportCodeInfo(code, info); | ||||
|                 return true; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             catch (Exception) | ||||
|             { | ||||
|                 string p = "\"msg\":\""; | ||||
|                 string m = json; | ||||
|  | @ -445,7 +448,6 @@ namespace Guru | |||
|                 Analytics.LogCrashlytics(info); | ||||
|                 ReportCodeInfo(code, info); | ||||
|             } | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         /** | ||||
|  | @ -544,7 +546,7 @@ namespace Guru | |||
|                 case AnalyticsCode.Unknown: | ||||
|                 case AnalyticsCode.DELETE_EXPIRED: | ||||
|                 case AnalyticsCode.UPLOAD_FAIL: | ||||
|                 case AnalyticsCode.Network_Lost: | ||||
|                 case AnalyticsCode.NETWORK_LOST: | ||||
|                 case AnalyticsCode.CRONET_INIT_FAIL: | ||||
|                 case AnalyticsCode.CRONET_INIT_EXCEPTION: | ||||
|                 case AnalyticsCode.ERROR_API: | ||||
|  | @ -566,15 +568,15 @@ namespace Guru | |||
|                 canCatch = true; | ||||
|             } | ||||
| 
 | ||||
|             if (ErrorCodeList != null && ErrorCodeList.Count > 0) | ||||
|             if (_errorCodeList != null && _errorCodeList.Count > 0) | ||||
|             { | ||||
|                 if (ErrorCodeList[0] == -1) | ||||
|                 if (_errorCodeList[0] == -1) | ||||
|                 { | ||||
|                     canCatch = true; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     canCatch = ErrorCodeList.Contains(code); | ||||
|                     canCatch = _errorCodeList.Contains(code); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|  | @ -608,7 +610,7 @@ namespace Guru | |||
|          | ||||
|         DELETE_EXPIRED = 12, | ||||
|         UPLOAD_FAIL = 14, | ||||
|         Network_Lost = 22, | ||||
|         NETWORK_LOST = 22, | ||||
|         CRONET_INIT_FAIL = 26, | ||||
|         CRONET_INIT_EXCEPTION = 27, | ||||
|          | ||||
|  |  | |||
|  | @ -39,11 +39,13 @@ namespace Guru | |||
|                 if (ClassAnalytics != null) | ||||
|                 { | ||||
|                     ClassAnalytics.CallStatic(methodName, args); | ||||
|                     // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|                     if(_isDebug) Debug.Log($"{GuruAnalytics.Tag} Android call static :: {methodName}"); | ||||
|                 } | ||||
|             } | ||||
|             catch (Exception e) | ||||
|             { | ||||
|                 // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|                 Debug.LogError(e.Message); | ||||
|             } | ||||
| #endif     | ||||
|  | @ -104,6 +106,7 @@ namespace Guru | |||
|         public void SetUserProperty(string key, string value) | ||||
|         { | ||||
|             if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return; | ||||
|             // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|             CallStatic("setUserProperty", key, value); | ||||
|         } | ||||
|         public void SetFirebaseId(string id) | ||||
|  |  | |||
|  | @ -1,28 +1,27 @@ | |||
| using System.Collections.Generic; | ||||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
|      | ||||
|     using System.Collections.Concurrent; | ||||
|     using System.Collections.Generic; | ||||
|     public interface IEventDriver | ||||
|     { | ||||
|         void TriggerFlush(); | ||||
|         void AddEvent(TrackingEvent trackingEvent); | ||||
|     } | ||||
| 
 | ||||
|     public interface IPropertyCollecter | ||||
|     public interface IPropertyCollector | ||||
|     { | ||||
|         void AddProperty(string key, string value); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     public abstract class AbstractEventDriver: IEventDriver, IPropertyCollecter | ||||
|     public abstract class AbstractEventDriver: IEventDriver, IPropertyCollector | ||||
|     { | ||||
|         private GuruEventBuffer<TrackingEvent> _eventBuffer = new GuruEventBuffer<TrackingEvent>(); | ||||
|         private GuruEventBuffer<TrackingProperty> _propertyBuffer = new GuruEventBuffer<TrackingProperty>(); | ||||
|         private readonly GuruEventBuffer<TrackingEvent> _eventBuffer = new GuruEventBuffer<TrackingEvent>(); | ||||
|         private readonly ConcurrentDictionary<string, string> _userPropertyMap = new ConcurrentDictionary<string, string>(); | ||||
|          | ||||
|         // Firebase 是否可用 | ||||
|         private bool _isDriverReady = false; | ||||
|         private bool _isDriverReady; | ||||
|          | ||||
|         public void TriggerFlush() | ||||
|         { | ||||
|  | @ -35,6 +34,7 @@ namespace Guru | |||
|             Analytics.EventSetting setting = null, int priority = -1) | ||||
|         { | ||||
|             var trackingEvent= new TrackingEvent(eventName, data, setting, priority); | ||||
|             // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|             AddEvent(trackingEvent); | ||||
|         } | ||||
| 
 | ||||
|  | @ -46,6 +46,7 @@ namespace Guru | |||
|         { | ||||
|             if (_isDriverReady) | ||||
|             { | ||||
|                 // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|                 FlushTrackingEvent(trackingEvent); | ||||
|             } | ||||
|             else | ||||
|  | @ -61,14 +62,13 @@ namespace Guru | |||
|         /// <param name="value"></param> | ||||
|         public void AddProperty(string key, string value) | ||||
|         { | ||||
|             var property = new TrackingProperty(key, value); | ||||
|             if (_isDriverReady) | ||||
|             { | ||||
|                 FlushProperty(property); | ||||
|                 SetUserProperty(key, value); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 _propertyBuffer.Push(property); | ||||
|                 _userPropertyMap[key] = value; | ||||
|             } | ||||
|         } | ||||
|          | ||||
|  | @ -82,10 +82,11 @@ namespace Guru | |||
|                 FlushTrackingEvent(trackingEvent); | ||||
|             } | ||||
| 
 | ||||
|             while (_propertyBuffer.Pop(out var property)) | ||||
|             foreach (var key in _userPropertyMap.Keys) | ||||
|             { | ||||
|                 FlushProperty(property); | ||||
|                 SetUserProperty(key, _userPropertyMap[key]);     | ||||
|             } | ||||
|             _userPropertyMap.Clear(); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|  | @ -96,7 +97,7 @@ namespace Guru | |||
|         protected abstract void FlushTrackingEvent(TrackingEvent trackEvent); | ||||
| 
 | ||||
| 
 | ||||
|         protected abstract void FlushProperty(TrackingProperty property); | ||||
|         protected abstract void SetUserProperty(string key, string value); | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
|  |  | |||
|  | @ -1,7 +1,3 @@ | |||
| 
 | ||||
| 
 | ||||
| using System.Collections.Generic; | ||||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
|     using com.adjust.sdk; | ||||
|  | @ -31,7 +27,7 @@ namespace Guru | |||
|             } | ||||
|         } | ||||
| 
 | ||||
|         protected override void FlushProperty(TrackingProperty property) | ||||
|         protected override void SetUserProperty(string key, string value) | ||||
|         { | ||||
|              | ||||
|         } | ||||
|  |  | |||
|  | @ -18,7 +18,7 @@ namespace Guru | |||
|             FBService.LogEvent(eventName, null, data); | ||||
|         } | ||||
|          | ||||
|         protected override void FlushProperty(TrackingProperty property) | ||||
|         protected override void SetUserProperty(string key, string value) | ||||
|         { | ||||
|              | ||||
|         } | ||||
|  |  | |||
|  | @ -54,10 +54,9 @@ namespace Guru | |||
|         /// <summary> | ||||
|         /// 输出属性 | ||||
|         /// </summary> | ||||
|         /// <param name="property"></param> | ||||
|         protected override void FlushProperty(TrackingProperty property) | ||||
|         protected override void SetUserProperty(string key, string value) | ||||
|         { | ||||
|             FirebaseAnalytics.SetUserProperty(property.key, property.value); | ||||
|             FirebaseAnalytics.SetUserProperty(key, value); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
|  |  | |||
|  | @ -3,7 +3,7 @@ namespace Guru | |||
|     using System.Collections.Concurrent; | ||||
|     public class GuruEventBuffer<T>  | ||||
|     { | ||||
|         private ConcurrentQueue<T> _buffer; | ||||
|         private readonly ConcurrentQueue<T> _buffer; | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 构造函数 | ||||
|  |  | |||
|  | @ -4,16 +4,17 @@ namespace Guru | |||
|     { | ||||
|         protected override void FlushTrackingEvent(TrackingEvent trackingEvent) | ||||
|         { | ||||
|             // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|             GuruAnalytics.Instance.LogEvent(trackingEvent.eventName, trackingEvent.data, trackingEvent.priority); | ||||
|         } | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 输出属性 | ||||
|         /// </summary> | ||||
|         /// <param name="property"></param> | ||||
|         protected override void FlushProperty(TrackingProperty property) | ||||
|         protected override void SetUserProperty(string key, string value) | ||||
|         { | ||||
|             GuruAnalytics.Instance.SetUserProperty(property.key, property.value); | ||||
|             // ReSharper disable once Unity.PerformanceCriticalCodeInvocation | ||||
|             GuruAnalytics.Instance.SetUserProperty(key, value); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
|  |  | |||
|  | @ -39,25 +39,7 @@ namespace Guru | |||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// <summary> | ||||
|     /// 追踪用户属性 | ||||
|     /// </summary> | ||||
|     public class TrackingProperty | ||||
|     { | ||||
|         public string key; | ||||
|         public string value; | ||||
|          | ||||
|         public TrackingProperty(string key, string value) | ||||
|         { | ||||
|             this.key = key; | ||||
|             this.value = value; | ||||
|         } | ||||
|          | ||||
|         public override string ToString() | ||||
|         { | ||||
|             return $"property: {key}:{value}"; | ||||
|         } | ||||
|     } | ||||
|      | ||||
|      | ||||
|      | ||||
|      | ||||
|  |  | |||
|  | @ -18,7 +18,8 @@ namespace Guru | |||
| 
 | ||||
| 		public static Action<bool> OnFirebaseAuthResult; | ||||
| 		public static Action<bool> OnUserAuthResult; | ||||
| 		public static Action<string> OnAdjustDeeplinkCallback = null; | ||||
| 		// TODO: 需要从 FirebaseUtils 内拿走 | ||||
| 		public static Action<string> OnAdjustDeeplinkCallback = null;  | ||||
| 		 | ||||
| 		 | ||||
| 		public static void InitFirebase(Action callback) | ||||
|  | @ -48,7 +49,8 @@ namespace Guru | |||
| 		{ | ||||
| 			InitCrashlytics();  // 老项目沿用此逻辑 | ||||
| 			InitRemoteConfig();	// 老项目沿用此逻辑 | ||||
| 			InitAdjustService();  // 初始化 Firebase 服务 | ||||
| 			// TODO: 需要从 FirebaseUtils 内拿走 | ||||
| 			InitAdjustService();  // 初始化 adjust 服务 | ||||
| 			 | ||||
| 			if (IPMConfig.IPM_UID.IsNullOrEmpty()) | ||||
| 			{ | ||||
|  | @ -93,6 +95,7 @@ namespace Guru | |||
| 
 | ||||
| 		#region 启动 Adjust 服务 | ||||
| 		 | ||||
| 		// TODO: 需要从 FirebaseUtils 内拿走 | ||||
| 		/// <summary> | ||||
| 		/// 启动 Adjust 服务 | ||||
| 		/// </summary> | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue