LoginSignup
4
4

More than 5 years have passed since last update.

KotlinでAdMobをGDPR対応させる(Consent SDKの簡単な説明)

Last updated at Posted at 2018-06-01

kotlinでの実装方法です。
参考程度にお使いください。
自己責任で。

実行環境

AndroidStudio 3.1
Kotlin 1.2.30

参考

公式リファレンス(英語) Google

導入

下記のリンクを参考にして下さい
AdMobを使っているAndroidアプリでEU一般データ保護規則(GDPR)対応する(Qiita)

実装

package パッケージ名

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.ads.consent.*
import java.net.MalformedURLException
import java.net.URL

class MainActivity : AppCompatActivity() {

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

        val consentInformation = ConsentInformation.getInstance(this)

        //テストの時に必要な記述 =========================
        consentInformation.reset()//同意情報をリセット
        consentInformation.addTestDevice("000000000000000000000000000")//テストデバイスIDを追加
        consentInformation.debugGeography = DebugGeography.DEBUG_GEOGRAPHY_EEA//地域をEEAに設定
        // consentInformation.debugGeography = DebugGeography.DEBUG_GEOGRAPHY_NOT_EEA//EEA圏外に設定
        //=============================================

        val publisherIds = arrayOf("AdMobのパブリッシャーID")

        var privacyUrl: URL? = null
        try {
            // TODO: Replace with your app's privacy policy URL.
            privacyUrl = URL("プライバシーポリシーURL")

        } catch (e: MalformedURLException) {
            e.printStackTrace()
            // Handle error.
        }

        var form: ConsentForm? = null

        //フォーム作成 ===================================
        form = ConsentForm.Builder(this, privacyUrl).withListener(object : ConsentFormListener() {

            override fun onConsentFormLoaded() {
                super.onConsentFormLoaded()

                //ロード終了時処理
                form?.show()//ロードが終わったため同意フォーム表示
            }

            override fun onConsentFormOpened() {
                super.onConsentFormOpened()
                //フォームがオープンした
            }

            override fun onConsentFormClosed(consentStatus: ConsentStatus?, userPrefersAdFree: Boolean?) {
                super.onConsentFormClosed(consentStatus, userPrefersAdFree)

                //フォームが閉じられた
                //ここで選択された情報をもとに処理

                if (userPrefersAdFree!!) {
                    //有料版ボタンを選択した 有料版へ誘導
                    return
                }

                when (consentStatus) {//同意ステータスを元に分岐

                    ConsentStatus.PERSONALIZED -> {
                        //パーソナライズ広告を表示
                    }
                    ConsentStatus.NON_PERSONALIZED -> {
                        //ノンパーソナライズ広告を表示

                    }
                    ConsentStatus.UNKNOWN -> {
                        //ステータス不明
                    }
                }
            }

            override fun onConsentFormError(reason: String?) {
                super.onConsentFormError(reason)
                //フォームエラー
                Log.d("onConsentFormError", reason)
            }
        })
                .withPersonalizedAdsOption()//パーソナライズ同意ボタンを追加
                .withNonPersonalizedAdsOption()//ノンパーソナライズ同意ボタンを追加
                .withAdFreeOption()//有料版ボタンを追加
                .build()
        //================================================


        //ステータス更新処理
        consentInformation.requestConsentInfoUpdate(publisherIds, object : ConsentInfoUpdateListener {

            override fun onFailedToUpdateConsentInfo(reason: String?) {
                //ユーザーの同意ステータス更新失敗
                Log.d("onFailedToUpdate", reason)
            }

            override fun onConsentInfoUpdated(consentStatus: ConsentStatus?) {

                //isRequestLocationInEeaOrUnknownでユーザーがEEAまたは不明な場所か判定
                if (consentInformation.isRequestLocationInEeaOrUnknown) {
                    when (consentStatus) {

                        ConsentStatus.PERSONALIZED -> {
                            //パーソナライズに同意済み
                        }
                        ConsentStatus.NON_PERSONALIZED -> {
                            //ノンパーソナライズに同意済み
                        }

                        ConsentStatus.UNKNOWN -> {
                            form.load()//ステータス不明なので同意フォームをロード
                        }
                    }
                }
            }

        })
    }
}

AdMobをノンパーソナライズに設定する

val adRequest = AdRequest.Builder()
......

//下記を記述
if (ConsentInformation.getInstance(context).consentStatus == ConsentStatus.NON_PERSONALIZED) {
   val extras = Bundle()
       extras.putString("npa", "1")
       adRequest.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)

 }

テストデバイスIDの取得

Logcat

I/ConsentInformation: Use ConsentInformation.getInstance(context).addTestDevice("テストデバイスID") to get test ads on this device.
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4