Skip to content

Commit 05d5180

Browse files
Merge pull request #53 from marius-bughiu/feat/ad-config-extras
Add ad targeting options
2 parents 885be41 + 32edfc7 commit 05d5180

26 files changed

Lines changed: 503 additions & 201 deletions

samples/Foo.Bar.SampleApp.Net9/Foo.Bar.SampleApp.Net9.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
</ItemGroup>
5959

6060
<ItemGroup>
61+
<MauiXaml Update="Pages\AdTargetingOptionsPage.xaml">
62+
<Generator>MSBuild:Compile</Generator>
63+
</MauiXaml>
6164
<MauiXaml Update="Pages\NativeAdsPage.xaml">
6265
<Generator>MSBuild:Compile</Generator>
6366
</MauiXaml>

samples/Foo.Bar.SampleApp.Net9/MainPage.xaml

Lines changed: 131 additions & 131 deletions
Large diffs are not rendered by default.

samples/Foo.Bar.SampleApp.Net9/MainPage.xaml.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Foo.Bar.SampleApp.Pages;
1+
using Foo.Bar.SampleApp.Net9.Pages;
2+
using Foo.Bar.SampleApp.Pages;
23
using Foo.Bar.SampleApp.Views;
34
using Plugin.AdMob;
45
using Plugin.AdMob.Services;
@@ -232,6 +233,11 @@ private void NativeAdView_OnAdClosed(object sender, EventArgs e)
232233
{
233234
Debug.WriteLine("Native ad closed.");
234235
}
236+
237+
private void OnAdTargetingOptionsClicked(object sender, EventArgs e)
238+
{
239+
Navigation.PushAsync(new AdTargetingOptionsPage());
240+
}
235241
}
236242

237243
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:admob="clr-namespace:Plugin.AdMob;assembly=Plugin.AdMob"
5+
x:Class="Foo.Bar.SampleApp.Net9.Pages.AdTargetingOptionsPage"
6+
Title="AdTargetingOptionsPage">
7+
<ScrollView>
8+
<Grid RowSpacing="16" ColumnSpacing="16" Padding="16">
9+
<Grid.RowDefinitions>
10+
<RowDefinition Height="Auto" />
11+
<RowDefinition Height="Auto" />
12+
<RowDefinition Height="Auto" />
13+
<RowDefinition Height="Auto" />
14+
</Grid.RowDefinitions>
15+
<Grid.ColumnDefinitions>
16+
<ColumnDefinition Width="2*" />
17+
<ColumnDefinition Width="*" />
18+
</Grid.ColumnDefinitions>
19+
20+
<Label Text="TagForChildDirectedTreatment" VerticalOptions="Center" />
21+
<Picker x:Name="TagForChildDirectedTreatmentPicker" Grid.Column="1">
22+
<Picker.ItemsSource>
23+
<x:Array Type="{x:Type admob:TagForChildDirectedTreatment}">
24+
<admob:TagForChildDirectedTreatment>None</admob:TagForChildDirectedTreatment>
25+
<admob:TagForChildDirectedTreatment>True</admob:TagForChildDirectedTreatment>
26+
<admob:TagForChildDirectedTreatment>False</admob:TagForChildDirectedTreatment>
27+
<admob:TagForChildDirectedTreatment>Unspecified</admob:TagForChildDirectedTreatment>
28+
</x:Array>
29+
</Picker.ItemsSource>
30+
</Picker>
31+
32+
<Label Text="TagForUnderAgeOfConsent" Grid.Row="1" VerticalOptions="Center" />
33+
<Picker x:Name="TagForUnderAgeOfConsentPicker" Grid.Row="1" Grid.Column="1">
34+
<Picker.ItemsSource>
35+
<x:Array Type="{x:Type admob:TagForUnderAgeOfConsent}">
36+
<admob:TagForUnderAgeOfConsent>None</admob:TagForUnderAgeOfConsent>
37+
<admob:TagForUnderAgeOfConsent>True</admob:TagForUnderAgeOfConsent>
38+
<admob:TagForUnderAgeOfConsent>False</admob:TagForUnderAgeOfConsent>
39+
<admob:TagForUnderAgeOfConsent>Unspecified</admob:TagForUnderAgeOfConsent>
40+
</x:Array>
41+
</Picker.ItemsSource>
42+
</Picker>
43+
44+
<Label Text="MaxAdContentRating" Grid.Row="2" VerticalOptions="Center" />
45+
<Picker x:Name="MaxAdContentRatingPicker" Grid.Row="2" Grid.Column="1">
46+
<Picker.ItemsSource>
47+
<x:Array Type="{x:Type admob:MaxAdContentRating}">
48+
<admob:MaxAdContentRating>None</admob:MaxAdContentRating>
49+
<admob:MaxAdContentRating>G</admob:MaxAdContentRating>
50+
<admob:MaxAdContentRating>PG</admob:MaxAdContentRating>
51+
<admob:MaxAdContentRating>T</admob:MaxAdContentRating>
52+
<admob:MaxAdContentRating>MA</admob:MaxAdContentRating>
53+
</x:Array>
54+
</Picker.ItemsSource>
55+
</Picker>
56+
</Grid>
57+
</ScrollView>
58+
</ContentPage>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Plugin.AdMob;
2+
using Plugin.AdMob.Configuration;
3+
4+
namespace Foo.Bar.SampleApp.Net9.Pages;
5+
6+
public partial class AdTargetingOptionsPage : ContentPage
7+
{
8+
public AdTargetingOptionsPage()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
protected override void OnAppearing()
14+
{
15+
base.OnAppearing();
16+
17+
TagForChildDirectedTreatmentPicker.SelectedItem = AdConfig.TagForChildDirectedTreatment;
18+
TagForUnderAgeOfConsentPicker.SelectedItem = AdConfig.TagForUnderAgeOfConsent;
19+
MaxAdContentRatingPicker.SelectedItem = AdConfig.MaxAdContentRating;
20+
}
21+
22+
protected override void OnDisappearing()
23+
{
24+
AdConfig.TagForChildDirectedTreatment = (TagForChildDirectedTreatment)TagForChildDirectedTreatmentPicker.SelectedItem;
25+
AdConfig.TagForUnderAgeOfConsent = (TagForUnderAgeOfConsent)TagForUnderAgeOfConsentPicker.SelectedItem;
26+
AdConfig.MaxAdContentRating = (MaxAdContentRating)MaxAdContentRatingPicker.SelectedItem;
27+
28+
base.OnDisappearing();
29+
}
30+
}

