update: 打点事件优化, 优化打点缓存逻辑和初始化行为
--story=1021114 --user=yufei.hu 【中台】打点逻辑优化:初始化流程, 属性缓存 (QA 无需测试) https://www.tapd.cn/33527076/s/1159381 Signed-off-by: huyufei <yufei.hu@castbox.fm>dev
							parent
							
								
									515c875ee8
								
							
						
					
					
						commit
						dc991efbf3
					
				|  | @ -14,7 +14,7 @@ namespace Guru | |||
|     public class GuruAnalytics | ||||
|     { | ||||
|         // Plugin Version | ||||
|         public const string Version = "1.10.5"; | ||||
|         public const string Version = "1.11.1"; | ||||
|          | ||||
|         public static readonly string Tag = "[ANU]"; | ||||
|         private static readonly string ActionName = "logger_error"; | ||||
|  | @ -60,8 +60,6 @@ namespace Guru | |||
|         /// 错误 code 表 | ||||
|         /// </summary> | ||||
|         public static List<int> ErrorCodeList = new List<int>(); | ||||
| 
 | ||||
|         private static bool _autoSyncProperties = false; | ||||
|         private static bool _enableErrorLog = false; | ||||
|          | ||||
|         /// <summary> | ||||
|  | @ -84,11 +82,9 @@ namespace Guru | |||
|         /// 初始化接口 | ||||
|         /// </summary> | ||||
|         public static void Init(string appId, string deviceInfo, bool isDebug = false,  | ||||
|             bool enableErrorLog = false, bool syncProperties = false) | ||||
|             bool enableErrorLog = false) | ||||
|         { | ||||
|             Debug.Log($"{Tag} --- Guru Analytics [{Version}] initialing..."); | ||||
|              | ||||
|             _autoSyncProperties = syncProperties; | ||||
|             _enableErrorLog = enableErrorLog; | ||||
|             Agent?.Init(appId, deviceInfo, isDebug); | ||||
|             if(_enableErrorLog) InitCallbacks(); // 激活错误日志回调 | ||||
|  | @ -204,16 +200,13 @@ namespace Guru | |||
|         /// <param name="priority"></param> | ||||
|         public static void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1) | ||||
|         { | ||||
|             if(_autoSyncProperties) | ||||
|                 UpdateAllUserProperties(); // 每次打点更新用户属性 | ||||
|              | ||||
|             string raw = ""; | ||||
|             if (data != null && data.Count > 0) | ||||
|             { | ||||
|                 raw = BuildParamsJson(data); | ||||
|             } | ||||
|             if (priority < 0) priority = EventPriorityDefault; | ||||
|             Debug.Log($"{Tag} event:{eventName} | raw: {raw} | priority: {priority}"); | ||||
|             Debug.Log($"{Tag} --- LogEvent GuruAnalytics:{eventName} | raw: {raw} | priority: {priority}"); | ||||
|             Agent?.LogEvent(eventName, raw, priority); | ||||
|         } | ||||
| 
 | ||||
|  |  | |||
|  | @ -127,7 +127,7 @@ namespace Guru | |||
|                         } | ||||
|                     } | ||||
|                      | ||||
|                     Debug.Log($"{TAG} LogEvent: event:<color=orange>{eventName} ({priority})</color>  Properties:\n{sb.ToString()}"); | ||||
|                     Debug.Log($"{TAG} LogEvent: GuruAnalytics:<color=orange>{eventName} ({priority})</color>  Properties:\n{sb.ToString()}"); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  |  | |||
|  | @ -1,5 +1,7 @@ | |||
| 
 | ||||
| 
 | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace Guru | ||||
