如何在自定义Firebase分析事件中发送数组



我在项目中使用Firebase Analytics。我创建了包含项目数组的自定义事件。在DebugView中,我收到错误代码21。此错误代码表示事件不支持数组参数。那么,有什么方法可以解决这个错误吗?

要发送自定义模型/对象的数组,您需要将自定义模型分解为Bundle对象,稍后您必须创建该Bundle的数组。然后你可以在另一个bundle对象中添加bundle的数组&在活动中传递它。

您可以参考以下示例:

在这里,我需要传递火球事件中的产品数组:

data class Product(
val productId: String = "",
val name: String = "",
val coupon: String = "",
val currency: String = "",
val discount: Double = 0.0,
val brand: String = "",
val category: String = "",
val price: Double = 0.0,
val quantity: Int = 0)

像这样->Array<Product>

为此,您可以将您的单个产品分解为如下捆绑包:

val itemBundle = Bundle()
itemBundle.putString("item_id", product.productId)
itemBundle.putString("item_name", product.name)
itemBundle.putString("item_category", product.category)
itemBundle.putString("item_brand", product.brand)
itemBundle.putString("coupon", "")
itemBundle.putString("currency", product.currency)
itemBundle.putInt("price", product.price.toInt())
itemBundle.putInt("quantity", product.quantity)

稍后,您可以将其添加到最终的事件捆绑包中,如下所示:

val eventBundle = Bundle().apply {
this.putParcelableArray("items", arrayOf(itemBundle))
}

然后记录您的活动

mFirebaseAnalytics.logEvent("your_event_name", eventBundle)

希望这能有所帮助!

最新更新