Set up UMP SDK

The Google User Messaging Platform (UMP) SDK is a privacy and messaging tool to help you manage privacy choices. For more information, see About Privacy & messaging.

Create a message type

Create user messages with one of the Available user message types under the Privacy & messaging tab of your AdMob account. The UMP SDK attempts to display a privacy message created from the AdMob Application ID set in your project.

You should request an update of the user's consent information at every app launch, using requestConsentInfoUpdate(). This request checks the following:

  • Whether consent is required. For example, consent is required for the first time, or the previous consent decision expired.
  • Whether a privacy options entry point is required. Some privacy messages require apps to allow users to modify their privacy options at any time.
/**
 * Requests consent information update on app launch.
 */
export function requestConsent(onComplete?: () => void) {
  requestConsentInfoUpdate({}, (error) => {
    if (error) {
      console.warn('Consent info update error:', error.message);
    } else {
      console.log('Consent info update complete.');
    }

    // Load and show consent form if required.
    loadAndPresentConsentForm(onComplete);
  });
}

Load and present the privacy message form

After you have received the most up-to-date consent status, call loadAndShowConsentFormIfRequired() to load any forms required to collect user consent. After loading, the forms present immediately.

/**
 * Loads and presents the privacy message form if required to collect user consent.
 */
export function loadAndPresentConsentForm(onComplete?: () => void) {
  loadAndShowConsentFormIfRequired((formError) => {
    if (formError) {
      console.warn('Consent form error:', formError.message);
    } else {
      console.log('Consent gathering complete.');
    }

    // Execute logic that depends on consent state (for example, initialize the SDK).
    onComplete?.();
  });
}

Privacy options

Some privacy message forms are presented from a publisher-rendered privacy options entry point, letting users manage their privacy options at any time. To learn more about which message your users see at the privacy options entry point, see Available user message types.

Check if a privacy options entry point is required

After you have called requestConsentInfoUpdate(), check getPrivacyOptionsRequirementStatus() to determine if a privacy options entry point is required for your app. If an entry point is required, add a visible and interactable UI element to your app that presents the privacy options form. If a privacy entry point is not required, configure your UI element to be not visible and interactable.

/**
 * Helper to determine if the privacy options entry point is required.
 */
export function isPrivacyOptionsRequired(): boolean {
  return (
    getPrivacyOptionsRequirementStatus() ===
    PrivacyOptionsRequirementStatus.REQUIRED
  );
}

For the full list of privacy options requirement statuses, see PrivacyOptionsRequirementStatus.

Present the privacy options form

When the user interacts with your element, present the privacy options form:

/**
 * Renders a privacy options button if required by user consent policy.
 */
export function PrivacyOptionsButton({
  onDismissed,
}: {
  onDismissed?: (error?: FormError) => void;
}) {
  const isRequired =
    getPrivacyOptionsRequirementStatus() ===
    PrivacyOptionsRequirementStatus.REQUIRED;

  if (!isRequired) {
    return null;
  }

  const handleShowPrivacyOptions = () => {
    showPrivacyOptionsForm((error) => {
      if (error) {
        console.error('Failed to show privacy options form:', error.message);
      } else {
        console.log('Privacy options form dismissed.');
      }

      // Execute custom logic after form is dismissed.
      onDismissed?.(error);
    });
  };

  return <Button title="Privacy Settings" onPress={handleShowPrivacyOptions} />;
}

Request ads with user consent

Before requesting ads, use canRequestAds() to check if you've obtained consent from the user:

/**
 * Checks whether user consent allows requesting ads and initializes the SDK.
 */
let isMobileAdsInitialized = false;

export function initializeMobileAdsSdkIfConsentGiven() {
  if (canRequestAds()) {
    if (!isMobileAdsInitialized) {
      isMobileAdsInitialized = true;
      initialize({}, (status) => {
        console.log('Google Mobile Ads SDK initialized:', status);
      });
    }
  }
}

Listed are the following places to check if you can request ads while gathering consent:

  • After the UMP SDK gathers consent in the current session.
  • Immediately after you have called requestConsentInfoUpdate(). The UMP SDK might have obtained consent in the previous app session.

If an error occurs during the consent gathering process, check if you can request ads. The UMP SDK uses the consent status from the previous app session.

Prevent redundant ad request work

As you check canRequestAds() after gathering consent and after calling requestConsentInfoUpdate(), ensure your logic prevents redundant ad requests that might result in both checks returning true. For example, with a boolean variable.

Testing

If you want to test the integration in your app as you're developing, follow these steps to programmatically register your test device. Be sure to remove the code that sets these test device IDs before you release your app.

  1. Call requestConsentInfoUpdate().
  2. Check the log output for a message similar to the following example, which shows your device ID and how to add it as a test device:

    Android

    Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("33BE2250B43518CCDA7DE426D04EE231")
    to set this as a debug device.
    

    iOS

    <UMP SDK>To enable debug mode for this device,
    set: UMPDebugSettings.testDeviceIdentifiers = @[2077ef9a63d2b398840261c8221a0c9b]
    
  3. Copy your test device ID to your clipboard.

  4. Modify your code to call consentDebugSettings.testDeviceIdentifiers and pass in a list of your test device IDs.

    /**
     * Registers a test device for testing consent flows.
     */
    export function configureTestDevice() {
      requestConsentInfoUpdate(
        {
          consentDebugSettings: {
            debugGeography: DebugGeography.DISABLED,
            testDeviceIdentifiers: ["TEST_DEVICE_HASHED_ID"],
          },
        },
        (error) => {
          if (!error) {
            console.log('Test device registered.');
          }
        },
      );
    }

Force a geography

The UMP SDK provides a way to test your app's behavior as though the device were located in various regions, such as the European Economic Area (EEA), the United Kingdom (UK), and Switzerland using DebugGeography. Note that debug settings only work on test devices.

/**
 * Configures debug settings to force EEA geography on a test device.
 */
export function configureDebugGeography() {
  requestConsentInfoUpdate(
    {
      consentDebugSettings: {
        debugGeography: DebugGeography.EEA,
        testDeviceIdentifiers: ["TEST_DEVICE_HASHED_ID"],
      },
    },
    (error) => {
      if (!error) {
        console.log('Consent debug test configuration loaded.');
      }
    },
  );
}

When testing your app with the UMP SDK, you might find it helpful to reset the state of the SDK so that you can simulate a user's first install experience. The SDK provides the reset() method to do this.

/**
 * Resets consent state for testing purposes.
 */
export function resetConsentState() {
  reset();
}