以下是我们在 I/O 大会上宣布的所有内容,从新的 Firebase Studio 功能到集成 AI 的更多方式,内容非常丰富。
阅读博客。
衡量应用内购买
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
应用内购买 (IAP) 是可以通过 Google Play 或 Apple App Store 在移动应用中销售的数字内容或功能,这样您的应用就无需处理财务交易。应用内购买的示例包括订阅内容和特殊的游戏棋子。
Analytics 在应用内购买报告中显示应用内购买事件。
对于 Android 应用,Analytics SDK 与 Google Play 集成。对于 Apple 平台应用,该 SDK 使用 Apple 的 StoreKit 1 和 StoreKit 2 API 与 Apple App Store 集成。
在大多数情况下,Analytics SDK 会自动收集应用内购买事件,而无需在应用中进行 API 调用。本指南介绍了如何设置项目以进行自动跟踪,还介绍了一些需要添加几行代码才能实现的特殊情况。
准备工作
如果您正在开发一款 Android 应用,可以在关联到 Google Play 后立即衡量应用内购买事件。本指南的其余部分重点介绍了 Apple 平台应用。
如果您是 Apple 开发者,请参阅 Apple 文档,确保您熟悉 Apple StoreKit 1 和 StoreKit 2 应用内购买 API。
实现
Swift
如果您使用的是 StoreKit 1,则 Analytics SDK 会自动记录应用内购买事件。
如果您使用的是 StoreKit 2,请使用以下代码记录应用内购买事件。
import StoreKit
import FirebaseAnalytics
// A user tapped a button to purchase an item.
func userTappedPurchaseUpgradeButton() {
let product = ...
purchaseSomeProduct(product)
}
func purchaseSomeProduct(_ product: Product) {
// Purchase a Product. This is mostly standard boilerplate StoreKit 2
// code, except for the Analytics.logTransaction() call.
let result = try await product.purchase()
switch result {
case .success(let verification):
let transaction = try checkVerified(verification)
// Call this Firebase API to log the in-app purchase event.
Analytics.logTransaction(transaction)
await transaction.finish()
...
}
Objective-C
如果您使用的是 StoreKit 1,则 Analytics SDK 会自动记录应用内购买事件。
StoreKit 2 仅适用于 Swift,因此不支持 Objective-C 实现。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-18。
[null,null,["最后更新时间 (UTC):2025-08-18。"],[],[],null,["In-app purchases (IAP) are digital content or features that you can sell in a\nmobile app through Google Play or the Apple App Store so your app doesn't\nhave to process financial transactions. Examples of in-app purchases include\nsubscription-based content or special game pieces.\n\nAnalytics shows IAP events in the\n[In-app purchases report](https://support.google.com/analytics/answer/12923440).\n\nFor Android apps, the Analytics SDK integrates with Google Play. For Apple\nplatform apps, the SDK integrates with the Apple App Store using the\nStoreKit 1 and StoreKit 2 APIs from Apple.\n\nIn most cases, the Analytics SDK automatically collects IAP events without\nrequiring API calls in your app. This guide explains how to set up your project\nfor automatic tracking, and it describes some special cases that require a few\nlines of code to implement.\n\nBefore you begin\n\n- Set up your Firebase project and your app's codebase as described in\n [Get Started with Google Analytics](/docs/analytics/get-started).\n\n- [Link your Firebase project to a Google Analytics 4 property.](https://support.google.com/firebase/answer/9289399)\n\n- For Apple platform apps:\n\n - Make sure that your app is using the Analytics SDK v6.20.0+.\n- For Android apps:\n\n - Make sure that your app is using the Analytics SDK v17.3.0+ (or Firebase Android BoM v25.2.0+).\n - [Link your Firebase apps to Google Play](https://support.google.com/firebase/answer/6392038).\n\nIf you're developing an Android app, you can measure IAP events as soon as you\nlink to Google Play. The remainder of this guide is focused on Apple platform\napps.\n\nIf you're an Apple developer, make sure you're familiar with the Apple StoreKit\n1 and StoreKit 2 in-app purchase APIs by reviewing the\n[Apple documentation](https://developer.apple.com/documentation/storekit/in-app_purchase).\n\nImplementation \n\nSwift\n\nIf you're using StoreKit 1, the Analytics SDK automatically logs\nIAP events.\n\nIf you're using StoreKit 2, use the following code to log IAP events. \n\n```swift\nimport StoreKit\nimport FirebaseAnalytics\n\n// A user tapped a button to purchase an item.\nfunc userTappedPurchaseUpgradeButton() {\n let product = ...\n purchaseSomeProduct(product)\n}\n\nfunc purchaseSomeProduct(_ product: Product) {\n // Purchase a Product. This is mostly standard boilerplate StoreKit 2\n // code, except for the Analytics.logTransaction() call.\n let result = try await product.purchase()\n switch result {\n case .success(let verification):\n let transaction = try checkVerified(verification)\n\n // Call this Firebase API to log the in-app purchase event.\n Analytics.logTransaction(transaction)\n\n await transaction.finish()\n ...\n}\n```\n\nObjective-C\n\nIf you're using StoreKit 1, the Analytics SDK automatically logs\nIAP events.\n\nStoreKit 2 is Swift-only, so an Objective-C implementation is not supported.\n\nKotlin\n\nTo log IAP events,\n[link to Google Play](//support.google.com/firebase/answer/6392038).\n\nJava\n\nTo log IAP events,\n[link to Google Play](//support.google.com/firebase/answer/6392038)."]]