diff --git a/packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart b/packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart index 0676996ca2ad..de6f0d57f630 100755 --- a/packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart +++ b/packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart @@ -1205,6 +1205,43 @@ class FirebaseAnalytics extends FirebasePluginPlatform { ); } + /// Logs the standard `in_app_purchase` event. + /// + /// This event signifies that an item(s) was purchased by a user. + /// + /// This API supports manually logging in-app purchase events on iOS and Android. + /// This is especially useful in cases where purchases happen outside the native + /// billing systems (e.g. custom payment flows). + Future logInAppPurchase({ + String? currency, + bool? freeTrial, + double? price, + bool? priceIsDiscounted, + String? productID, + String? productName, + int? quantity, + bool? subscription, + num? value, + }) { + if (defaultTargetPlatform != TargetPlatform.iOS) { + throw UnimplementedError('logInAppPurchase() is only supported on iOS.'); + } + return _delegate.logEvent( + name: 'in_app_purchase', + parameters: filterOutNulls({ + _CURRENCY: currency, + _FREE_TRIAL: freeTrial, + _PRICE: price, + _PRICE_IS_DISCOUNTED: priceIsDiscounted, + _PRODUCT_ID: productID, + _PRODUCT_NAME: productName, + _QUANTITY: quantity, + _SUBSCRIPTION: subscription, + _VALUE: value, + }), + ); + } + /// Sets the duration of inactivity that terminates the current session. /// /// The default value is 1800000 milliseconds (30 minutes). @@ -1547,3 +1584,24 @@ const String _PROMOTION_ID = 'promotion_id'; /// The name of a product promotion const String _PROMOTION_NAME = 'promotion_name'; + +/// Whether the purchase is a free trial +const String _FREE_TRIAL = 'free_trial'; + +/// The price of the item +const String _PRICE = 'price'; + +/// Whether the price is discounted +const String _PRICE_IS_DISCOUNTED = 'price_is_discounted'; + +/// The ID of the product +const String _PRODUCT_ID = 'product_id'; + +/// The name of the product +const String _PRODUCT_NAME = 'product_name'; + +/// The quantity of the product +const String _QUANTITY = 'quantity'; + +/// Whether the purchase is a subscription +const String _SUBSCRIPTION = 'subscription'; diff --git a/tests/integration_test/firebase_analytics/firebase_analytics_e2e_test.dart b/tests/integration_test/firebase_analytics/firebase_analytics_e2e_test.dart index 9b302593cbf6..f7d309a3b306 100644 --- a/tests/integration_test/firebase_analytics/firebase_analytics_e2e_test.dart +++ b/tests/integration_test/firebase_analytics/firebase_analytics_e2e_test.dart @@ -170,6 +170,27 @@ void main() { ); }); + test( + 'logInAppPurchase', + () async { + await expectLater( + FirebaseAnalytics.instance.logInAppPurchase( + currency: 'USD', + freeTrial: false, + price: 4.99, + priceIsDiscounted: false, + productID: 'com.example.product', + productName: 'Example Product', + quantity: 1, + subscription: true, + value: 4.99, + ), + completes, + ); + }, + skip: defaultTargetPlatform != TargetPlatform.iOS, + ); + test('setUserId', () async { await expectLater( FirebaseAnalytics.instance.setUserId(id: 'foo'),