Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: ads

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: ads
Showing posts with label ads. Show all posts
Showing posts with label ads. Show all posts

Admob EU Consent Implementation

Admob EU Consent Implementation





Since Google Announce New Policy EU User. But Some Of US Dont Understand What we Do So let's Start Today We Start How To Comply EU Users for Admob.

Here We Implement Consent sdk which prompt user which ads type they want .

Let's Start.



Lets Start.

First Login Your Admob Account 

Then Open Menu = > Blocking Controls =>  EU User Consent

And Select Custom Ads Provider and Select 12 Ads Provider.



Then Save.

Now Let's Start Coding In Our App.

Open Your build.gradle and add this consent library . 
implementation 'com.google.android.ads.consent:consent-library:1.0.3'
 
Then 
 
Currently i am loading ads in my main activity so first i call the consent methods.
 
  

    private ConsentForm form;
Boolean Adtype=false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        constant=new Constant(getApplicationContext());
          final ConsentInformation consentInformation = ConsentInformation.getInstance(MainActivity.this);
            String[] publisherIds = {"YOUR_PUBLISHER_ID"};
       
             consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
                    @Override
                    public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                        // User's consent status successfully updated.
                        Log.d("ok", consentInformation.toString());
                    }

                    @Override
                    public void onFailedToUpdateConsentInfo(String errorDescription) {
                        // User's consent status failed to update.
                    }
                });
           showAdmob();
showADS();

        }

public void showAdmob(){
     URL privacyUrl = null;
        try {
            // TODO: Replace with your app's privacy policy URL.
            privacyUrl = new URL("APP_PRIVACYURL_OF_YOUR_SITE");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            // Handle error.
        }

        ConsentForm.Builder builder = new ConsentForm.Builder(MainActivity.this, privacyUrl);

        builder.withListener(new ConsentFormListener() {
            @Override
            public void onConsentFormLoaded() {

                form.show();


            }

            @Override
            public void onConsentFormOpened() {

            }

            @Override
            public void onConsentFormClosed(
                    ConsentStatus consentStatus, Boolean userPrefersAdFree) {

                if(consentStatus.name().equalsIgnoreCase("PERSONALIZED")){
Adtype=true;
     showADS();
 

                }
                else {
Adtype=false;
    showADS();
                }
            }

            @Override
            public void onConsentFormError(String errorDescription) {

            }
        });
        builder.withPersonalizedAdsOption();
        builder.withNonPersonalizedAdsOption();
        form = builder.build();

        form.load();

}

//======By Default Admob Use Non Peronalised Ads but Here Confimation box ask from EU User Which Ads They Want===
//====If Non Relevant ads selected then we pass bundle with "npa" => "1" in adbuilder===
//=====Then It Will Display the Ads also we call The Admob Ads===============

   public void showADS(){
        AdRequest request;
        if(Adtype){
            Bundle extras = new Bundle();
            extras.putString("npa", "1");
            request = new AdRequest.Builder()
                    .addNetworkExtrasBundle(AdMobAdapter.class, extras)
                    .build();
        }
        else{
            request = new AdRequest.Builder().build();
        }
        final InterstitialAd mInterstitialAd;
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("YOUR_ADS_UNIT);
        mInterstitialAd.loadAd(request);
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                mInterstitialAd.show();

            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
            }

            @Override
            public void onAdOpened() {
            }

            @Override
            public void onAdLeftApplication() {
            }

            @Override
            public void onAdClosed() {
            }
        });


    }
2