GDPRメッセージをAndroidアプリに組み込む

2024年2月13日

開発環境
OS:Windows 11
SDK:Android Studio Flamingo | 2022.2.1

概要

AndroidアプリにGDPRメッセージを組み込みます。

AdMobに表示された警告

AdMobのコンソール画面を表示したところGDPRメッセージについて警告が表示されていました。

2024年1月14日からイギリス、または、欧州経済領域で広告を表示するには事前に広告表示に関する確認が必要になるようです。

GPDRメッセージとはその確認のための案内文であり、UMP SDKをアプリに組み込むとAdMobで作成したメッセージを表示してくるようです。

GDPRメッセージ作成の流れ

早速、実装していきます。

まずは、AdMobのコンソール画面に表示された「GPDRメッセージを作成」をタップします。

(メニュー「プライバシーとメッセージ」のGDPRから作成・編集することもできます。)

GDPRメッセージの作成フローが表示されます。

流れに沿って設定していけば滞りなく実装できます。

ステップ1.メッセージを作成する

まずはGDPRメッセージを作成します。

メッセージ自体はデフォルトのフォーマットがあるためそのまま使用します。

設定すべき項目としては、

「表示対象アプリ」、「言語」、「同意しないボタンの表示有無」ぐらいです。

私は、同意しないボタンを表示するパターンと表示しないパターンの2パターン作成し、

表示対象アプリをそれぞれのパターンに振り分けることにしました。

基本的には同意しないボタンを表示しないパターンを使用し、

こども向けゲームアプリだけ同意しないボタンを表示するパターンを使用することにしました。

ステップ2.UMP SDKを組み込む

メッセージの作成が終わったら、アプリにUMP SDKを組み込みます。

まずはbuilde.gradleの実装。

    implementation 'com.google.android.ump:user-messaging-platform:2.1.0'

次に必要なモジュールをインポートします。

import com.google.android.ump.ConsentForm
import com.google.android.ump.ConsentInformation
import com.google.android.ump.ConsentInformation.OnConsentInfoUpdateFailureListener
import com.google.android.ump.ConsentInformation.OnConsentInfoUpdateSuccessListener
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.UserMessagingPlatform
import com.google.android.ump.ConsentDebugSettings
import java.util.concurrent.atomic.AtomicBoolean

あとは適当な場所にメッセージの呼び出し処理を追加します。

    private lateinit var consentInformation: ConsentInformation
    private var isMobileAdsInitializeCalled = AtomicBoolean(false)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val params = ConsentRequestParameters
            .Builder()
            .setTagForUnderAgeOfConsent(false)
            .build()

        consentInformation = UserMessagingPlatform.getConsentInformation(this)
        consentInformation.requestConsentInfoUpdate(
            this,
            params,
            OnConsentInfoUpdateSuccessListener {
                UserMessagingPlatform.loadAndShowConsentFormIfRequired(
                    this@MainActivity,
                    ConsentForm.OnConsentFormDismissedListener {
                        // Consent has been gathered.
                        if (consentInformation.canRequestAds()) {
                            initializeMobileAdsSdk()
                        }
                    }
                )
            },
            OnConsentInfoUpdateFailureListener {

            })

        if (consentInformation.canRequestAds()) {
            initializeMobileAdsSdk()
        }
    }

最後にメッセージロード後の処理を追加して完了です。

    private fun initializeMobileAdsSdk() {
        if (isMobileAdsInitializeCalled.get()) {
            return
        }
        isMobileAdsInitializeCalled.set(true)

        MobileAds.initialize(this)
    }

ステップ3.デバッグする

最後に動作確認をします。

まずはデバッグ用のパラメータを追加します。

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val debugSettings = ConsentDebugSettings.Builder(this)
            .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
            .addTestDeviceHashedId("46********************E1")←デバッグ機のデバイスID
            .build()

        val params = ConsentRequestParameters
            .Builder()
//            .setTagForUnderAgeOfConsent(false)
            .setConsentDebugSettings(debugSettings)
            .build()

        consentInformation = UserMessagingPlatform.getConsentInformation(this)
        consentInformation.requestConsentInfoUpdate(
            this,
            params,
     ~
    }

デバッグするには、デバッグ機のデバイスIDが必要になります。

アプリを起動するとログにデバイスIDが出力されます。

ログは「.addTestDeviceHash」で絞り込むと見やすいです。

アプリを起動させ、以下のような画面が表示されたら実装成功です。

もう1度確認したい場合は、

consentInformation.requestConsentInfoUpdateの前に以下を追加すると

同意を行った後もまた案内文が表示されるようになります。

        consentInformation.reset()

ここまで動作確認できれば、あとはリリースするだけです。

お疲れさまでした。