diff --git a/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs b/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs index 6708a08..ee97a29 100644 --- a/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs +++ b/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs @@ -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 /// /// 错误 code 表 /// - public List ErrorCodeList = new List(); - private bool _enableErrorLog = false; + private readonly List _errorCodeList = new List(); + private bool _enableErrorLog; /// /// 启动日志错误上报 @@ -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); } /// @@ -226,6 +228,7 @@ namespace Guru /// 事件名称 /// INT类型的值 /// + [SuppressMessage("ReSharper", "Unity.PerformanceCriticalCodeInvocation")] public void LogEvent(string eventName, Dictionary data = null, int priority = -1) { string raw = ""; @@ -252,7 +255,8 @@ namespace Guru } */ - private string BuildParamsJson(Dictionary data) + [SuppressMessage("ReSharper", "Unity.PerformanceCriticalCodeInvocation")] + private static string BuildParamsJson(Dictionary data) { try { @@ -368,7 +372,7 @@ namespace Guru /// 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, diff --git a/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs b/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs index 079be01..97dd967 100644 --- a/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs +++ b/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs @@ -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) diff --git a/Runtime/GuruCore/Runtime/Analytics/Events/AbstractEventDriver.cs b/Runtime/GuruCore/Runtime/Analytics/Events/AbstractEventDriver.cs index de69f4c..5a6bc8a 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Events/AbstractEventDriver.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Events/AbstractEventDriver.cs @@ -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 _eventBuffer = new GuruEventBuffer(); - private GuruEventBuffer _propertyBuffer = new GuruEventBuffer(); + private readonly GuruEventBuffer _eventBuffer = new GuruEventBuffer(); + private readonly ConcurrentDictionary _userPropertyMap = new ConcurrentDictionary(); // 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 /// 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); } diff --git a/Runtime/GuruCore/Runtime/Analytics/Events/AdjustEventDriver.cs b/Runtime/GuruCore/Runtime/Analytics/Events/AdjustEventDriver.cs index a91e54b..7cfe63d 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Events/AdjustEventDriver.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Events/AdjustEventDriver.cs @@ -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) { } diff --git a/Runtime/GuruCore/Runtime/Analytics/Events/FBEventDriver.cs b/Runtime/GuruCore/Runtime/Analytics/Events/FBEventDriver.cs index 9cba219..d81d61b 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Events/FBEventDriver.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Events/FBEventDriver.cs @@ -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) { } diff --git a/Runtime/GuruCore/Runtime/Analytics/Events/FirebaseEventDriver.cs b/Runtime/GuruCore/Runtime/Analytics/Events/FirebaseEventDriver.cs index 16526cd..2e0bda7 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Events/FirebaseEventDriver.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Events/FirebaseEventDriver.cs @@ -54,10 +54,9 @@ namespace Guru /// /// 输出属性 /// - /// - protected override void FlushProperty(TrackingProperty property) + protected override void SetUserProperty(string key, string value) { - FirebaseAnalytics.SetUserProperty(property.key, property.value); + FirebaseAnalytics.SetUserProperty(key, value); } } diff --git a/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventBuffer.cs b/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventBuffer.cs index 78b185b..5eb877f 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventBuffer.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventBuffer.cs @@ -3,7 +3,7 @@ namespace Guru using System.Collections.Concurrent; public class GuruEventBuffer { - private ConcurrentQueue _buffer; + private readonly ConcurrentQueue _buffer; /// /// 构造函数 diff --git a/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventDriver.cs b/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventDriver.cs index 27aa75f..3489d22 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventDriver.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Events/GuruEventDriver.cs @@ -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); } /// /// 输出属性 /// - /// - 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); } } diff --git a/Runtime/GuruCore/Runtime/Analytics/Events/TrackingEvent.cs b/Runtime/GuruCore/Runtime/Analytics/Events/TrackingEvent.cs index 968ddee..fd06116 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Events/TrackingEvent.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Events/TrackingEvent.cs @@ -39,25 +39,7 @@ namespace Guru } } - /// - /// 追踪用户属性 - /// - 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}"; - } - } + diff --git a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs index 5e76bd8..ad9441e 100644 --- a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs +++ b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs @@ -18,7 +18,8 @@ namespace Guru public static Action OnFirebaseAuthResult; public static Action OnUserAuthResult; - public static Action OnAdjustDeeplinkCallback = null; + // TODO: 需要从 FirebaseUtils 内拿走 + public static Action 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 内拿走 /// /// 启动 Adjust 服务 ///