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
|
public class GuruAnalytics
|
||||||
{
|
{
|
||||||
// Plugin Version
|
// Plugin Version
|
||||||
public const string Version = "1.10.5";
|
public 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";
|
||||||
|
|
@ -60,8 +60,6 @@ namespace Guru
|
||||||
/// 错误 code 表
|
/// 错误 code 表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static List<int> ErrorCodeList = new List<int>();
|
public static List<int> ErrorCodeList = new List<int>();
|
||||||
|
|
||||||
private static bool _autoSyncProperties = false;
|
|
||||||
private static bool _enableErrorLog = false;
|
private static bool _enableErrorLog = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -84,11 +82,9 @@ namespace Guru
|
||||||
/// 初始化接口
|
/// 初始化接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Init(string appId, string deviceInfo, bool isDebug = false,
|
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...");
|
Debug.Log($"{Tag} --- Guru Analytics [{Version}] initialing...");
|
||||||
|
|
||||||
_autoSyncProperties = syncProperties;
|
|
||||||
_enableErrorLog = enableErrorLog;
|
_enableErrorLog = enableErrorLog;
|
||||||
Agent?.Init(appId, deviceInfo, isDebug);
|
Agent?.Init(appId, deviceInfo, isDebug);
|
||||||
if(_enableErrorLog) InitCallbacks(); // 激活错误日志回调
|
if(_enableErrorLog) InitCallbacks(); // 激活错误日志回调
|
||||||
|
|
@ -204,16 +200,13 @@ namespace Guru
|
||||||
/// <param name="priority"></param>
|
/// <param name="priority"></param>
|
||||||
public static void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1)
|
public static void LogEvent(string eventName, Dictionary<string, dynamic> data = null, int priority = -1)
|
||||||
{
|
{
|
||||||
if(_autoSyncProperties)
|
|
||||||
UpdateAllUserProperties(); // 每次打点更新用户属性
|
|
||||||
|
|
||||||
string raw = "";
|
string raw = "";
|
||||||
if (data != null && data.Count > 0)
|
if (data != null && data.Count > 0)
|
||||||
{
|
{
|
||||||
raw = BuildParamsJson(data);
|
raw = BuildParamsJson(data);
|
||||||
}
|
}
|
||||||
if (priority < 0) priority = EventPriorityDefault;
|
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);
|
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
|
namespace Guru
|
||||||
{
|
{
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
@ -30,6 +32,21 @@ namespace Guru
|
||||||
Debug.Log($"{Tag} Consent Request -> deviceid: {deviceId} debugGeography: {debugGeography}");
|
Debug.Log($"{Tag} Consent Request -> deviceid: {deviceId} debugGeography: {debugGeography}");
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#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);
|
string msg = callbackMsgFmt.Replace("$0", $"{DebugStatusCode}").Replace("$1",DebugMessage);
|
||||||
var go = GameObject.Find(_objName);
|
var go = GameObject.Find(_objName);
|
||||||
if (go != null)
|
if (go != null)
|
||||||
|
|
@ -40,9 +57,10 @@ namespace Guru
|
||||||
{
|
{
|
||||||
Debug.LogError($"{Tag} Can't find callback object");
|
Debug.LogError($"{Tag} Can't find callback object");
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 DMA 字段
|
/// 获取 DMA 字段
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,30 @@ namespace Guru
|
||||||
{
|
{
|
||||||
public class EventSetting
|
public class EventSetting
|
||||||
{
|
{
|
||||||
public bool EnableFirebaseAnalytics = false;
|
public bool EnableFirebaseAnalytics;
|
||||||
public bool EnableAdjustAnalytics = false;
|
public bool EnableAdjustAnalytics;
|
||||||
public bool EnableFacebookAnalytics = false;
|
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是否初始化完成
|
private static bool _isInited; //Analytics是否初始化完成
|
||||||
public static bool EnableDebugAnalytics; //允许Debug包上报打点
|
public static bool EnableDebugAnalytics; //允许Debug包上报打点
|
||||||
|
|
||||||
|
|
@ -32,18 +49,19 @@ namespace Guru
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
//Analytics没有初始化不上报打点
|
||||||
|
if (!_isInited)
|
||||||
|
return false;
|
||||||
|
|
||||||
//Firebase服务没有初始化完成不上报打点
|
//Firebase服务没有初始化完成不上报打点
|
||||||
if (!FirebaseUtil.IsFirebaseInitialized)
|
if (!FirebaseUtil.IsFirebaseInitialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//Analytics没有初始化不上报打点
|
#if !UNITY_EDITOR
|
||||||
if (!_isInited)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
//开发环境打点不上报
|
//开发环境打点不上报
|
||||||
if (IsDebugMode && !EnableDebugAnalytics)
|
if (IsDebugMode && !EnableDebugAnalytics)
|
||||||
return false;
|
return false;
|
||||||
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -60,18 +78,7 @@ namespace Guru
|
||||||
|
|
||||||
// ------- 初始化自打点 ----------
|
// ------- 初始化自打点 ----------
|
||||||
InstallGuruAnalytics(IsDebug);
|
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;
|
FirebaseUtil.onInitComplete += OnFirebaseCompleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,17 +86,20 @@ namespace Guru
|
||||||
{
|
{
|
||||||
FirebaseUtil.onInitComplete -= OnFirebaseCompleted;
|
FirebaseUtil.onInitComplete -= OnFirebaseCompleted;
|
||||||
|
|
||||||
|
Debug.Log($"[SDK] --- Analytics Init After FirebaseCompleted: {success}");
|
||||||
|
|
||||||
|
ConsumeAllCachedEvent();
|
||||||
|
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
Crashlytics.IsCrashlyticsCollectionEnabled = true;
|
Crashlytics.IsCrashlyticsCollectionEnabled = true;
|
||||||
if (_defaultEventSetting.EnableFirebaseAnalytics)
|
|
||||||
{
|
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
|
||||||
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
|
FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
|
||||||
FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
|
SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google");
|
||||||
SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google");
|
SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID);
|
||||||
SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID);
|
// SetUserProperty(PropertyFirstOpenTime, FirstOpenTime);
|
||||||
// SetUserProperty(PropertyFirstOpenTime, FirstOpenTime);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -150,13 +160,17 @@ namespace Guru
|
||||||
/// <param name="eventSetting"></param>
|
/// <param name="eventSetting"></param>
|
||||||
internal static void LogEvent(string eventName, EventSetting eventSetting = null, int priority = -1)
|
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缓存和消费
|
CheckLogCache(eventName, null, eventSetting); // log缓存和消费
|
||||||
|
|
||||||
if (!IsEnable) return;
|
if (!IsEnable) return;
|
||||||
|
|
||||||
eventSetting ??= _defaultEventSetting;
|
Log.I(TAG, $" --- logEvent:{eventName}, eventSetting:{eventSetting}");
|
||||||
|
|
||||||
if (eventSetting.EnableFirebaseAnalytics)
|
if (eventSetting.EnableFirebaseAnalytics)
|
||||||
{
|
{
|
||||||
|
|
@ -183,21 +197,24 @@ namespace Guru
|
||||||
/// <param name="priority"></param>
|
/// <param name="priority"></param>
|
||||||
internal static void LogEvent(string eventName, Dictionary<string, dynamic> extras, EventSetting eventSetting = null, int priority = -1)
|
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缓存和消费
|
CheckLogCache(eventName, extras, eventSetting); // log缓存和消费
|
||||||
|
|
||||||
if (!IsEnable) return;
|
if (!IsEnable) return;
|
||||||
|
|
||||||
if (extras == null)
|
if (extras == null)
|
||||||
{
|
{
|
||||||
LogEvent(eventName, eventSetting, priority); // 防空判定
|
LogEvent(eventName, eventSetting, priority); // 防空判定
|
||||||
return;
|
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)
|
if (eventSetting.EnableFirebaseAnalytics)
|
||||||
{
|
{
|
||||||
List<Parameter> parameters = new List<Parameter>();
|
List<Parameter> parameters = new List<Parameter>();
|
||||||
|
|
@ -356,28 +373,47 @@ namespace Guru
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!_isInited)
|
if (!IsEnable)
|
||||||
{
|
{
|
||||||
|
// 保存打点
|
||||||
if (data == null) data = new Dictionary<string, dynamic>();
|
if (data == null) data = new Dictionary<string, dynamic>();
|
||||||
data["log_stamp"] = TimeUtil.GetCurrentTimeStamp().ToString();
|
data["log_stamp"] = TimeUtil.GetCurrentTimeStamp().ToString();
|
||||||
|
if (setting == null) setting = DefaultEventSetting;
|
||||||
|
setting.EnableGuruAnalytics = false;
|
||||||
|
|
||||||
SavedLogs.Enqueue(new SavedLog(key, data, setting, priority));
|
SavedLogs.Enqueue(new SavedLog(key, data, setting, priority));
|
||||||
|
Log.W(TAG,$" --- LogEvent [{key}] has been cached before SDK init!");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int len = SavedLogs.Count;
|
ConsumeAllCachedEvent();
|
||||||
if (len > 0)
|
|
||||||
{
|
|
||||||
while (SavedLogs.Count > 0)
|
|
||||||
{
|
|
||||||
var log = SavedLogs.Dequeue();
|
|
||||||
LogEvent(log.key, log.data, log.setting, log.priority);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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