src/Plugin.AdMob/Config.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public static class Config
3030
/// <param name="iosDefaultNativeAdUnitId">Optional native ad unit ID to be used by default on Android. See <see cref="AdConfig.DefaultNativeAdUnitId" />.</param>
3131
/// <param name="disableConsentCheck">Default is false. When set to true, the plugin will no longer check if there is consent before making an ad request. You should use <see cref="IAdConsentService" /> to ask for consent and ensure you can request ads before making any ad request. If consent is missing, the Google Ads SDK will automatically drop the ad requests and no ads will be served.</param>
3232
/// <param name="automaticallyAskForConsent">Default is true. When set to false, the plugin will no longer ask for consent automatically. Use <see cref="IAdConsentService" /> to manage consent.</param>
33+
/// <param name="tagForChildDirectedTreatment">Indicate whether you want Google to treat your content as child-directed when you make an ad request. See <see cref="TagForChildDirectedTreatment" />.</param>
34+
/// <param name="tagForUnderAgeOfConsent">Indicate the treatment for users in the European Economic Area (EEA) under the age of consent. See <see cref="TagForUnderAgeOfConsent" />.</param>
35+
/// <param name="maxAdContentRating">Specifies the maximum ad content rating for your ad requests. AdMob ads returned when this is configured have a content rating at or below that level. See <see cref="MaxAdContentRating" />.</param>
3336
/// <returns>The source MAUI application builder.</returns>
3437
public static MauiAppBuilder UseAdMob(
3538
this MauiAppBuilder builder,
@@ -46,7 +49,10 @@ public static MauiAppBuilder UseAdMob(
4649
string? androidDefaultNativeAdUnitId = null,
4750
string? iosDefaultNativeAdUnitId = null,
4851
bool disableConsentCheck = false,
49-
bool automaticallyAskForConsent = true)
52+
bool automaticallyAskForConsent = true,
53+
TagForChildDirectedTreatment tagForChildDirectedTreatment = TagForChildDirectedTreatment.None,
54+
TagForUnderAgeOfConsent tagForUnderAgeOfConsent = TagForUnderAgeOfConsent.None,
55+
MaxAdContentRating maxAdContentRating = MaxAdContentRating.None)
5056
{
5157
#if ANDROID
5258
AdConfig.DefaultBannerAdUnitId = androidDefaultBannerAdUnitId;
@@ -65,6 +71,9 @@ public static MauiAppBuilder UseAdMob(
6571
#endif
6672

6773
AdConfig.DisableConsentCheck = disableConsentCheck;
74+
AdConfig.TagForChildDirectedTreatment = tagForChildDirectedTreatment;
75+
AdConfig.TagForUnderAgeOfConsent = tagForUnderAgeOfConsent;
76+
AdConfig.MaxAdContentRating = maxAdContentRating;
6877

6978
builder.ConfigureMauiHandlers(handlers =>
7079
{
@@ -78,7 +87,7 @@ public static MauiAppBuilder UseAdMob(
7887
builder.Services.AddSingleton<IAppOpenAdService, AppOpenAdService>();
7988
builder.Services.AddSingleton<INativeAdService, NativeAdService>();
8089

81-
IAdConsentService adConsentService = new AdConsentService();
90+
var adConsentService = new AdConsentService();
8291
builder.Services.AddSingleton<IAdConsentService>(adConsentService);
8392

8493
#if ANDROID || IOS

src/Plugin.AdMob/Configuration/AdConfig.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,19 @@ public static void AddTestDevice(string deviceId)
6262
{
6363
TestDevices.Add(deviceId);
6464
}
65+
66+
/// <summary>
67+
/// Allows you to indicate whether you want Google to treat your content as child-directed when you make an ad request.
68+
/// </summary>
69+
public static TagForChildDirectedTreatment TagForChildDirectedTreatment { get; set; } = TagForChildDirectedTreatment.None;
70+
71+
/// <summary>
72+
/// Allows you to indicate the treatment for users in the European Economic Area (EEA) under the age of consent.
73+
/// </summary>
74+
public static TagForUnderAgeOfConsent TagForUnderAgeOfConsent { get; set; } = TagForUnderAgeOfConsent.None;
75+
76+
/// <summary>
77+
/// Sets the maximum ad content rating for your ad requests. AdMob ads returned when this is configured have a content rating at or below that level.
78+
/// </summary>
79+
public static MaxAdContentRating MaxAdContentRating { get; set; } = MaxAdContentRating.None;
6580
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Plugin.AdMob;
2+
3+
/// <summary>
4+
/// Ad rating values. Each rating is cumulative.
5+
/// For example, if you choose T, the ad content will include ads rated G, PG, and T, but block ads rated MA.
6+
/// </summary>
7+
public enum MaxAdContentRating
8+
{
9+
/// <summary>
10+
/// No tag will be specified on ad requests.
11+
/// </summary>
12+
None,
13+
14+
/// <summary>
15+
/// Indicate that your app can only show G-rated ads.
16+
/// </summary>
17+
G,
18+
19+
/// <summary>
20+
/// Indicate that your app can only show G- and PG-rated ads.
21+
/// </summary>
22+
PG,
23+
24+
/// <summary>
25+
/// Indicate that your app can only show G-, PG- and T-rated ads.
26+
/// </summary>
27+
T,
28+
29+
/// <summary>
30+
/// Indicate that your app can show MA-rated ads.
31+
/// </summary>
32+
MA
33+
}

src/Plugin.AdMob/Platforms/Android/AppOpen/AppOpenAd.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Android.Gms.Ads;
2-
using Plugin.AdMob.Configuration;
2+
using Plugin.AdMob.Platforms.Android;
33
using Plugin.AdMob.Platforms.Android.AppOpen;
44

55
namespace Plugin.AdMob;
@@ -11,7 +11,7 @@ internal partial class AppOpenAd
1111
public void Load()
1212
{
1313
var configBuilder = new RequestConfiguration.Builder();
14-
configBuilder.SetTestDeviceIds(AdConfig.TestDevices);
14+
configBuilder.ApplyGlobalAdConfiguration();
1515
MobileAds.RequestConfiguration = configBuilder.Build();
1616

1717
var requestBuilder = new AdRequest.Builder();

src/Plugin.AdMob/Platforms/Android/Banner/BannerAdHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Android.Util;
33
using Microsoft.Maui.Handlers;
44
using Plugin.AdMob.Configuration;
5+
using Plugin.AdMob.Platforms.Android;
56
using Plugin.AdMob.Services;
67

78
namespace Plugin.AdMob.Handlers;
@@ -75,7 +76,7 @@ private void LoadAd(AdView adView)
7576
}
7677

7778
var configBuilder = new RequestConfiguration.Builder();
78-
configBuilder.SetTestDeviceIds(AdConfig.TestDevices);
79+
configBuilder.ApplyGlobalAdConfiguration();
7980
MobileAds.RequestConfiguration = configBuilder.Build();
8081

8182
var requestBuilder = new AdRequest.Builder();
@@ -117,7 +118,7 @@ private int GetScreenWidth()
117118
{
118119
DisplayMetrics displayMetrics;
119120

120-
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.R)
121+
if (OperatingSystem.IsAndroidVersionAtLeast(30))
121122
{
122123
displayMetrics = new DisplayMetrics();
123124
Context.Display!.GetMetrics(displayMetrics);

0 commit comments

Comments
 (0)