parent
							
								
									67abd081c8
								
							
						
					
					
						commit
						d1dba297a7
					
				|  | @ -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); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
|  |      | ||||||
|  |      | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | @ -30,6 +30,10 @@ namespace Guru | ||||||
|                 Adjust.trackEvent(adjustEvent); |                 Adjust.trackEvent(adjustEvent); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|          | 
 | ||||||
|  |         protected override void FlushProperty(TrackingProperty property) | ||||||
|  |         { | ||||||
|  |              | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -17,5 +17,10 @@ namespace Guru | ||||||
|              |              | ||||||
|             FBService.LogEvent(eventName, null, data); |             FBService.LogEvent(eventName, null, data); | ||||||
|         } |         } | ||||||
|  |          | ||||||
|  |         protected override void FlushProperty(TrackingProperty property) | ||||||
|  |         { | ||||||
|  |              | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -50,6 +50,15 @@ namespace Guru | ||||||
|                 FirebaseAnalytics.LogEvent(eventName); |                 FirebaseAnalytics.LogEvent(eventName); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |          | ||||||
|  |         /// <summary> | ||||||
|  |         /// 输出属性 | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="property"></param> | ||||||
|  |         protected override void FlushProperty(TrackingProperty property) | ||||||
|  |         { | ||||||
|  |             FirebaseAnalytics.SetUserProperty(property.key, property.value); | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -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); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|      |      | ||||||
|          |          | ||||||
|  |  | ||||||
|  | @ -6,6 +6,15 @@ 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); | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -0,0 +1,12 @@ | ||||||
|  | namespace Guru | ||||||
|  | { | ||||||
|  |     using System.Collections.Concurrent; | ||||||
|  |      | ||||||
|  |     public class GuruPropertyBuffer | ||||||
|  |     { | ||||||
|  |         private ConcurrentQueue<TrackingEvent> _propertyQueue; | ||||||
|  |          | ||||||
|  |          | ||||||
|  |          | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | fileFormatVersion: 2 | ||||||
|  | guid: 38faf6b8af864e829144d9de91c4b0a5 | ||||||
|  | timeCreated: 1721983424 | ||||||
|  | @ -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}"; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |      | ||||||
|  |      | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | fileFormatVersion: 2 | ||||||
|  | guid: 6b9532ec20444115a888ffc89276172a | ||||||
|  | timeCreated: 1721983520 | ||||||
		Loading…
	
		Reference in New Issue