fix: 修复用户属性缓存问题

Signed-off-by: huyufei <yufei.hu@castbox.fm>
dev
胡宇飞 2024-07-26 19:17:03 +08:00
parent 67abd081c8
commit d1dba297a7
10 changed files with 190 additions and 31 deletions

View File

@ -1,8 +1,25 @@
using System.Collections.Generic;
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 是否可用
private bool _isDriverReady = false;
@ -12,7 +29,20 @@ namespace Guru
_isDriverReady = true;
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)
{
@ -20,18 +50,42 @@ namespace Guru
}
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()
{
while(_buffer.Pop(out var trackingEvent))
while(_eventBuffer.Pop(out var 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 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);
}
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
{
using System.Collections.Concurrent;
public class GuruEventBuffer
public class GuruEventBuffer<T>
{
private ConcurrentQueue<TrackingEvent> _eventQueue;
public int Count => _eventQueue?.Count ?? -1;
private ConcurrentQueue<T> _buffer;
/// <summary>
/// 构造函数
/// </summary>
public GuruEventBuffer()
{
_eventQueue = new ConcurrentQueue<TrackingEvent>();
_buffer = new ConcurrentQueue<T>();
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="trackEvent"></param>
public void Push(TrackingEvent trackEvent)
/// <param name="item"></param>
public void Push(T item)
{
_eventQueue.Enqueue(trackEvent);
_buffer.Enqueue(item);
}
/// <summary>
/// 出栈
/// </summary>
/// <param name="trackingEvent"></param>
/// <param name="item"></param>
/// <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);
}
/// <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