Ad targeting

Targeting is used to set precise parameters so that users can see ads that are more relevant to them. Here's an example of using targeting:

Format

Class

  • Sticky banners

  • Inline banners

  • Fixed banners

AdRequest

  • Interstitial ads

  • Rewarded ads

  • App open ads

AdRequestConfiguration

BannerComponent.cs

    private void Start()
    {
    if (!string.IsNullOrEmpty(currentAdUnitId))
    {
        ConfigureBanner();

        double lat = 60.0, lon = 30.0;

        // Build the Location object
        Location location = new Location.Builder()
            .SetLatitude(lat)
            .SetLongitude(lon)
            .Build();

        // Create targeting parameters
        var adRequest = new AdRequest.Builder()
            .WithAge("25")
            .WithGender("male") // or "female", "other"
            .WithContextTags(new List<string>() { "games", "unity", "test" })
            .WithContextQuery("user_search_query") // search query string
            .WithLocation(location)
            .WithParameters(new Dictionary<string, string> {
                    { "custom1", "value1" },
                    { "custom2", "value2" }
            })
            .Build();

        banner.LoadAd(adRequest);
    }
    else
    {
        Debug.LogError("Banner configuration failed. Ad Unit ID is missing.");
    }
    }

InterstitialAdComponent.cs

private void ConfigureAd()
{
    double lat = 60.0, lon = 30.0;

    // Build the Location object
    Location location = new Location.Builder()
        .SetLatitude(lat)
        .SetLongitude(lon)
        .Build();

    AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(CurrentAdUnitId)
            .WithAge("25")
            .WithGender("male") // or "female", "other"
            .WithContextTags(new List<string>() { "games", "unity", "test" })
            .WithContextQuery("user_search_query") // search query string
            .WithLocation(location)
            .WithParameters(new Dictionary<string, string> {
                    { "custom1", "value1" },
                    { "custom2", "value2" }
            }).Build();
    try
    {
        interstitialAdLoader.LoadAd(adRequestConfiguration);
    }
    catch (Exception ex)
    {
        Debug.LogError($"Configuration failed: {ex.Message}");
    }
}

RewardAdComponent.cs

private void ConfigureAd()
{
    double lat = 60.0, lon = 30.0;

    // Build the Location object
    Location location = new Location.Builder()
        .SetLatitude(lat)
        .SetLongitude(lon)
        .Build();

    AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(CurrentAdUnitId)
            .WithAge("25")
            .WithGender("male") // or "female", "other"
            .WithContextTags(new List<string>() { "games", "unity", "test" })
            .WithContextQuery("user_search_query") // search query string
            .WithLocation(location)
            .WithParameters(new Dictionary<string, string> {
                    { "custom1", "value1" },
                    { "custom2", "value2" }
            }).Build();
    try
    {
        rewardedAdLoader.LoadAd(adRequestConfiguration);
    }
    catch (Exception ex)
    {
        Debug.LogError($"{adName}: Configuration failed: {ex.Message}");
    }
}

AppOpenAdComponent.cs

private void ConfigureAd()
{
    double lat = 60.0, lon = 30.0;

    // Build the Location object
    Location location = new Location.Builder()
        .SetLatitude(lat)
        .SetLongitude(lon)
        .Build();

    AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(CurrentAdUnitId)
            .WithAge("25")
            .WithGender("male") // or "female", "other"
            .WithContextTags(new List<string>() { "games", "unity", "test" })
            .WithContextQuery("user_search_query") // search query string
            .WithLocation(location)
            .WithParameters(new Dictionary<string, string> {
                    { "custom1", "value1" },
                    { "custom2", "value2" }
            }).Build();
    try
    {
        appOpenAdLoader.LoadAd(adRequestConfiguration);
    }
    catch (Exception ex)
    {
        OnAdFailedToLoad?.Invoke($"Configuration failed: {ex.Message}");
    }
}

Parameter

Description

age

The age of a user: a number in the string format (for example, "14", "18", and so on).

gender

The gender of a user: "male", "female".

You can use the Gender object to get a valid string.

location

The location of a user as detected by your app.

To use location data, you must obtain the user's consent to do so and then set MobileAds.setLocationConsent(true) in the SDK.

contextQuery

The user's search query in the app.

For instance, if a user searched for a car, this parameter might look like that: "Buy a used car in Moscow".

contextTags

The keywords from the page the user was viewing. These could be the page's title, parts of its content, tags, and other elements.

For a page with a car search, this parameter might look like that: "buy a car", "used car", "in Moscow".