Android Question Admob Native Ads PersonalizedAds setting

sorex

Expert
Licensed User
Longtime User
Hello,

Can anyone tell me how I can set the ad builder to personalized ads or not for native ads.
(I'm using the example code seen in this thread > https://www.b4x.com/android/forum/threads/any-news-about-native-ads-advanced.88100/#post-562893 )

According to the admob site it should be this...

For the AdMob tag:
builder.setAdvancedOptionValue("personalizedAds", "false");

altho this fails

B4X: Copy
builder.RunMethod("setAdvancedOptionValue",Array("personalizedAds", "false"))

java.lang.RuntimeException: Method: setAdvancedOptionValue not found in: com.google.android.gms.ads.AdLoader$Builder


but as I'm not used to javaobject it might be wrong what I'm trying to do :)
 
Here


they mention

B4X: Copy
The default behavior of the Google Mobile Ads SDK is to serve personalized ads. If a user has consented to receive only non-personalized ads, you can configure an AdRequest object to specify that only non-personalized ads should be requested. The following code causes non-personalized ads to be requested regardless of whether or not the user is in the EEA:

Bundle extras = new Bundle();
extras.putString("npa", "1");

AdRequest request = new AdRequest.Builder()
        .addNetworkExtrasBundle(AdMobAdapter.class, extras)
        .build();

but there's not really something mentioned about personalized ads in the Native Ads part of the api.
 
Upvote 0
indeed, it is only mentioned on this page at the bottom.


and also mentions admob & ios but it's not clear if it is related to their android/ios admob SDKs.

my guess is that we need the same code that's behind the builder.NonPersonalizedAds command in the current lib in the loadAd sub.
 
Upvote 0
I use the native ads and I don't have this personalized ads.
Until now it works fine to me. See my example:

 
Upvote 0
I know it works that's not the problem :)

The problem is that if you live in europe you must decide if it can deliver personal/tailored ads based on your location (ip/gps?).

If you don't want that you need to set that bit somehow.

For banners you can use the builder option but this javaobject method should be dealed in some other way.
 
Upvote 0
y guess is that we need the same code that's behind the builder.NonPersonalizedAds command in the current lib in the loadAd sub.
The relevant code is:
Code: Copy
Bundle extras = new Bundle();
 extras.putString("npa", "1");

You can do:
Code: Copy
Dim rb As AdRequestBuilder = builder
rb.NonPersonalizedAds
'continue to work with builder
 
Upvote 0
thank for the hint.

that resulted in an error tho.

java.lang.ClassCastException: com.google.android.gms.ads.AdLoader$Builder cannot be cast to com.google.android.gms.ads.AdRequest$Builder

do we need a traditional builder to set the flag and then continue with the JavaObject one?
wouldn't that create 2 different builder objects thus not having the flag set in the end?

B4X: Copy
    Dim ctxt As JavaObject
    ctxt.InitializeContext

    Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
    Dim OnContentAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeContentAd.OnContentAdLoadedListener".Replace("/", "."), _
       "ContentAdLoaded", Null)
    builder.RunMethod("forContentAd", Array(OnContentAdLoadedListener))

    Dim adreqb As AdRequestBuilder=builder '!!!!!!!!!!!
    adreqb.NonPersonalizedAds              '!!!!!!!!!!!

    Dim Listener As JavaObject
    Listener.InitializeNewInstance(Application.PackageName & ".main$MyAdListener", Array("NativeAd"))  'change 'main' with the current activity module name
    builder.RunMethod("withAdListener", Array(Listener))
    Dim AdLoader As JavaObject = builder.RunMethod("build", Null)

    Dim AdRequestBuilder As JavaObject
    AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)

    AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
    Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)
 
Upvote 0
no not yet. there ain't much to find about it unfortunately.

that npa=1 method appeared a few times but not really in a full "official" example.

I don't know if this is java or something else but it was found on stackoverflow


B4X: Copy
AdRequest.Builder builder = new AdRequest.Builder();
if (/*Request non-personalized ads*/) {
    Bundle extras = new Bundle();
    extras.putString("npa", "1");
    builder.addNetworkExtrasBundle(AdMobAdapter.class, extras);
}
AdRequest request = builder.build();
 
Upvote 0
Back
Top