RevenueCat purchases-android

repository·main·Indexed 20 days ago

https://github.com/revenuecat/purchases-android

An open-source wrapper around Google Play Billing that integrates with the RevenueCat backend for in-app subscription management, receipt validation, and cross-platform status tracking. Includes a Purchases Codegen Gradle plugin for generating type-safe Kotlin code for entitlements and offerings, and an AdMob adapter for automating the mapping of Google AdMob callbacks to RevenueCat ad events.

Tokens
20.6K
Snippets
44
Records
78
Agent score
67%

What's inside purchases-android

  1. Overview of the RevenueCat AdMob Adapter

    main

    The RevenueCat AdMob Adapter wraps standard AdMob ad lifecycle callbacks to automatically track ad events within the RevenueCat dashboard. It acts as a drop-in replacement for standard AdMob loading calls, allowing you to track events such as loaded, displayed, opened, revenue, and failed-to-load with minimal code changes.

    Note on Language Support:

    • Kotlin: Full support for the load-and-track helper APIs.
    • Java: Helper APIs are not currently available. Java users must call the core AdTracker APIs (e.g., trackAdDisplayed, trackAdRevenue) directly.
  2. Overview of the Purchases Android SDK

    main
    Purchases is the client-side SDK for RevenueCat, designed to simplify the implementation of in-app subscriptions and purchases on Android. It provides the necessary classes and methods to manage customer entitlements, track purchases, and integrate with the RevenueCat backend.
  3. Overview of Purchases Android SDK

    main

    The Purchases SDK is a client-side wrapper for the RevenueCat subscription and purchase tracking system. It wraps the Google Play BillingClient and communicates with the RevenueCat backend to simplify implementing in-app purchases and subscriptions on Android.

    Key capabilities include:

    • Receipt Validation: Server-side validation of purchases.
    • Subscription Status Tracking: Cross-platform tracking of whether a user is currently subscribed.
    • Remote Configuration: Managing products, offerings, and entitlements via the RevenueCat dashboard.
    • Webhooks: Real-time server-to-server notifications for events like renewals, cancellations, and purchases.
    • Analytics: Automatic calculation of business metrics like MRR, churn, and conversion.
  4. Integrate Google AdMob with RevenueCat using the AdMob adapter

    main

    The purchases-android-admob adapter library automates the mapping of Google AdMob callbacks to RevenueCat ad events. Instead of manually calling RevenueCat trackers, you use the adapter's extension functions or adTracker methods to load ads. This ensures that events like Ad Loaded, Ad Displayed, Ad Opened, Ad Revenue, and Ad Failed to Load are automatically sent to your RevenueCat Dashboard.

    Supported formats include:

    • Banner Ads
    • Interstitial Ads
    • App Open Ads
    • Rewarded Ads
    • Rewarded Interstitial Ads
    • Native Ads
    • Native Video Ads
    // The adapter sits between AdMob and RevenueCat, automatically mapping callbacks.
    // Example of the flow:
    // AdMob SDK (Load Ads) -> Adapter Library (Tracks events) -> RevenueCat Dashboard
  5. What is Custom Entitlements Computation mode?

    main

    Custom Entitlements Computation mode is a specialized behavior mode for the RevenueCat SDK designed for apps that perform their own entitlement computation independently of RevenueCat.

    Key characteristics of this mode:

    • Entitlement Management: Apps rely on webhooks to signal their backends to refresh entitlements with RevenueCat.
    • User IDs: RevenueCat will not generate anonymous user IDs.
    • Caching: The CustomerInfo cache is not refreshed automatically. It is only refreshed when a purchase is completed.
    • API Restrictions: Most SDK methods are disallowed; only methods for configuration, switching users, getting offerings, and making purchases are available.
    • Configuration Timing: The SDK should only be configured once the initial appUserID is known.
  6. Events tracked by the RevenueCat AdMob Adapter

    main

    All supported ad formats automatically report the following RevenueCat ad events to ensure accurate analytics and revenue tracking:

    • Ad Loaded: The ad was successfully loaded.
    • Ad Displayed: An impression was recorded.
    • Ad Opened: The user clicked or interacted with the ad.
    • Ad Revenue: Revenue reported via AdMob's OnPaidEventListener.
    • Ad Failed to Load: A load error occurred.
  7. Handle Subscription Offers and Free Trials in V6

    main

    In V6, a StoreProduct represents a subscription duration and contains multiple SubscriptionOptions (e.g., base plans, free trials, intro offers).

    When you pass a Package or StoreProduct to purchase(), the SDK automatically selects an option by:

    1. Filtering out offers with the rc-ignore-offer tag.
    2. Selecting the SubscriptionOption with the longest free trial or cheapest first phase.
    3. Falling back to the base plan.

    To gain manual control over which offer is presented, create your PurchaseParams.Builder using a specific SubscriptionOption instead of a Package or StoreProduct.

    val basePlan = storeProduct.subscriptionOptions?.basePlan
    val defaultOffer = storeProduct.subscriptionOptions?.defaultOffer
    val freeOffer = storeProduct.subscriptionOptions?.freeTrial
    val introOffer = storeProduct.subscriptionOptions?.introOffer
    val offersForLapsedCustomers = storeProduct.subscriptionOptions?.withTag("lapsed-customers")
  8. Migrate from PurchaserInfo to CustomerInfo (v5+)

    main

    The PurchaserInfo type and its associated functions have been renamed to CustomerInfo to maintain parity with iOS. You should update all references to these types and methods in your codebase.

    | Old type name | New type name |
    |------------|------|
    | `PurchaserInfo` | `CustomerInfo` |
    | `ReceivePurchaserInfoListener` | `ReceiveCustomerInfoCallback` |
    | `UpdatedPurchaserInfoListener` | `UpdatedCustomerInfoListener` |
    | `ReceiveOfferingsListener` | `ReceiveOfferingsCallback` |
    | `PurchasesErrorListener` | `PurchasesErrorCallback` |
  9. Understand the trade-offs of using Codegen

    main

    The Purchases Codegen plugin is best suited for stable identifiers (Entitlement IDs, Offerings, and Packages) because it provides compile-time safety and autocomplete.

    Key Trade-off: Dynamic vs. Static Content

    • Static (Codegen): Any change in the RevenueCat dashboard (renaming an offering, adding a package) requires a new build and app release to update the generated Kotlin code.
    • Dynamic (Runtime API): If you access offering.availablePackages directly via the standard SDK, your app can pick up new packages from the backend at runtime without a new release.

    Decision Rule: Use Codegen when you want to reference specific IDs in your logic (e.g., RCEntitlementId.PREMIUM_ACCESS). Use the raw API if you manage paywall presentation entirely from the dashboard without touching code.

  10. Using Placements to categorize ad events

    main

    Every tracking method in the AdMob Adapter accepts an optional placement string. This string tags all events associated with a specific ad, allowing you to distinguish performance across different screens or ad slots in your RevenueCat dashboard.

    Examples of logical placement names include:

    • "home_banner"
    • "level_complete_interstitial"
    • "feed_native"
    • "bonus_coins_rewarded"

    Placements are free-form strings; you should establish a consistent naming convention for your application.

  11. Migrate SDK configuration to PurchasesConfiguration (v5+)

    main

    In version 5 and later, the configure function has changed. Instead of passing multiple parameters directly, you must now use a PurchasesConfiguration.Builder to create a configuration object. This change supports both Google Play and Amazon Appstore.

    // For Google Play
    Purchases.configure(PurchasesConfiguration.Builder(this, "public_google_sdk_key").build())
    
    // For Amazon
    Purchases.configure(AmazonConfiguration.Builder(this, "public_amazon_sdk_key").build())