update: SetUserProperty 代码 review

--story=1021114 --user=yufei.hu 【中台】打点逻辑优化:初始化流程, 设置用户属性缓存 (QA 无需测试) https://www.tapd.cn/33527076/s/1160725

Signed-off-by: huyufei <yufei.hu@castbox.fm>
胡宇飞 2024-07-30 21:14:39 +08:00
parent 7e499aa26c
commit cd83fd8bab
10 changed files with 52 additions and 65 deletions

View File

@ -1,20 +1,20 @@
namespace Guru namespace Guru
{ {
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using UnityEngine; using UnityEngine;
using System.Diagnostics.CodeAnalysis;
public class GuruAnalytics public class GuruAnalytics
{ {
// Plugin Version // Plugin Version
public const string Version = "1.11.1"; private const string Version = "1.11.1";
public static readonly string Tag = "[ANU]"; public static readonly string Tag = "[ANU]";
private static readonly string ActionName = "logger_error"; private static readonly string ActionName = "logger_error";
@ -36,6 +36,7 @@ namespace Guru
private bool _isReady = false; private bool _isReady = false;
public bool IsReady => _isReady; public bool IsReady => _isReady;
private IAnalyticsAgent _agent; private IAnalyticsAgent _agent;
@ -82,8 +83,8 @@ namespace Guru
/// <summary> /// <summary>
/// 错误 code 表 /// 错误 code 表
/// </summary> /// </summary>
public List<int> ErrorCodeList = new List<int>(); private readonly List<int> _errorCodeList = new List<int>();
private bool _enableErrorLog = false; private bool _enableErrorLog;
/// <summary> /// <summary>
/// 启动日志错误上报 /// 启动日志错误上报
@ -149,6 +150,7 @@ namespace Guru
{ {
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return;
CacheUserProperty(key, value); // 添加用户属性 CacheUserProperty(key, value); // 添加用户属性
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
Agent.SetUserProperty(key, value); Agent.SetUserProperty(key, value);
} }
/// <summary> /// <summary>
@ -226,6 +228,7 @@ namespace Guru
/// <param name="eventName">事件名称</param> /// <param name="eventName">事件名称</param>
/// <param name="data">INT类型的值</param> /// <param name="data">INT类型的值</param>
/// <param name="priority"></param> /// <param name="priority"></param>
[SuppressMessage("ReSharper", "Unity.PerformanceCriticalCodeInvocation")]
public void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1) public void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1)
{ {
string raw = ""; 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 try
{ {
@ -368,7 +372,7 @@ namespace Guru
/// <param name="errorInfo"></param> /// <param name="errorInfo"></param>
private void OnLoggerErrorEvent(int code, string errorInfo = "") 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(); var codeString = ((AnalyticsCode)code).ToString();
if (string.IsNullOrEmpty(codeString) || codeString == AnalyticsCode.Unknown.ToString()) codeString = $"ErrorCode:{code}"; 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}"); Debug.Log($"{Tag} ------ ParseWithJson: json:\n{json}");
@ -432,9 +436,8 @@ namespace Guru
info = "no data property"; info = "no data property";
} }
ReportCodeInfo(code, info); ReportCodeInfo(code, info);
return true;
} }
catch (Exception ex) catch (Exception)
{ {
string p = "\"msg\":\""; string p = "\"msg\":\"";
string m = json; string m = json;
@ -445,7 +448,6 @@ namespace Guru
Analytics.LogCrashlytics(info); Analytics.LogCrashlytics(info);
ReportCodeInfo(code, info); ReportCodeInfo(code, info);
} }
return false;
} }
/** /**
@ -544,7 +546,7 @@ namespace Guru
case AnalyticsCode.Unknown: case AnalyticsCode.Unknown:
case AnalyticsCode.DELETE_EXPIRED: case AnalyticsCode.DELETE_EXPIRED:
case AnalyticsCode.UPLOAD_FAIL: case AnalyticsCode.UPLOAD_FAIL:
case AnalyticsCode.Network_Lost: case AnalyticsCode.NETWORK_LOST:
case AnalyticsCode.CRONET_INIT_FAIL: case AnalyticsCode.CRONET_INIT_FAIL:
case AnalyticsCode.CRONET_INIT_EXCEPTION: case AnalyticsCode.CRONET_INIT_EXCEPTION:
case AnalyticsCode.ERROR_API: case AnalyticsCode.ERROR_API:
@ -566,15 +568,15 @@ namespace Guru
canCatch = true; canCatch = true;
} }
if (ErrorCodeList != null && ErrorCodeList.Count > 0) if (_errorCodeList != null && _errorCodeList.Count > 0)
{ {
if (ErrorCodeList[0] == -1) if (_errorCodeList[0] == -1)
{ {
canCatch = true; canCatch = true;
} }
else else
{ {
canCatch = ErrorCodeList.Contains(code); canCatch = _errorCodeList.Contains(code);
} }
} }
@ -608,7 +610,7 @@ namespace Guru
DELETE_EXPIRED = 12, DELETE_EXPIRED = 12,
UPLOAD_FAIL = 14, UPLOAD_FAIL = 14,
Network_Lost = 22, NETWORK_LOST = 22,
CRONET_INIT_FAIL = 26, CRONET_INIT_FAIL = 26,
CRONET_INIT_EXCEPTION = 27, CRONET_INIT_EXCEPTION = 27,

View File

@ -39,11 +39,13 @@ namespace Guru
if (ClassAnalytics != null) if (ClassAnalytics != null)
{ {
ClassAnalytics.CallStatic(methodName, args); ClassAnalytics.CallStatic(methodName, args);
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
if(_isDebug) Debug.Log($"{GuruAnalytics.Tag} Android call static :: {methodName}"); if(_isDebug) Debug.Log($"{GuruAnalytics.Tag} Android call static :: {methodName}");
} }
} }
catch (Exception e) catch (Exception e)
{ {
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
Debug.LogError(e.Message); Debug.LogError(e.Message);
} }
#endif #endif
@ -104,6 +106,7 @@ namespace Guru
public void SetUserProperty(string key, string value) public void SetUserProperty(string key, string value)
{ {
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return;
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
CallStatic("setUserProperty", key, value); CallStatic("setUserProperty", key, value);
} }
public void SetFirebaseId(string id) public void SetFirebaseId(string id)

View File

@ -1,28 +1,27 @@
using System.Collections.Generic;
namespace Guru namespace Guru
{ {
using System.Collections.Concurrent;
using System.Collections.Generic;
public interface IEventDriver public interface IEventDriver
{ {
void TriggerFlush(); void TriggerFlush();
void AddEvent(TrackingEvent trackingEvent); void AddEvent(TrackingEvent trackingEvent);
} }
public interface IPropertyCollecter public interface IPropertyCollector
{ {
void AddProperty(string key, string value); 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 readonly GuruEventBuffer<TrackingEvent> _eventBuffer = new GuruEventBuffer<TrackingEvent>();
private GuruEventBuffer<TrackingProperty> _propertyBuffer = new GuruEventBuffer<TrackingProperty>(); private readonly ConcurrentDictionary<string, string> _userPropertyMap = new ConcurrentDictionary<string, string>();
// Firebase 是否可用 // Firebase 是否可用
private bool _isDriverReady = false; private bool _isDriverReady;
public void TriggerFlush() public void TriggerFlush()
{ {
@ -35,6 +34,7 @@ namespace Guru
Analytics.EventSetting setting = null, int priority = -1) Analytics.EventSetting setting = null, int priority = -1)
{ {
var trackingEvent= new TrackingEvent(eventName, data, setting, priority); var trackingEvent= new TrackingEvent(eventName, data, setting, priority);
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
AddEvent(trackingEvent); AddEvent(trackingEvent);
} }
@ -46,6 +46,7 @@ namespace Guru
{ {
if (_isDriverReady) if (_isDriverReady)
{ {
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
FlushTrackingEvent(trackingEvent); FlushTrackingEvent(trackingEvent);
} }
else else
@ -61,14 +62,13 @@ namespace Guru
/// <param name="value"></param> /// <param name="value"></param>
public void AddProperty(string key, string value) public void AddProperty(string key, string value)
{ {
var property = new TrackingProperty(key, value);
if (_isDriverReady) if (_isDriverReady)
{ {
FlushProperty(property); SetUserProperty(key, value);
} }
else else
{ {
_propertyBuffer.Push(property); _userPropertyMap[key] = value;
} }
} }
@ -82,10 +82,11 @@ namespace Guru
FlushTrackingEvent(trackingEvent); 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 FlushTrackingEvent(TrackingEvent trackEvent);
protected abstract void FlushProperty(TrackingProperty property); protected abstract void SetUserProperty(string key, string value);
} }

View File

@ -1,7 +1,3 @@
using System.Collections.Generic;
namespace Guru namespace Guru
{ {
using com.adjust.sdk; using com.adjust.sdk;
@ -31,7 +27,7 @@ namespace Guru
} }
} }
protected override void FlushProperty(TrackingProperty property) protected override void SetUserProperty(string key, string value)
{ {
} }

View File

@ -18,7 +18,7 @@ namespace Guru
FBService.LogEvent(eventName, null, data); FBService.LogEvent(eventName, null, data);
} }
protected override void FlushProperty(TrackingProperty property) protected override void SetUserProperty(string key, string value)
{ {
} }

View File

@ -54,10 +54,9 @@ namespace Guru
/// <summary> /// <summary>
/// 输出属性 /// 输出属性
/// </summary> /// </summary>
/// <param name="property"></param> protected override void SetUserProperty(string key, string value)
protected override void FlushProperty(TrackingProperty property)
{ {
FirebaseAnalytics.SetUserProperty(property.key, property.value); FirebaseAnalytics.SetUserProperty(key, value);
} }
} }

View File

@ -3,7 +3,7 @@ namespace Guru
using System.Collections.Concurrent; using System.Collections.Concurrent;
public class GuruEventBuffer<T> public class GuruEventBuffer<T>
{ {
private ConcurrentQueue<T> _buffer; private readonly ConcurrentQueue<T> _buffer;
/// <summary> /// <summary>
/// 构造函数 /// 构造函数

View File

@ -4,16 +4,17 @@ namespace Guru
{ {
protected override void FlushTrackingEvent(TrackingEvent trackingEvent) protected override void FlushTrackingEvent(TrackingEvent trackingEvent)
{ {
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
GuruAnalytics.Instance.LogEvent(trackingEvent.eventName, trackingEvent.data, trackingEvent.priority); GuruAnalytics.Instance.LogEvent(trackingEvent.eventName, trackingEvent.data, trackingEvent.priority);
} }
/// <summary> /// <summary>
/// 输出属性 /// 输出属性
/// </summary> /// </summary>
/// <param name="property"></param> protected override void SetUserProperty(string key, string value)
protected override void FlushProperty(TrackingProperty property)
{ {
GuruAnalytics.Instance.SetUserProperty(property.key, property.value); // ReSharper disable once Unity.PerformanceCriticalCodeInvocation
GuruAnalytics.Instance.SetUserProperty(key, value);
} }
} }

View File

@ -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}";
}
}

View File

@ -18,6 +18,7 @@ namespace Guru
public static Action<bool> OnFirebaseAuthResult; public static Action<bool> OnFirebaseAuthResult;
public static Action<bool> OnUserAuthResult; public static Action<bool> OnUserAuthResult;
// TODO: 需要从 FirebaseUtils 内拿走
public static Action<string> OnAdjustDeeplinkCallback = null; public static Action<string> OnAdjustDeeplinkCallback = null;
@ -48,7 +49,8 @@ namespace Guru
{ {
InitCrashlytics(); // 老项目沿用此逻辑 InitCrashlytics(); // 老项目沿用此逻辑
InitRemoteConfig(); // 老项目沿用此逻辑 InitRemoteConfig(); // 老项目沿用此逻辑
InitAdjustService(); // 初始化 Firebase 服务 // TODO: 需要从 FirebaseUtils 内拿走
InitAdjustService(); // 初始化 adjust 服务
if (IPMConfig.IPM_UID.IsNullOrEmpty()) if (IPMConfig.IPM_UID.IsNullOrEmpty())
{ {
@ -93,6 +95,7 @@ namespace Guru
#region 启动 Adjust 服务 #region 启动 Adjust 服务
// TODO: 需要从 FirebaseUtils 内拿走
/// <summary> /// <summary>
/// 启动 Adjust 服务 /// 启动 Adjust 服务
/// </summary> /// </summary>