You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何为Firebase in_app_purchase事件添加参数?解决动态内购场景下仅靠product_id/product_name无法区分购买的问题

How to Add Custom Parameters to Firebase in_app_purchase Events for Dynamic Purchase Scenarios

Great question—this is such a common pain point when dealing with dynamic in-app purchases where standard product_id or product_name aren't enough to distinguish unique buying behaviors. The good news is Firebase Analytics gives you full flexibility to add custom parameters to your in_app_purchase events, so you can track exactly what you need.

Here's how to implement this effectively:

While Firebase can automatically log in_app_purchase events for standard purchases, manual logging gives you full control over adding custom parameters. This is ideal for dynamic scenarios where you need to capture context-specific data tied to each unique purchase.

Example Code (Flutter, using firebase_analytics package)

When a purchase completes successfully, call the logEvent method with both standard required parameters and your custom context fields:

import 'package:firebase_analytics/firebase_analytics.dart';

Future<void> logDynamicInAppPurchase({
  required String productId,
  required double price,
  required String currency,
  required String dynamicVariantId, // Unique ID for the dynamic purchase variant
  required String purchaseContext, // e.g., "limited_time_offer", "user_level_10_upgrade", "popup_promo"
  int? userCurrentLevel, // Optional: Add user-specific context
}) async {
  await FirebaseAnalytics.instance.logEvent(
    name: 'in_app_purchase',
    parameters: {
      // Standard Firebase in-app purchase parameters (keep these for consistency)
      'product_id': productId,
      'price': price,
      'currency': currency,
      'quantity': 1,
      // Custom parameters to distinguish dynamic behaviors
      'dynamic_variant_id': dynamicVariantId,
      'purchase_trigger_context': purchaseContext,
      if (userCurrentLevel != null) 'user_level': userCurrentLevel,
    },
  );
}

Example for Native Android (Kotlin)

val firebaseAnalytics = FirebaseAnalytics.getInstance(context)
val params = Bundle().apply {
    putString("product_id", "com.yourapp.dynamic_item")
    putDouble("price", 9.99)
    putString("currency", "USD")
    // Custom parameters for dynamic context
    putString("dynamic_variant_id", "user_123_custom_offer")
    putString("purchase_trigger_context", "referral_bonus")
}
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.IN_APP_PURCHASE, params)

2. Enhancing Auto-Collected in_app_purchase Events

If you want to keep using Firebase's automatic event collection, you can still attach custom context—but this is less precise than manual logging, since auto-collected events fire before you can attach dynamic purchase details.

A better workaround is:

  • Disable automatic in_app_purchase collection in your Firebase project settings
  • Replace it with manual logging (as shown above) to ensure all custom parameters are included at the exact moment of purchase

3. Best Practices for Custom Parameters

  • Avoid reserved names: Don't use parameter names Firebase already uses for in_app_purchase (e.g., product_id, price, currency). Stick to unique, descriptive names to avoid conflicts.
  • Keep values simple: Use strings, numbers, or booleans—Firebase Analytics doesn't support nested objects or complex data structures.
  • Add to Firebase Console dimensions: Once you start logging custom parameters, head to your Firebase Console > Analytics > Events > in_app_purchase > Manage Dimensions, and add your custom parameters as dimensions. This lets you filter and segment purchase data directly in the console.
  • Be consistent: Use the same parameter names across all dynamic purchase scenarios to keep your analysis clean and actionable.

Why This Works

For dynamic purchases (like user-specific offers, limited-time variants, or context-triggered buys), these custom parameters let you:

  • Segment purchases by the scenario that triggered them (e.g., "popup promo" vs "store tab")
  • Track performance of unique dynamic variants even if they share a product_id
  • Correlate purchase behavior with user-specific context (like current level or referral source)

Content of the question comes from Stack Exchange, asked by Kaarlo Räihä

火山引擎 最新活动