| { | ||||
|     using UnityEngine; | ||||
|  | @ -30,6 +32,21 @@ namespace Guru | |||
|             Debug.Log($"{Tag} Consent Request -> deviceid: {deviceId} debugGeography: {debugGeography}"); | ||||
| 
 | ||||
| #if UNITY_EDITOR | ||||
|             SendEditorCallback(); | ||||
| #endif | ||||
|         } | ||||
|          | ||||
| #if UNITY_EDITOR | ||||
|          | ||||
| #endif | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 延迟触发 Consent | ||||
|         /// </summary> | ||||
|         private async void SendEditorCallback() | ||||
|         { | ||||
|             await Task.Delay(2000); | ||||
|              | ||||
|             string msg = callbackMsgFmt.Replace("$0", $"{DebugStatusCode}").Replace("$1",DebugMessage); | ||||
|             var go = GameObject.Find(_objName); | ||||
|             if (go != null) | ||||
|  | @ -40,9 +57,10 @@ namespace Guru | |||
|             { | ||||
|                 Debug.LogError($"{Tag} Can't find callback object"); | ||||
|             } | ||||
| #endif | ||||
|         } | ||||
|          | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 获取 DMA 字段 | ||||
|         /// </summary> | ||||
|  |  | |||
|  | @ -16,13 +16,30 @@ namespace Guru | |||
| 	{ | ||||
| 		public class EventSetting | ||||
| 		{ | ||||
| 			public bool EnableFirebaseAnalytics = false; | ||||
| 			public bool EnableAdjustAnalytics = false; | ||||
| 			public bool EnableFacebookAnalytics = false; | ||||
| 			public bool EnableFirebaseAnalytics; | ||||
| 			public bool EnableAdjustAnalytics; | ||||
| 			public bool EnableFacebookAnalytics; | ||||
| 			public bool EnableGuruAnalytics; | ||||
| 
 | ||||
| 			public override string ToString() | ||||
| 			{ | ||||
| 				return $"EvenSetting: firebase:{EnableFirebaseAnalytics}, adjust:{EnableAdjustAnalytics}, facebook:{EnableFacebookAnalytics}, guru:{EnableGuruAnalytics}"; | ||||
| 			} | ||||
| 
 | ||||
| 			public static EventSetting GetDefaultSetting() | ||||
| 			{ | ||||
| 				return new EventSetting() | ||||
| 				{ | ||||
| 					EnableFirebaseAnalytics = true, | ||||
| 					EnableFacebookAnalytics = true, | ||||
| 					EnableAdjustAnalytics = true, | ||||
| 					EnableGuruAnalytics = true | ||||
| 				}; | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		private static EventSetting _defaultEventSetting; | ||||
| 		 | ||||
| 		private static EventSetting DefaultEventSetting => EventSetting.GetDefaultSetting(); | ||||
| 
 | ||||
| 		private static bool _isInited;				//Analytics是否初始化完成 | ||||
| 		public static bool EnableDebugAnalytics;	//允许Debug包上报打点 | ||||
| 
 | ||||
|  | @ -32,18 +49,19 @@ namespace Guru | |||
| 		{ | ||||
| 			get | ||||
| 			{ | ||||
| 				//Analytics没有初始化不上报打点 | ||||
| 				if (!_isInited) | ||||
| 					return false; | ||||
| 				 | ||||
| 				//Firebase服务没有初始化完成不上报打点 | ||||
| 				if (!FirebaseUtil.IsFirebaseInitialized) | ||||
| 					return false; | ||||
| 				 | ||||
| 				//Analytics没有初始化不上报打点 | ||||
| 				if (!_isInited) | ||||
| 					return false; | ||||
| 
 | ||||
| #if !UNITY_EDITOR  | ||||
| 				//开发环境打点不上报 | ||||
| 				if (IsDebugMode && !EnableDebugAnalytics) | ||||
| 					return false; | ||||
| 
 | ||||
| #endif | ||||
| 				return true; | ||||
| 			} | ||||
| 		} | ||||
|  | @ -60,18 +78,7 @@ namespace Guru | |||
| 			 | ||||
| 			// ------- 初始化自打点 ---------- | ||||
| 			InstallGuruAnalytics(IsDebug); | ||||
| 			 | ||||
| 			if (_defaultEventSetting == null) | ||||
| 			{ | ||||
| 				var analyticsSetting = GuruSettings.Instance.AnalyticsSetting; | ||||
| 				_defaultEventSetting = new EventSetting | ||||
| 				{ | ||||
| 					EnableFirebaseAnalytics = analyticsSetting.EnalbeFirebaseAnalytics, | ||||
| 					EnableFacebookAnalytics = analyticsSetting.EnalbeFacebookAnalytics, | ||||
| 					EnableAdjustAnalytics = analyticsSetting.EnalbeAdjustAnalytics | ||||
| 				}; | ||||
| 			} | ||||
| 			 | ||||
| 		 | ||||
| 			FirebaseUtil.onInitComplete += OnFirebaseCompleted; | ||||
| 		} | ||||
| 
 | ||||
|  | @ -79,17 +86,20 @@ namespace Guru | |||
| 		{ | ||||
| 			FirebaseUtil.onInitComplete -= OnFirebaseCompleted; | ||||
| 
 | ||||
| 			Debug.Log($"[SDK] --- Analytics Init After FirebaseCompleted: {success}"); | ||||
| 			 | ||||
| 			ConsumeAllCachedEvent(); | ||||
| 			 | ||||
| 			if (success) | ||||
| 			{ | ||||
| 				Crashlytics.IsCrashlyticsCollectionEnabled = true; | ||||
| 				if (_defaultEventSetting.EnableFirebaseAnalytics) | ||||
| 				{ | ||||
| 					FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); | ||||
| 					FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0)); | ||||
| 					SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google"); | ||||
| 					SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); | ||||
| 					// SetUserProperty(PropertyFirstOpenTime, FirstOpenTime); | ||||
| 				} | ||||
| 
 | ||||
