# Revenue

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

Source: https://docs.grovs.io/docs/sdk/react-native/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**.

## 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). The [iOS Revenue guide](https://docs.grovs.io/docs/sdk/ios/revenue#2-configure-app-store-server-notifications) lists the URLs.

## Logging purchases

### Platform store purchases

`logInAppPurchase` takes an object with one platform-specific field. Each platform ignores the other's:

- **iOS:** `transactionId`, the StoreKit 2 transaction ID as a decimal string.
- **Android:** `originalJson`, the Google Play Billing purchase's `originalJson` string.

```typescript
import { Platform } from 'react-native';
import Grovs from 'react-native-grovs-wrapper';

try {
    const success = await Grovs.logInAppPurchase(
        Platform.OS === 'ios'
            ? { transactionId: transaction.id }        // StoreKit 2 transaction ID
            : { originalJson: purchase.originalJson }   // Play Billing purchase JSON
    );
    console.log('Purchase tracked:', success);
} catch (error) {
    console.error('Error tracking purchase:', error);
}
```

The wrapper rejects with an error if the current platform's field is missing, or if `transactionId` isn't a numeric string on iOS. The resolved boolean differs per platform:

- **iOS:** `false` means the native SDK didn't accept the purchase: the ID isn't a 64-bit integer, or the device runs iOS 14 or earlier (StoreKit 2 needs iOS 15). Only the ID is sent; the backend resolves product, price and currency. An ID already logged on the device is skipped.
- **Android:** `true` as soon as the JSON reaches the native SDK; it doesn't confirm backend acceptance. The SDK reads the product ID and purchase token; the backend verifies the purchase, fills in price and currency and de-duplicates on purchase token.

Log each purchase once, right after it completes. (In wrapper 1.x this method took a plain string; see the [3.0 upgrade guide](https://docs.grovs.io/docs/upgrade-guides/react-native-3-0#3-update-loginapppurchase-calls).)

### Custom purchases

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

```typescript
try {
    const success = await Grovs.logCustomPurchase(
        'buy',              // type: 'buy' | 'cancel' | 'refund'
        999,                // priceInCents: $9.99
        'USD',              // currency code
        'premium_monthly',  // product identifier
    );
    console.log('Custom purchase tracked:', success);
} catch (error) {
    console.error('Error:', error);
}
```

**Parameters:**

| Parameter      | Type              | Description                                                                                                                                                                                            |
| -------------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `type`         | `TransactionType` | `'buy'`, `'cancel'` or `'refund'`                                                                                                                                                                      |
| `priceInCents` | `number`          | Amount in cents (for example 999 for $9.99). Converted to an integer natively                                                                                                                          |
| `currency`     | `string`          | ISO 4217 currency code (for example `'USD'`, `'EUR'`)                                                                                                                                                  |
| `productId`    | `string`          | Your product identifier                                                                                                                                                                                |
| `startDate`    | `string?`         | ISO 8601 date string, for example `2026-09-14T10:21:33Z`. Defaults to now. iOS parses it with `ISO8601DateFormatter`, which rejects fractional seconds; Android falls back to now if it can't parse it |

The promise resolves `true` once the native SDK has the purchase. It stores it and sends it when authenticated.

### Tracking cancellations and refunds

```typescript
// Log a cancellation
await Grovs.logCustomPurchase('cancel', 999, 'USD', 'premium_monthly');

// Log a refund
await Grovs.logCustomPurchase('refund', 999, 'USD', 'premium_monthly');
```

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