Compare commits

...

2 Commits

Author SHA1 Message Date
胡宇飞 561d351b73 fix: 优化网络状态打点
Signed-off-by: huyufei <yufei.hu@castbox.fm>
2024-07-26 19:17:37 +08:00
胡宇飞 d1dba297a7 fix: 修复用户属性缓存问题
Signed-off-by: huyufei <yufei.hu@castbox.fm>
2024-07-26 19:17:03 +08:00
12 changed files with 251 additions and 108 deletions

View File

@ -130,14 +130,19 @@ namespace Guru
public static void SetCurrentScreen(string screenName, string className) public static void SetCurrentScreen(string screenName, string className)
{ {
if (!_isInitOnce)
{
return;
}
Log.I(TAG,$"SetCurrentScreen -> screenName:{screenName}, className:{className}"); Log.I(TAG,$"SetCurrentScreen -> screenName:{screenName}, className:{className}");
GuruAnalytics.Instance.SetScreen(screenName); GuruAnalytics.Instance.SetScreen(screenName);
if (!IsReady) return; TrackEvent(EventScreenView, new Dictionary<string, dynamic>()
FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventScreenView, {
new Parameter(FirebaseAnalytics.ParameterScreenClass, className), [ParameterScreenName] = screenName,
new Parameter(FirebaseAnalytics.ParameterScreenName, screenName) [ParameterScreenClass] = className,
); });
} }
#endregion #endregion
@ -159,15 +164,40 @@ namespace Guru
/// <summary> /// <summary>
/// Firebase上报用户属性 /// Firebase上报用户属性
/// </summary> /// </summary>
public static void SetUserProperty(string propertyName, string propertyValue) public static void SetUserProperty(string key, string value)
{ {
Debug.Log($"{TAG} --- SetUserProperty -> propertyName:{propertyName}, propertyValue:{propertyValue}"); if (!_isInitOnce)
{
throw new Exception($"[{TAG}][SDK] Analytics did not initialized, Call <Analytics.{nameof(InitAnalytics)}()> first!");
}
if (!IsReady) if (IsDebug && !EnableDebugAnalytics)
{
Debug.LogWarning($"[{TAG}][SDK] --- SetProperty {key}:{value} can not send int Debug mode. Set <InitConfig.EnableDebugAnalytics> with `true`");
return; return;
}
try
{
// 填充相关的追踪事件
_guruEventDriver.AddProperty(key, value);
_firebaseEventDriver.AddProperty(key, value);
Debug.Log($"{TAG} --- SetUserProperty -> propertyName:{key}, propertyValue:{value}");
}
catch (Exception ex)
{
if (FirebaseUtil.IsReady)
{
Crashlytics.LogException(ex);
}
else
{
Debug.Log($"Catch Error: {ex}");
}
}
FirebaseAnalytics.SetUserProperty(propertyName, propertyValue);
CustomSetUserProperty(propertyName, propertyValue);
} }
#endregion #endregion
@ -207,19 +237,19 @@ namespace Guru
// 填充相关的追踪事件 // 填充相关的追踪事件
if (eventSetting.EnableGuruAnalytics) if (eventSetting.EnableGuruAnalytics)
{ {
_guruEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority)); _guruEventDriver.AddEvent(eventName, data, eventSetting, priority);
} }
if (eventSetting.EnableFirebaseAnalytics) if (eventSetting.EnableFirebaseAnalytics)
{ {
_firebaseEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority)); _firebaseEventDriver.AddEvent(eventName, data, eventSetting, priority);
} }
if (eventSetting.EnableAdjustAnalytics) if (eventSetting.EnableAdjustAnalytics)
{ {
_adjustEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority)); _adjustEventDriver.AddEvent(eventName, data, eventSetting, priority);
} }
if (eventSetting.EnableFacebookAnalytics) if (eventSetting.EnableFacebookAnalytics)
{ {
_fbEventDriver.Append(new TrackingEvent(eventName, data, eventSetting, priority)); _fbEventDriver.AddEvent(eventName, data, eventSetting, priority);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -318,60 +348,9 @@ namespace Guru
#endregion #endregion
#region 打点缓存
private static Queue<TrackingEvent> _savedLogs;
internal static Queue<TrackingEvent> SavedLogs
{
get
{
if (_savedLogs == null) _savedLogs = new Queue<TrackingEvent>(20);
return _savedLogs;
}
}
#endregion
} }
public class TrackingEvent
{
public string eventName;
public int priority;
public Dictionary<string, dynamic> data;
public Analytics.EventSetting setting;
public TrackingEvent()
{
}
/// <summary>
/// 保存打点信息
/// </summary>
/// <param name="eventName"></param>
/// <param name="_data"></param>
/// <param name="_setting"></param>
/// <param name="_priority"></param>
public TrackingEvent(string eventName, Dictionary<string, dynamic> data = null, Analytics.EventSetting setting = null, int priority = -1)
{
this.eventName = eventName;
this.data = data;
this.setting = setting;
this.priority = priority;
}
public void Flush()
{
Analytics.TrackEvent(eventName, data, setting, priority);
}
public override string ToString()
{
return $"eventName: {eventName}, data: {data}, setting: {setting}, priority: {priority}";
}
}
} }