| 				FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); | ||||
| 				FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0)); | ||||
| 				SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google"); | ||||
| 				SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); | ||||
| 				// SetUserProperty(PropertyFirstOpenTime, FirstOpenTime); | ||||
| 				 | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
|  | @ -150,13 +160,17 @@ namespace Guru | |||
| 		/// <param name="eventSetting"></param> | ||||
| 		internal static void LogEvent(string eventName, EventSetting eventSetting = null, int priority = -1) | ||||
| 		{ | ||||
| 			Log.I(TAG, $"eventName:{eventName}"); | ||||
| 			CustomLogEvent(eventName, null, priority); // 自定义打点上报 | ||||
| 			 | ||||
| 			if (eventSetting == null) eventSetting = DefaultEventSetting; | ||||
| 			 | ||||
| 			if(eventSetting.EnableGuruAnalytics)  | ||||
| 				CustomLogEvent(eventName, null, priority); // 自定义打点上报 | ||||
| 			 | ||||
| 			CheckLogCache(eventName, null, eventSetting); // log缓存和消费 | ||||
| 			 | ||||
| 			if (!IsEnable) return; | ||||
| 
 | ||||
| 			eventSetting ??= _defaultEventSetting; | ||||
| 			 | ||||
| 			Log.I(TAG, $" --- logEvent:{eventName}, eventSetting:{eventSetting}"); | ||||
| 			 | ||||
| 			if (eventSetting.EnableFirebaseAnalytics) | ||||
| 			{ | ||||
|  | @ -183,21 +197,24 @@ namespace Guru | |||
| 		/// <param name="priority"></param> | ||||
| 		internal static void LogEvent(string eventName, Dictionary<string, dynamic> extras, EventSetting eventSetting = null, int priority = -1) | ||||
| 		{ | ||||
| 			CustomLogEvent(eventName, extras, priority); // 自定义打点上报 | ||||
| 			if (eventSetting == null) eventSetting = DefaultEventSetting; | ||||
| 			 | ||||
| 			if(eventSetting.EnableGuruAnalytics)  | ||||
| 				CustomLogEvent(eventName, extras, priority); // 自定义打点上报 | ||||
| 			 | ||||
| 			CheckLogCache(eventName, extras, eventSetting); // log缓存和消费 | ||||
| 			 | ||||
| 			if (!IsEnable) return; | ||||
| 
 | ||||
| 			 | ||||
| 			if (extras == null) | ||||
| 			{ | ||||
| 				LogEvent(eventName, eventSetting, priority); // 防空判定 | ||||
| 				return; | ||||
| 			} | ||||
| 			 | ||||
| 			string paramStr = string.Join(",", extras); | ||||
| 			Log.I(TAG, $"eventName:{eventName}, params:{paramStr}"); | ||||
| 
 | ||||
| 			if (eventSetting == null) eventSetting = _defaultEventSetting; | ||||
| 			string paramStr = string.Join(",", extras); | ||||
| 			Log.I(TAG, $" --- logEvent:{eventName}, params:{paramStr}, eventSetting:{eventSetting}"); | ||||
| 			 | ||||
| 			if (eventSetting.EnableFirebaseAnalytics) | ||||
| 			{ | ||||
| 				List<Parameter> parameters = new List<Parameter>(); | ||||
|  | @ -356,28 +373,47 @@ namespace Guru | |||
| 		{ | ||||
| 			try | ||||
| 			{ | ||||
| 				if (!_isInited) | ||||
| 				if (!IsEnable) | ||||
| 				{ | ||||
| 					// 保存打点 | ||||
| 					if (data == null) data = new Dictionary<string, dynamic>(); | ||||
| 					data["log_stamp"] = TimeUtil.GetCurrentTimeStamp().ToString(); | ||||
| 					if (setting == null) setting = DefaultEventSetting; | ||||
| 					setting.EnableGuruAnalytics = false; | ||||
| 					 | ||||
| 					SavedLogs.Enqueue(new SavedLog(key, data, setting, priority)); | ||||
| 					Log.W(TAG,$" --- LogEvent [{key}] has been cached before SDK init!"); | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					int len = SavedLogs.Count; | ||||
| 					if (len > 0) | ||||
| 					{ | ||||
| 						while (SavedLogs.Count > 0) | ||||
| 						{ | ||||
| 							var log = SavedLogs.Dequeue(); | ||||
| 							LogEvent(log.key, log.data, log.setting, log.priority); | ||||
| 						} | ||||
| 					} | ||||
| 					ConsumeAllCachedEvent(); | ||||
| 				} | ||||
| 			} | ||||
| 			catch (Exception ex) | ||||
| 			{ | ||||
| 				Crashlytics.LogException(ex); | ||||
| 				if (FirebaseUtil.IsReady) | ||||
| 				{ | ||||
| 					Crashlytics.LogException(ex); | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					Debug.Log($"Catch Error: {ex}"); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		/// <summary> | ||||
| 		/// 消耗所有的缓存日志 | ||||
| 		/// </summary> | ||||
| 		private static void ConsumeAllCachedEvent() | ||||
| 		{ | ||||
| 			// 消耗打点 | ||||
| 			int len = SavedLogs.Count; | ||||
| 			if (len <= 0) return; | ||||
| 			while (SavedLogs.Count > 0) | ||||
| 			{ | ||||
| 				var log = SavedLogs.Dequeue(); | ||||
| 				LogEvent(log.key, log.data, log.setting, log.priority); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue