In-App Purchase Ads

In-App Purchase (IAP) ads are interstitial ads that display offers for your in-app products. They allow users to make purchases as part of your normal ad flow without leaving your app. Before integrating IAP ads into your app, make sure you create an IAP house ad campaign and IAP ad.

The Google Mobile Ads Unity plugin offers support for two different IAP ad flows:

  • Default In-App Purchase Flow (recommended for beginners)
  • Custom In-App Purchase Flow (for advanced publishers)

In the Default In-App Purchase Flow, the entire Google Play billing flow is already implemented for you in the Mobile Ads SDK. In the Custom In-App Purchase Flow, the developer controls the entire in-app billing process. PlayStorePurchaseListener vs. InAppPurchaseListener explains the differences between the two in more detail.

Default In-App Purchase Flow

Once you've set up your campaign, follow these steps to integrate Default IAP ads:

  1. In the AndroidManifest.xml in Assets/Plugins/Android/GoogleMobileAdsPlugin, uncomment the line to enable billing permissions:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.unity">
    <!-- Google Mobile Ads Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- Uncomment to add billing for in-app purchase ads -->
    <uses-permission android:name="com.android.vending.BILLING" />
    

  2. Create a class that implements the IDefaultInAppPurchaseProcessor interface, such as DefaultGoogleMobileAdsDemoHandler:

    public class DefaultGoogleMobileAdsDemoHandler : IDefaultInAppPurchaseProcessor
    {
        // TODO: Insert your app's valid skus here
        private readonly string[] validSkus = { "android.test.purchased" };
    
        // Will only be sent on a success.
        public void ProcessCompletedInAppPurchase(IInAppPurchaseResult result)
        {
            result.FinishPurchase();
        }
    
        // Check SKU against valid SKUs.
        public bool IsValidPurchase(string sku)
        {
            foreach(string validSku in validSkus) {
                if (sku == validSku) {
                    return true;
                }
            };
            Debug.Log("SKU Not found!: " + sku);
            return false;
        }
    
        // Return the app's public key.
        public string AndroidPublicKey
        {
            // In a real app, return public key instead of null.
            get { return null; }
        }
    }
    
  3. Pass in the above implementation of IDefaultInAppPurchaseProcessor to InterstitialAd.SetInAppPurchaseHandler:

    interstitial.SetInAppPurchaseHandler(new DefaultGoogleMobileAdsDemoHandler());
    

Custom In-App Purchase Flow

Once you've set up your campaign, follow these steps to integrate Custom IAP ads:

  1. Create a class that implements the ICustomInAppPurchaseProcessor interface, such as CustomGoogleMobileAdsDemoHandler:

    public class CustomGoogleMobileAdsDemoHandler : ICustomInAppPurchaseProcessor
    {
        // Process custom in-app purchase.
        public void ProcessInAppPurchase(ICustomInAppPurchase purchase)
        {
            String productId = purchase.ProductId;
            // Perform custom purchase flow logic.
            ...
        }
    }
    
  2. Pass in the above implementation of ICustomInAppPurchaseProcessor to InterstitialAd.SetInAppPurchaseHandler.

    interstitial.SetInAppPurchaseHandler(new CustomGoogleMobileAdsDemoHandler());
    
  3. When the purchase flow has completed, record the result of the purchase by calling ICustomInAppPurchase.recordResolution(...):

    purchase.RecordResolution(PurchaseResolutionType.Success);
    

    The possible resolutions are described by the following enum:

    public enum PurchaseResolutionType
    {
        Failure = 0,
        Success = 1,
        Cancelled = 2,
        InvalidProduct = 3,
    }
    

IAP Conversion Tracking

Use the AdWords Conversion Tracking SDK to track IAP conversions and report on them in the AdMob front-end. Copy the JAR for the SDK to the to the Plugins/Android directory. to include this SDK in your Unity project.

Send feedback about...

AdMob by Google
AdMob by Google