Firebase Analytics无数据及实时数据异常问题求助
Firebase Analytics 数据异常排查请求
我已按照Firebase Unity集成指南完成Analytics集成,初始化无报错,Firebase Auth(Google Play Games登录)功能正常,但Analytics存在以下异常:
- 实时数据不稳定:启动游戏仅显示
session_start事件,无每分钟活跃用户数据;「最近30分钟用户」仅记录1分钟(测试时全程打开应用) - 次日常规Analytics视图无数据(测试期间全程联网)
- 实时概览偶尔缺失事件条目
附首个场景空GameObject上的初始化代码:
using UnityEngine; using System.Collections; using Firebase; using Firebase.Extensions; #if UNITY_ANDROID using GooglePlayGames; #endif public class FirebaseInitializer : MonoBehaviour { private FirebaseApp app; private bool firebaseInitialized; void Awake() { //Initialize PlayGamesPlatform #if UNITY_ANDROID PlayGamesPlatform.DebugLogEnabled = true; PlayGamesPlatform.Activate(); #endif } // Start is called before the first frame update IEnumerator Start() { InitializeFirebaseAndStart(); while (!firebaseInitialized) { yield return null; } Social.localUser.Authenticate((bool success) => { print("Auth " + success); Firebase.Analytics.FirebaseAnalytics .LogEvent(Firebase.Analytics.FirebaseAnalytics.EventLogin); PlayGamesPlatform.Instance.RequestServerSideAccess(/* forceRefreshToken= */ false, authCode => { Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode); auth.SignInWithCredentialAsync(credential).ContinueWith(task => { if (task.IsCanceled) { Debug.LogError("SignInWithCredentialAsync was canceled."); return; } if (task.IsFaulted) { Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception); return; } Firebase.Auth.FirebaseUser newUser = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); }); }); }); Firebase.Analytics.FirebaseAnalytics .LogEvent(Firebase.Analytics.FirebaseAnalytics.EventAppOpen); yield break; } // When the app starts, check to make sure that we have // the required dependencies to use Firebase, and if not, // add them if possible. void InitializeFirebaseAndStart() { FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => { var dependencyStatus = task.Result; if (dependencyStatus == DependencyStatus.Available) { app = FirebaseApp.DefaultInstance; firebaseInitialized = true; } else { Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus); } }); } }
该对象还附加了DoNotDestroy脚本:
public class DoNotDestroy : MonoBehaviour { private static DoNotDestroy _instance; // Start is called before the first frame update void Awake() { if (_instance == null) { _instance = this; } else { Destroy(this.gameObject); } DontDestroyOnLoad(transform.gameObject); } }
调试步骤建议
1. 验证事件发送日志
- 开启Analytics调试模式:在Android设备执行命令
adb shell setprop debug.firebase.analytics.app 你的应用包名,重启应用后查看logcat中FirebaseAnalytics标签的日志,确认app_open、login等事件是否有Event recorded或Upload scheduled的记录 - 检查数据收集开关:确保代码中未调用
FirebaseAnalytics.SetAnalyticsCollectionEnabled(false),或在初始化完成后才启用收集
2. 确认Session跟踪逻辑
- Firebase默认session超时为30分钟,但应用退后台超过1分钟会自动结束session。检查测试过程中应用是否意外进入后台(如系统弹窗、资源限制)
- 手动测试session切换:将应用退后台等待1分钟以上再切回,查看实时数据是否触发新的
session_start事件
3. 排查数据上报延迟与网络问题
- 常规Analytics报表(非实时)需要24小时左右完成数据处理,确认次日查看的时间是否足够
- 验证网络连通性:确保测试设备网络稳定,无代理或防火墙拦截
app-measurement.com的HTTPS请求
4. 检查项目配置与依赖
- 确认
google-services.json文件正确放置在Assets/Plugins/Android目录,且包名、项目ID与Firebase控制台完全一致 - 升级Firebase SDK到最新稳定版,确保Analytics与Auth SDK版本兼容
- 验证DoNotDestroy逻辑:在Update方法中打印
FirebaseApp.DefaultInstance是否为null,确认Firebase服务未被意外销毁
5. 检查数据过滤与DebugView
- 查看Firebase控制台Analytics设置,确认未开启「排除内部流量」或测试设备未被加入排除列表
- 使用Firebase DebugView实时监控事件:选择测试设备,确认所有触发的事件都能被捕获,这是验证上报最直接的方式
内容的提问来源于stack exchange,提问作者Mateusz Cicheński
相关产品推荐
相关产品推荐

