Skip to content

Commit 426f1da

Browse files
committed
feat(firebase_analytics): add logInAppPurchase support for iOS and Android
1 parent 7113857 commit 426f1da

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,40 @@ class FirebaseAnalytics extends FirebasePluginPlatform {
12051205
);
12061206
}
12071207

1208+
/// Logs the standard `in_app_purchase` event.
1209+
///
1210+
/// This event signifies that an item(s) was purchased by a user.
1211+
///
1212+
/// This API supports manually logging in-app purchase events on iOS and Android.
1213+
/// This is especially useful in cases where purchases happen outside the native
1214+
/// billing systems (e.g. custom payment flows).
1215+
Future<void> logInAppPurchase({
1216+
String? currency,
1217+
bool? freeTrial,
1218+
double? price,
1219+
bool? priceIsDiscounted,
1220+
String? productID,
1221+
String? productName,
1222+
int? quantity,
1223+
bool? subscription,
1224+
num? value,
1225+
}) {
1226+
return _delegate.logEvent(
1227+
name: 'in_app_purchase',
1228+
parameters: filterOutNulls(<String, Object?>{
1229+
_CURRENCY: currency,
1230+
_FREE_TRIAL: freeTrial,
1231+
_PRICE: price,
1232+
_PRICE_IS_DISCOUNTED: priceIsDiscounted,
1233+
_PRODUCT_ID: productID,
1234+
_PRODUCT_NAME: productName,
1235+
_QUANTITY: quantity,
1236+
_SUBSCRIPTION: subscription,
1237+
_VALUE: value,
1238+
}),
1239+
);
1240+
}
1241+
12081242
/// Sets the duration of inactivity that terminates the current session.
12091243
///
12101244
/// The default value is 1800000 milliseconds (30 minutes).
@@ -1547,3 +1581,24 @@ const String _PROMOTION_ID = 'promotion_id';
15471581

15481582
/// The name of a product promotion
15491583
const String _PROMOTION_NAME = 'promotion_name';
1584+
1585+
/// Whether the purchase is a free trial
1586+
const String _FREE_TRIAL = 'free_trial';
1587+
1588+
/// The price of the item
1589+
const String _PRICE = 'price';
1590+
1591+
/// Whether the price is discounted
1592+
const String _PRICE_IS_DISCOUNTED = 'price_is_discounted';
1593+
1594+
/// The ID of the product
1595+
const String _PRODUCT_ID = 'product_id';
1596+
1597+
/// The name of the product
1598+
const String _PRODUCT_NAME = 'product_name';
1599+
1600+
/// The quantity of the product
1601+
const String _QUANTITY = 'quantity';
1602+
1603+
/// Whether the purchase is a subscription
1604+
const String _SUBSCRIPTION = 'subscription';

packages/firebase_analytics/firebase_analytics_web/lib/firebase_analytics_web.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class FirebaseAnalyticsWeb extends FirebaseAnalyticsPlatform {
7070
Map<String, Object?>? parameters,
7171
AnalyticsCallOptions? callOptions,
7272
}) async {
73+
if (name == 'in_app_purchase') {
74+
throw UnimplementedError('in_app_purchase() is not supported on Web.');
75+
}
7376
return convertWebExceptions(() {
7477
return _delegate.logEvent(
7578
name: name,

0 commit comments

Comments
 (0)