diff --git a/Runtime/GuruAnalytics/Editor/Dependencies.xml b/Runtime/GuruAnalytics/Editor/Dependencies.xml index 0828d2a..3c3900c 100644 --- a/Runtime/GuruAnalytics/Editor/Dependencies.xml +++ b/Runtime/GuruAnalytics/Editor/Dependencies.xml @@ -8,18 +8,20 @@ Sample Dependencies.xml: - - - + + + - - + + - + + + diff --git a/Runtime/GuruAnalytics/Plugins/Android/U3DAnalytics-1.11.1.aar b/Runtime/GuruAnalytics/Plugins/Android/U3DAnalytics-1.11.1.aar index 4886e8c..955b391 100644 Binary files a/Runtime/GuruAnalytics/Plugins/Android/U3DAnalytics-1.11.1.aar and b/Runtime/GuruAnalytics/Plugins/Android/U3DAnalytics-1.11.1.aar differ diff --git a/Runtime/GuruAnalytics/Runtime/Script/Experiment/GuruAnalyticsExp.cs b/Runtime/GuruAnalytics/Runtime/Script/Experiment/GuruAnalyticsExp.cs index 81174a2..8ec7220 100644 --- a/Runtime/GuruAnalytics/Runtime/Script/Experiment/GuruAnalyticsExp.cs +++ b/Runtime/GuruAnalytics/Runtime/Script/Experiment/GuruAnalyticsExp.cs @@ -1,16 +1,37 @@ - - namespace Guru { - using Guru; using System; using UnityEngine; using Random = UnityEngine.Random; - using System.Collections.Generic; + using Firebase.RemoteConfig; + using System.Linq; public class GuruAnalyticsExp { - + + private const string Tag = "[SDK][ANU][EXP]"; + + private static bool IsDebug + { + get + { +#if UNITY_EDITOR || DEBUG + return true; +#endif + return false; + } + } + + private static string SavedGuruAnalyticsExpGroupId + { + get => PlayerPrefs.GetString(nameof(SavedGuruAnalyticsExpGroupId), ""); + set + { + PlayerPrefs.SetString(nameof(SavedGuruAnalyticsExpGroupId), value); + PlayerPrefs.Save(); + } + } + /** * 原始数据 private const string JSON_GROUP_B = @@ -25,7 +46,7 @@ namespace Guru /// /// /// - public static GuruAnalyticsExpData Parse(string json) + private static GuruAnalyticsExpData Parse(string json) { if (string.IsNullOrEmpty(json)) return null; return JsonParser.ToObject(json); @@ -50,24 +71,132 @@ namespace Guru }] }"; - public static GuruAnalyticsExpData DefaultData => Parse(DEFAULT_GURU_ANALYTICS_EXP); + /// + /// 获取默认数据 + /// + private static GuruAnalyticsExpData DefaultData => Parse(DEFAULT_GURU_ANALYTICS_EXP); + + + /// + /// 在当前版本中,随机获取线上配置的值 + /// 若无法获取线上配置,则默认是 B 分组 + /// + /// + /// + /// + /// + public static void GetGuruAnalyticsExpParams(out string groupId, out string baseUrl, out string[] uploadIpAddress, out bool isEnable) + { + groupId = ""; + baseUrl = ""; + uploadIpAddress = null; + isEnable = true; + string localGroup = ""; + string remoteGroup = ""; + GuruAnalyticsExpData expData = null; + bool usingLocalData = false; + + var gid = SavedGuruAnalyticsExpGroupId; + if(IsDebug) Debug.LogWarning($"{Tag} --- #0 Analytics EXP saved groupId :{gid}"); + + // 拉取云控数据 + var json = ""; + if(FirebaseUtil.IsFirebaseInitialized && FirebaseRemoteConfig.DefaultInstance.Keys.Contains(KEY_GURU_ANALYTICS_EXP)) + json = FirebaseRemoteConfig.DefaultInstance.GetValue(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP).StringValue; + + if (string.IsNullOrEmpty(json)) + { + if(IsDebug) Debug.LogWarning($"{Tag} --- #1 Analytics EXP json is Null -> using DefaultData"); + expData = DefaultData; + usingLocalData = true; + } + else + { + if(IsDebug) Debug.LogWarning($"{Tag} --- #2 Analytics EXP Try to get remote json -> {json}"); + expData = Parse(json); + usingLocalData = false; + } + + GuruAnalyticsExpConfig config = null; + if (string.IsNullOrEmpty(gid)) + { + config = expData.GetRandomConfig(); + if(IsDebug) Debug.LogWarning($"{Tag} --- #3 no group Id, get random one: id: {config?.groupId ?? "NULL"}"); + } + else + { + config = expData.GetConfig(gid); + if (config == null) + { + config = expData.GetRandomConfig(); + if(IsDebug) Debug.LogWarning($"{Tag} --- #4 Get old Config failed: {gid}, change to random: id: {config?.groupId ?? "NULL"}"); + } + } + + if (config == null) { + config = DefaultData.exps[0]; // 默认是 B 组 + if(IsDebug) Debug.LogWarning($"{Tag} --- #6 Get all config is Null -> using Default config B"); + } + + + baseUrl = config.baseUrl; + groupId = config.groupId; + uploadIpAddress = config.uploadIpAddress; + SavedGuruAnalyticsExpGroupId = groupId; + Debug.Log($"{Tag} --- Analytics EXP params:: groupId:{groupId} baseUrl:{baseUrl} uploadIpAddress:[{string.Join(",", uploadIpAddress)}]"); + } } /// /// 实验数据主题 /// - public class GuruAnalyticsExpData + internal class GuruAnalyticsExpData { public bool enable = true; // 默认是打开的状态 public GuruAnalyticsExpConfig[] exps; // 实验列表 public string ToJson() => JsonParser.ToJson(this); // 转换成 JSON 字符串 + /// + /// 获取随机分组 + /// + /// public GuruAnalyticsExpConfig GetRandomConfig() { if (exps == null || exps.Length == 0) return null; return exps[Random.Range(0, exps.Length)]; } + + /// + /// 根据分组名称获取分组 + /// + /// + /// + public GuruAnalyticsExpConfig GetConfig(string groupId) + { + foreach (var g in exps) + { + if (g.groupId == groupId) return g; + } + + return null; + } + + + /// + /// 分组是否存在 + /// + /// + /// + public bool IsGroupExists(string groupId) + { + foreach (var g in exps) + { + if (g.groupId == groupId) return true; + } + return false; + } + } /// @@ -77,7 +206,7 @@ namespace Guru { public string groupId; public string baseUrl; - public List uploadIpAddress; + public string[] uploadIpAddress; } } \ No newline at end of file diff --git a/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs b/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs index fa325f9..251f180 100644 --- a/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs +++ b/Runtime/GuruAnalytics/Runtime/Script/GuruAnalytics.cs @@ -105,7 +105,7 @@ namespace Guru /// /// /// - public static void InitWithConfig(string appId, string deviceInfo, string baseUrl, List uploadIpAddress, bool isEnable = true, bool enableErrorLog = true, bool isDebug = false) + public static void InitWithConfig(string appId, string deviceInfo, string baseUrl, string[] uploadIpAddress, bool isEnable = true, bool enableErrorLog = true, bool isDebug = false) { _enableErrorLog = enableErrorLog; #if UNITY_ANDROID diff --git a/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs b/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs index 12d1cb2..edc9bcc 100644 --- a/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs +++ b/Runtime/GuruAnalytics/Runtime/Script/Impl/AnalyticsAgentAndroid.cs @@ -88,7 +88,7 @@ namespace Guru bool useWorker = true; bool useCronet = false; string baseUrl = ""; - List uploadIpAddress = null; + string[] uploadIpAddress = null; CallSDKInit(appId, deviceInfo, bundleId, baseUrl, uploadIpAddress , useWorker, useCronet, isDebug); // 调用接口 } @@ -101,7 +101,7 @@ namespace Guru /// /// /// - public void InitWithConfig(string appId, string deviceInfo, string baseUrl, List uploadIpAddress, bool isDebug = false) + public void InitWithConfig(string appId, string deviceInfo, string baseUrl, string[]uploadIpAddress, bool isDebug = false) { _isDebug = isDebug; string bundleId = Application.identifier; @@ -125,12 +125,12 @@ namespace Guru string deviceInfo, string bundleId, string baseUrl = "", - List uploadIpAddress = null, + string[] uploadIpAddress = null, bool useWorker = true, bool useCronet = false, bool isDebug = false) { - CallStatic("init", appId, deviceInfo, bundleId, isDebug, useWorker, useCronet, baseUrl, uploadIpAddress); // 调用接口 + CallStatic("init", appId, deviceInfo, bundleId, isDebug, useWorker, useCronet, baseUrl, string.Join(",", uploadIpAddress)); // 调用接口 } diff --git a/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs b/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs index 1e76f4a..f7fa546 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs @@ -39,11 +39,13 @@ namespace Guru } - public static void InitGuruAnalyticService(string baseUrl, List uploadIpAddress, bool isEnable = true, bool isDebug = false, + public static void InitGuruAnalyticService(string baseUrl, string[] uploadIpAddress, bool isEnable = true, bool isDebug = false, bool enableErrorLog = false, string firebaseId = "") { if (_hasInited) return; + Debug.Log($"{TAG} --- InstallGuruAnalytics baseUrl: {baseUrl} enableErrorLog: {enableErrorLog} firebaseId:{firebaseId}"); + try { #if UNITY_EDITOR @@ -89,7 +91,7 @@ namespace Guru { Debug.Log($"---[ANA] UID: {IPMConfig.IPM_UID}"); GuruAnalytics.SetUid(IPMConfig.IPM_UID); - FirebaseAnalytics.SetUserProperty(PropertyUserID, IPMConfig.IPM_UID); + FirebaseSetUserProperty(PropertyUserID, IPMConfig.IPM_UID); _hasGotUid = true; } @@ -105,7 +107,7 @@ namespace Guru if (!string.IsNullOrEmpty(IPMConfig.IPM_DEVICE_ID)) { GuruAnalytics.SetDeviceId(IPMConfig.IPM_DEVICE_ID); - FirebaseAnalytics.SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); + FirebaseSetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); _hasGotDeviceId = true; } } @@ -194,6 +196,8 @@ namespace Guru /// private static void FetchFirebaseId() { + if (!IsFirebaseReady) return; + FirebaseAnalytics.GetAnalyticsInstanceIdAsync() .ContinueWithOnMainThread(task => { @@ -259,6 +263,8 @@ namespace Guru /// private static void UpdateAllValues() { + + Debug.Log($"{TAG} --- UpdateAllValues"); SetUid(); SetDeviceId(); SetAdjustId(); @@ -306,9 +312,16 @@ namespace Guru GuruAnalytics.SetUserProperty(key, value); UpdateAllValues(); // 同步所有的ID } - catch (Exception e) + catch (Exception ex) { - Crashlytics.LogException(e); + if (IsFirebaseReady) + { + Crashlytics.LogException(ex); + } + else + { + Debug.LogException(ex); + } } } @@ -328,7 +341,14 @@ namespace Guru } catch (Exception e) { - Crashlytics.LogException(e); + if (IsFirebaseReady) + { + Crashlytics.LogException(e); + } + else + { + Debug.LogWarning(e); + } } } @@ -352,7 +372,14 @@ namespace Guru } catch (Exception e) { - Crashlytics.LogException(e); + if (IsFirebaseReady) + { + Crashlytics.LogException(e); + } + else + { + Debug.LogWarning(e); + } } } diff --git a/Runtime/GuruCore/Runtime/Analytics/Analytics.cs b/Runtime/GuruCore/Runtime/Analytics/Analytics.cs index eb7db7b..3421397 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Analytics.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Analytics.cs @@ -1,6 +1,7 @@ using System.Collections; +using UnityEngine; namespace Guru { @@ -28,12 +29,14 @@ namespace Guru public static bool IsDebugMode => PlatformUtil.IsDebug(); + public static bool IsFirebaseReady => FirebaseUtil.IsFirebaseInitialized; + private static bool IsEnable { get { //Firebase服务没有初始化完成不上报打点 - if (!FirebaseUtil.IsFirebaseInitialized) + if (!IsFirebaseReady) return false; //Analytics没有初始化不上报打点 @@ -50,7 +53,7 @@ namespace Guru #region 初始化 - public static void InitAnalytics(string baseUrl = "", List uploadIpAddress = null, bool isEnable = false, bool enableErrorLog = false, string firebaseId = "") + public static void InitAnalytics(string baseUrl = "", string[] uploadIpAddress = null, bool isEnable = false, bool enableErrorLog = false, string firebaseId = "") { if (_isInited) return; _isInited = true; @@ -121,8 +124,7 @@ namespace Guru { Log.I(TAG,$"SetUserIDProperty -> userID:{userID}"); if (!IsEnable) return; - - FirebaseAnalytics.SetUserId(userID); + if (IsFirebaseReady) FirebaseAnalytics.SetUserId(userID); } /// @@ -132,13 +134,30 @@ namespace Guru { Log.I(TAG,$"SetUserProperty -> propertyName:{propertyName}, propertyValue:{propertyValue}"); - if (!IsEnable) - return; - - FirebaseAnalytics.SetUserProperty(propertyName, propertyValue); + if (!IsEnable) return; + FirebaseSetUserProperty(propertyName, propertyValue); CustomSetUserProperty(propertyName, propertyValue); } + + /// + /// Firebase 上报用户属性 + /// + /// + /// + private static void FirebaseSetUserProperty(string propertyName, string propertyValue) + { + if (IsFirebaseReady) + { + FirebaseAnalytics.SetUserProperty(propertyName, propertyValue); + } + else + { + Debug.Log($"{TAG} --- Firebase not ready, call Firebase Init first!"); + } + } + + #endregion #region 打点上报 @@ -375,7 +394,7 @@ namespace Guru } catch (Exception ex) { - Crashlytics.LogException(ex); + if(IsFirebaseReady) Crashlytics.LogException(ex); } } diff --git a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.Crashlytics.cs b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.Crashlytics.cs index 1fa140c..7db3c36 100644 --- a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.Crashlytics.cs +++ b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.Crashlytics.cs @@ -5,6 +5,8 @@ namespace Guru { public static partial class FirebaseUtil { + + public static void InitCrashlytics() { if(!string.IsNullOrEmpty(IPMConfig.IPM_UID)) @@ -17,6 +19,7 @@ namespace Guru /// public static void SetUserID(string userID) { + if (!IsFirebaseInitialized) return; Crashlytics.SetUserId(userID); } @@ -26,6 +29,7 @@ namespace Guru /// public static void SetCustomData(string key, string value) { + if (!IsFirebaseInitialized) return; Crashlytics.SetCustomKey(key, value); } @@ -34,6 +38,7 @@ namespace Guru /// public static void LogMessage(string message) { + if (!IsFirebaseInitialized) return; Crashlytics.Log(message); } @@ -42,6 +47,7 @@ namespace Guru /// public static void LogException(Exception exception) { + if (!IsFirebaseInitialized) return; Crashlytics.LogException(exception); } } diff --git a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs index f2cf0be..42e17fb 100644 --- a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs +++ b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs @@ -14,7 +14,7 @@ namespace Guru private static readonly string LOG_TAG = "Firebase"; private static bool _isDebug = false; private static bool _isReady = false; - public static bool IsReady => _isReady; + public static bool IsReady => _isReady && IsFirebaseInitialized; public static DependencyStatus DependencyStatus = DependencyStatus.UnavailableOther; public static bool IsFirebaseInitialized => DependencyStatus == DependencyStatus.Available; @@ -106,6 +106,9 @@ namespace Guru private static void InitAssetByFirebaseIdAsync() { + + Debug.Log($"[SDK] --- InitAssetByFirebaseIdAsync"); + // 异步获取 FirebaseID FirebaseAnalytics.GetAnalyticsInstanceIdAsync() .ContinueWithOnMainThread(task => @@ -115,12 +118,12 @@ namespace Guru { // 保存本地ID备份 IPMConfig.FIREBASE_ID = fid; // 保存FirebaseID - Debug.Log($"[SDk] --- Get FirebaseID: {fid}"); + Debug.Log($"[SDK] --- Get FirebaseID: {fid}"); GuruAnalytics.SetFirebaseId(fid); } else { - Debug.LogError($"[SDk] --- Fetch FirebaseID failed on start!"); + Debug.LogError($"[SDK] --- Fetch FirebaseID failed on start!"); } //--- 结束后启动相关的服务 --- @@ -157,7 +160,7 @@ namespace Guru var enableErrorLog = true; // 开启日志上报 // 随机抽取本地分组获取数据 - GetGuruAnalyticsExpConfig(out string groupId, out string baseUrl, out List uploadIpAddress, out bool isEnable); + GuruAnalyticsExp.GetGuruAnalyticsExpParams(out string groupId, out string baseUrl, out string[] uploadIpAddress, out bool isEnable); // 自打点启动前,先上报一下分数数据 GuruAnalytics.SetUserProperty(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, groupId); // 设置实验分组 @@ -178,45 +181,14 @@ namespace Guru // 添加默认的云控参数 private static void InitGuruAnalyticsExpDefaultRemoteValue() { - AppendDefaultValue(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, GuruAnalyticsExp.DEFAULT_GURU_ANALYTICS_EXP); + // AppendDefaultValue(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, GuruAnalyticsExp.DEFAULT_GURU_ANALYTICS_EXP); + FirebaseRemoteConfig.DefaultInstance.SetDefaultsAsync(new Dictionary() + { + { GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, GuruAnalyticsExp.DEFAULT_GURU_ANALYTICS_EXP } + }); } - /// - /// 在当前版本中,随机获取线上配置的值 - /// 若无法获取线上配置,则默认是 B 分组 - /// - /// - /// - /// - /// - private static void GetGuruAnalyticsExpConfig(out string groupId, out string baseUrl, out List uploadIpAddress, out bool isEnable) - { - groupId = ""; - baseUrl = ""; - uploadIpAddress = null; - isEnable = true; - - // 如果云测关闭 - var json = FirebaseRemoteConfig.DefaultInstance.GetValue(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP).StringValue; - var expData = GuruAnalyticsExp.Parse(json); - var defaultExpData = GuruAnalyticsExp.DefaultData; - - GuruAnalyticsExpConfig config = null; - if (expData == null) - { - config = defaultExpData.exps[0]; // 默认是 B 组 - } - else - { - config = expData.GetRandomConfig(); - if (config == null) config = defaultExpData.exps[0]; // 默认是 B 组 - } - - baseUrl = config.baseUrl; - groupId = config.groupId; - uploadIpAddress = config.uploadIpAddress; - Debug.Log($"[SDK][ANU] --- Analytics EXP Group: {groupId} baseUrl:{baseUrl} uploadIpAddress:[{string.Join(",", uploadIpAddress)}]"); - } + #endif #endregion diff --git a/Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs b/Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs index ef0017b..e6bdfbe 100644 --- a/Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs +++ b/Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs @@ -101,7 +101,7 @@ namespace Guru public static void LogException(Exception ex) { - if (!_isReady) + if (!_isReady || !FirebaseUtil.IsReady) { Install(); _expCache.Enqueue(ex); @@ -119,7 +119,7 @@ namespace Guru public static void Log(string msg) { if (!_isReady) return; - Crashlytics.Log(msg); + if(FirebaseUtil.IsReady) Crashlytics.Log(msg); // CheckSetUser(); } @@ -127,7 +127,7 @@ namespace Guru public static void SetCustomKey(string key, string value) { if (!_isReady) return; - Crashlytics.SetCustomKey(key, value); + if(FirebaseUtil.IsReady) Crashlytics.SetCustomKey(key, value); }