View File

@ -1,8 +1,25 @@
using System.Collections.Generic;
namespace Guru namespace Guru
{ {
public abstract class AbstractEventDriver: IEventDriver
public interface IEventDriver
{ {
private GuruEventBuffer _buffer = new GuruEventBuffer(); void TriggerFlush();
void AddEvent(TrackingEvent trackingEvent);
}
public interface IPropertyCollecter
{
void AddProperty(string key, string value);
}
public abstract class AbstractEventDriver: IEventDriver, IPropertyCollecter
{
private GuruEventBuffer<TrackingEvent> _eventBuffer = new GuruEventBuffer<TrackingEvent>();
private GuruEventBuffer<TrackingProperty> _propertyBuffer = new GuruEventBuffer<TrackingProperty>();
// Firebase 是否可用 // Firebase 是否可用
private bool _isDriverReady = false; private bool _isDriverReady = false;
@ -12,7 +29,20 @@ namespace Guru
_isDriverReady = true; _isDriverReady = true;
FlushAll(); FlushAll();
} }
public void Append(TrackingEvent trackingEvent)
public void AddEvent(string eventName, Dictionary<string, dynamic> data = null,
Analytics.EventSetting setting = null, int priority = -1)
{
var trackingEvent= new TrackingEvent(eventName, data, setting, priority);
AddEvent(trackingEvent);
}
/// <summary>
/// 添加事件
/// </summary>
/// <param name="trackingEvent"></param>
public void AddEvent(TrackingEvent trackingEvent)
{ {
if (_isDriverReady) if (_isDriverReady)
{ {
@ -20,18 +50,42 @@ namespace Guru
} }
else else
{ {
_buffer.Push(trackingEvent); _eventBuffer.Push(trackingEvent);
}
} }
/// <summary>
/// 添加属性
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddProperty(string key, string value)
{
var property = new TrackingProperty(key, value);
if (_isDriverReady)
{
FlushProperty(property);
}
else
{
_propertyBuffer.Push(property);
}
} }
/// <summary>
/// 写入所有
/// </summary>
private void FlushAll() private void FlushAll()
{ {
while(_buffer.Pop(out var trackingEvent)) while(_eventBuffer.Pop(out var trackingEvent))
{ {
FlushTrackingEvent(trackingEvent); FlushTrackingEvent(trackingEvent);
} }
while (_propertyBuffer.Pop(out var property))
{
FlushProperty(property);
}
} }
@ -42,5 +96,11 @@ namespace Guru
protected abstract void FlushTrackingEvent(TrackingEvent trackEvent); protected abstract void FlushTrackingEvent(TrackingEvent trackEvent);
protected abstract void FlushProperty(TrackingProperty property);
} }
} }

View File

@ -31,5 +31,9 @@ namespace Guru
} }
} }
protected override void FlushProperty(TrackingProperty property)
{
}
} }
} }

View File

@ -17,5 +17,10 @@ namespace Guru
FBService.LogEvent(eventName, null, data); FBService.LogEvent(eventName, null, data);
} }
protected override void FlushProperty(TrackingProperty property)
{
}
} }
} }

View File

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

View File

