Update: Firebase Analytics升级为GA4,已经正式发布,可以使用,详细的参考: iOS APP里集成Google Analytics 4
在移动互联网时代,用户行为数据是驱动产品优化和营销决策的核心。Firebase Analytics(现 GA4 for Firebase)提供了一套完整的移动端数据采集、分析和用户洞察工具。本文将详细介绍如何在 App 中集成 Firebase Analytics,实现数据驱动的产品和营销闭环。
Firebase Analytics 简介
Firebase Analytics 是 Google Firebase 平台的一部分,也是 GA4 在移动端的实现。它的核心功能包括:
- 自动收集用户行为事件(如 App 打开、页面浏览、应用内购买等)
- 自定义事件和用户属性采集
- 与 Firebase 生态系统其他工具(Crashlytics、Cloud Messaging、Remote Config)无缝集成
- 支持数据导出到 BigQuery,用于高级分析和营销自动化
- 与 Google Ads、Display & Video 360 等广告平台打通,实现精准再营销
延伸阅读:Firebase详细介绍:移动与Web应用的全栈开发与分析平台
集成 Firebase Analytics 的前置条件
- Firebase 项目
- 在 Firebase 控制台创建项目
- 获取
GoogleService-Info.plist(iOS)或google-services.json(Android)
- App 开发环境
- iOS:Xcode 12+,Swift/Objective-C
- Android:Android Studio 4+,Kotlin/Java
- 依赖库和工具
- iOS:
Firebase/Core、Firebase/Analytics - Android:
com.google.firebase:firebase-analytics
- iOS:
- Google 帐号权限
- 用于访问 Firebase 控制台和 BigQuery 导出
在 Android 中集成 Firebase Analytics
添加依赖
在 build.gradle 文件中添加 Firebase Analytics SDK:
dependencies {
implementation platform('com.google.firebase:firebase-bom:34.3.0')
implementation 'com.google.firebase:firebase-analytics'
}
添加配置文件
将 google-services.json 放入 app/ 目录,并在 build.gradle 添加:
apply plugin: 'com.google.gms.google-services'
初始化 Firebase
通常在 Application 类中初始化:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
FirebaseApp.initializeApp(this)
}
}
自动事件收集
Firebase Analytics 会自动收集常用事件,如:
first_open:首次打开应用session_start:会话开始screen_view:页面浏览
自定义事件
例如用户点击购买按钮:
val analytics = Firebase.analytics
val bundle = Bundle()
bundle.putString("product_id", "123")
analytics.logEvent("add_to_cart", bundle)
用户属性
可以设置用户属性,用于分群和分析:
analytics.setUserProperty("membership_level", "gold")
在 iOS 中集成 Firebase Analytics
添加依赖
通过 CocoaPods 安装 Firebase Analytics:
pod 'Firebase/Analytics'
添加配置文件
将 GoogleService-Info.plist 拖入 Xcode 项目,并确保 Target 中包含。
初始化 Firebase
在 AppDelegate 中:
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
}
自动事件
Firebase Analytics 会自动采集:
first_open、session_start、screen_view- 设备信息、地理位置、OS 版本等
自定义事件
例如添加购物车事件:
Analytics.logEvent("add_to_cart", parameters: [
"product_id": "123" as NSObject
])
用户属性
Analytics.setUserProperty("vip_status", forName: "membership_level")
集成关键注意点
- 事件命名规范
- 使用小写字母、下划线分隔
- 避免使用保留关键字(如
purchase、login)冲突
- 避免频繁触发
- 高频事件可能导致采样或延迟
- 对于用户行为短时高频操作,可以批量或去重上报
- 区分自动与自定义事件
- 自动事件:无需开发者干预,Firebase 内置
- 自定义事件:业务特定事件,需要开发者显式调用
- 与其他 Firebase 服务结合
- Crashlytics:追踪事件异常
- Remote Config:根据用户属性动态调整体验
- Cloud Messaging:触发推送通知



