# Revenue

> Track in-app purchases and custom payments with the Grovs Flutter SDK.

Source: https://docs.grovs.io/docs/sdk/flutter/revenue

Log purchases to attribute revenue to links and campaigns, and see ARPU, LTV and product and platform breakdowns in the dashboard. Revenue tracking is in **beta**.

Self-hosted: revenue tracking needs the Enterprise edition (`GROVS_EE=true`). A Community edition backend has no purchase endpoint.

## Setup

### 1. Enable revenue tracking in the dashboard

1. In the [Grovs dashboard](https://app.grovs.io), go to **Analytics → Revenue**.
2. Click **Enable Revenue Tracking**. Only instance admins can turn it on or off.

See [Revenue](https://docs.grovs.io/docs/revenue) for the dashboard and [Goals](https://docs.grovs.io/docs/revenue/goals) for revenue goals.

### 2. Configure platform notifications

- **Android:** set up Google Play Real-Time Developer Notifications. See the [Android Revenue guide](https://docs.grovs.io/docs/sdk/android/revenue#2-configure-google-play-real-time-notifications).
- **iOS:** configure App Store Server Notifications in [App Store Connect](https://appstoreconnect.apple.com). See the [iOS Revenue guide](https://docs.grovs.io/docs/sdk/ios/revenue#2-configure-app-store-server-notifications).

## Logging purchases

### Platform store purchases

Pass the platform's transaction identifier as a string. The plugin routes it to the current platform's native SDK:

```dart
import 'dart:io' show Platform;

import 'package:grovs_flutter_plugin/grovs.dart';

final grovs = Grovs();

if (Platform.isIOS) {
  // The StoreKit 2 transaction ID, as a decimal string
  await grovs.logInAppPurchase(transaction.id.toString());
} else {
  // The Play Billing purchase's originalJson
  await grovs.logInAppPurchase(purchase.originalJson);
}
```

- **iOS:** the string must parse as an unsigned 64-bit integer, or a `GrovsException` with code `INVALID_ARGUMENT` is thrown. If the native SDK can't log the purchase, the code is `PAYMENT_ERROR`.
- **Android:** the string goes to the native SDK as-is. `PAYMENT_ERROR` is thrown only if the native call throws.

The backend then looks the transaction up, fills in product, price and currency, and de-duplicates repeated calls. See [iOS Revenue](https://docs.grovs.io/docs/sdk/ios/revenue#app-store-purchases-storekit-2) and [Android Revenue](https://docs.grovs.io/docs/sdk/android/revenue#google-play-purchases).

### Custom purchases

For purchases through Stripe, PayPal or any non-store system:

```dart
import 'package:grovs_flutter_plugin/models/grovs_link.dart';

await grovs.logCustomPurchase(
  type: TransactionType.buy,
  priceInCents: 999,       // $9.99
  currency: 'USD',
  productId: 'premium_monthly',
);
```

**Parameters:**

| Parameter      | Type              | Description                                                                                                           |
| -------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------- |
| `type`         | `TransactionType` | `buy`, `cancel` or `refund`                                                                                           |
| `priceInCents` | `int`             | Amount in cents (for example 999 for $9.99)                                                                           |
| `currency`     | `String`          | ISO 4217 currency code (for example `'USD'`, `'EUR'`)                                                                 |
| `productId`    | `String`          | Your product identifier                                                                                               |
| `startDate`    | `DateTime?`       | Transaction date, sent as milliseconds since the epoch. Honored on both platforms since plugin 3.0.0. Defaults to now |

### Tracking cancellations and refunds

```dart
// Log a cancellation
await grovs.logCustomPurchase(
  type: TransactionType.cancel,
  priceInCents: 999,
  currency: 'USD',
  productId: 'premium_monthly',
);

// Log a refund
await grovs.logCustomPurchase(
  type: TransactionType.refund,
  priceInCents: 999,
  currency: 'USD',
  productId: 'premium_monthly',
);
```

App Store and Google Play cancellations and refunds are detected automatically through the platform server notifications.

## While the SDK is disabled

Purchases aren't sent while disabled with `setSDK(false)`. What the native SDKs hold or drop: [Privacy & Consent](https://docs.grovs.io/docs/sdk/flutter/privacy).