@ -1,51 +1,40 @@
using System.Collections.Generic;
namespace Guru namespace Guru
{ {
using System.Collections.Concurrent; using System.Collections.Concurrent;
public class GuruEventBuffer public class GuruEventBuffer<T>
{ {
private ConcurrentQueue<TrackingEvent> _eventQueue; private ConcurrentQueue<T> _buffer;
public int Count => _eventQueue?.Count ?? -1;
/// <summary> /// <summary>
/// 构造函数 /// 构造函数
/// </summary> /// </summary>
public GuruEventBuffer() public GuruEventBuffer()
{ {
_eventQueue = new ConcurrentQueue<TrackingEvent>(); _buffer = new ConcurrentQueue<T>();
} }
/// <summary> /// <summary>
/// 入栈 /// 入栈
/// </summary> /// </summary>
/// <param name="trackEvent"></param> /// <param name="item"></param>
public void Push(TrackingEvent trackEvent) public void Push(T item)
{ {
_eventQueue.Enqueue(trackEvent); _buffer.Enqueue(item);
} }
/// <summary> /// <summary>
/// 出栈 /// 出栈
/// </summary> /// </summary>
/// <param name="trackingEvent"></param> /// <param name="item"></param>
/// <returns></returns> /// <returns></returns>
public bool Pop(out TrackingEvent trackingEvent) public bool Pop(out T item)
{ {
return _eventQueue.TryDequeue(out trackingEvent); return _buffer.TryDequeue(out item);
} }
} }
public interface IEventDriver
{
void TriggerFlush();
void Append(TrackingEvent trackingEvent);
}

View File

@ -7,5 +7,14 @@ namespace Guru
GuruAnalytics.Instance.LogEvent(trackingEvent.eventName, trackingEvent.data, trackingEvent.priority); GuruAnalytics.Instance.LogEvent(trackingEvent.eventName, trackingEvent.data, trackingEvent.priority);
} }
/// <summary>
/// 输出属性
/// </summary>
/// <param name="property"></param>
protected override void FlushProperty(TrackingProperty property)
{
GuruAnalytics.Instance.SetUserProperty(property.key, property.value);
}
} }
} }

View File

@ -0,0 +1,12 @@
namespace Guru
{
using System.Collections.Concurrent;
public class GuruPropertyBuffer
{
private ConcurrentQueue<TrackingEvent> _propertyQueue;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 38faf6b8af864e829144d9de91c4b0a5
timeCreated: 1721983424

View File

@ -0,0 +1,65 @@
namespace Guru
{
using System.Collections.Generic;
/// <summary>
/// 追踪事件
/// </summary>
public class TrackingEvent
{
public string eventName;
public int priority;
public Dictionary<string, dynamic> data;
public Analytics.EventSetting setting;
public TrackingEvent()
{
}
/// <summary>
/// 保存打点信息
/// </summary>
/// <param name="eventName"></param>
/// <param name="_data"></param>
/// <param name="_setting"></param>
/// <param name="_priority"></param>
public TrackingEvent(string eventName, Dictionary<string, dynamic> data = null, Analytics.EventSetting setting = null, int priority = -1)
{
this.eventName = eventName;
this.data = data;
this.setting = setting;
this.priority = priority;
}
public override string ToString()
{
return $"eventName: {eventName}, data: {data}, setting: {setting}, priority: {priority}";
}
}
/// <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

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6b9532ec20444115a888ffc89276172a
timeCreated: 1721983520

View File

@ -16,6 +16,8 @@ namespace Guru.Network
private const string NETWORK_STATUS_ETHERNET = "ethernet"; private const string NETWORK_STATUS_ETHERNET = "ethernet";
private const string NETWORK_STATUS_VPN = "vpn"; private const string NETWORK_STATUS_VPN = "vpn";
private const string NETWORK_STATUS_TETHER = "tether"; private const string NETWORK_STATUS_TETHER = "tether";
private const string NETWORK_STATUS_BLUETOOTH = "bluetooth";
private const string NETWORK_STATUS_OTHER = "other";
private bool _isReady = false; private bool _isReady = false;
@ -46,18 +48,21 @@ namespace Guru.Network
} }
/// <summary>
/// 网络状态列表
/// </summary>
private List<string> _statusNameList = new List<string>() private List<string> _statusNameList = new List<string>()
{ {
NETWORK_STATUS_NONE,
NETWORK_STATUS_ETHERNET,
NETWORK_STATUS_WIFI,
NETWORK_STATUS_MOBILE,
NETWORK_STATUS_VPN, NETWORK_STATUS_VPN,
NETWORK_STATUS_TETHER NETWORK_STATUS_TETHER,
NETWORK_STATUS_MOBILE,
NETWORK_STATUS_WIFI,
NETWORK_STATUS_ETHERNET,
NETWORK_STATUS_BLUETOOTH,
NETWORK_STATUS_OTHER,
NETWORK_STATUS_NONE,
}; };
public void Init(Action<bool> onInitComplete = null) public void Init(Action<bool> onInitComplete = null)
{ {
Proxy.Init(success => Proxy.Init(success =>
@ -85,7 +90,7 @@ namespace Guru.Network
statusName = _statusNameList[i]; statusName = _statusNameList[i];
if (status.Contains(statusName)) if (status.Contains(statusName))
{ {
Debug.Log($"{Tag} --- GetNetworkStatus : {statusName}"); Debug.Log($"{Tag} --- GetNetworkStatus: [{string.Join(",", status)}]");
return statusName; return statusName;
} }
} }