Search code examples
androidflutter

Flutter How to Implement the UMP SDK


I am using Flutter with google_mobile_ads package for my free app. As the app publisher who uses Admob, I need to Implement the UMP SDK to comply with the requirement to use a Google-certified Consent Management Platform (CMP).

I need to update my app code to comply.

Google says ask user consent for:

  1. Cookies and analytics
  2. personalized vs non-personalized ads

Consent is required if:

  1. a user is located in the EU or UK
  2. a user is located in the EU or UK and the user is under age 18

Implementation Steps:

  1. if the user is not in the EU or UK, then no consent form on app launch is needed

  2. if the user is in the EU or UK, then show consent form(for cookies and analytics and using personalized ads vs non-personalized ads). In this case, the user cannot proceed without accepting the consent form.

  3. if the user is in the EU or UK and under age 18, then show consent form(cookies and analytics and using personalized ads vs non-personalized ads). In this case, the user cannot proceed without accepting the consent form.

There are 4 questions:

  1. google ads are all use cookies for analytics. This question answer should always be yes otherwise, the consent will be declined, and the app should not launch. Do you agree?

  2. How do we determine the user location using the code?

  3. How do we determine the user age?

  4. Users have no control whether they are using personalized ads vs non-personalized ads b/c this is based on the admob account setting for the app. I don't think there is a way to check admob setting using the code.

There are instructions how to implement UMP SDK in Java/Kotlin

There is the package: user_messaging_platform (16 months old)

We could create plugin(to use Kotlin in Flutter) - please see question here: stackoverflow.com/questions/76815370/using-kotlin-code-in-flutter-via-methodchannel

Creating such plugin is too complex.

Another option is to create Flutter package(Dart only code) specifically to address this compliance issue which I feel is too complex.

We need to do the following: enter image description here

We could also pick an option in the Admob account, but we still neeed to do the above steps:

enter image description here

Please provide your thoughts.

Thank You


Solution

  • There is an easy way to implement the UMP SDK in flutter. It can be done with the google_mobile_ads package itself. Below I give a simple example of how to enable the UMP message to comply with the new European regulations related to advertisements.

    After the initial configuration of this package (Quick start) you can create the following repository.

    class AdRepository {
      static Future<InitializationStatus> initGoogleMobileAds() {
        return MobileAds.instance.initialize();
      }
    
      static showConsentUMP() {
        final params = ConsentRequestParameters();
        ConsentInformation.instance.requestConsentInfoUpdate(
          params,
          () async {
            if (await ConsentInformation.instance.isConsentFormAvailable()) {
              loadForm();
            }
          },
          (FormError error) {
            // Handle the error
          },
        );
      }
    
      static void loadForm() {
        ConsentForm.loadConsentForm(
          (ConsentForm consentForm) async {
            var status = await ConsentInformation.instance.getConsentStatus();
            if (status == ConsentStatus.required) {
              consentForm.show(
                (FormError? formError) {
                  loadForm();
                },
              );
            }
          },
          (formError) {
            // Handle the error
          },
        );
      }
    }
    

    Then in the Home class in the initState you can call the showConsentUMP function, as shown.

    class HomePage extends StatefulWidget {
      const HomePage({
        super.key,
      });
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      ...
      @override
      void initState() {
        super.initState();
        AdRepository.showConsentUMP();
      }
    
      @override
      Widget build(BuildContext context) {
        ...
      }
    }
    

    As an additional step, you must create a message in the Admob platform itself that will be shown to the user for ad customization.

    In the case of iOS, it is recommended to add the following keys to the Info.plist file that will allow customization of the ads.

    <key>NSUserTrackingUsageDescription</key>
    <string>Description of ad personalization to allow user tracking</string>
    <key>SKAdNetworkItems</key>
    <array>
       <dict>
         <key>SKAdNetworkIdentifier</key>
         <string>cstr6suwn9.skadnetwork</string>
       </dict>
              ...
     <array>
    

    When publishing the application in the App Store, you must mark in the privacy policy that you use user identifiers for advertising-oriented tracking.