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:

For all ad formats, use AdRequest with optional AdTargeting.

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();

        AdTargeting targeting = new AdTargeting(
            age: "25",
            gender: Gender.MALE,
            contextTags: new List<string>() { "games", "unity", "test" },
            contextQuery: "user_search_query",
            location: location
        );

        AdRequest adRequest = new AdRequest(currentAdUnitId, targeting: targeting);

        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();

    AdTargeting targeting = new AdTargeting(
        age: "25",
        gender: Gender.MALE,
        contextTags: new List<string>() { "games", "unity", "test" },
        contextQuery: "user_search_query",
        location: location
    );

    AdRequest adRequest = new AdRequest(CurrentAdUnitId, targeting: targeting);
    interstitialAdLoader.LoadAd(
        adRequest: adRequest,
        onLoaded: interstitial => { /* wire interstitial events and show */ },
        onFailed: args => Debug.LogError(args.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();

    AdTargeting targeting = new AdTargeting(
        age: "25",
        gender: Gender.MALE,
        contextTags: new List<string>() { "games", "unity", "test" },
        contextQuery: "user_search_query",
        location: location
    );

    AdRequest adRequest = new AdRequest(CurrentAdUnitId, targeting: targeting);
    rewardedAdLoader.LoadAd(
        adRequest: adRequest,
        onLoaded: rewardedAd => { /* wire rewarded events and show */ },
        onFailed: args => Debug.LogError(args.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();

    AdTargeting targeting = new AdTargeting(
        age: "25",
        gender: Gender.MALE,
        contextTags: new List<string>() { "games", "unity", "test" },
        contextQuery: "user_search_query",
        location: location
    );

    AdRequest adRequest = new AdRequest(CurrentAdUnitId, targeting: targeting);
    appOpenAdLoader.LoadAd(
        adRequest: adRequest,
        onLoaded: appOpenAd => { /* wire app open events and show */ },
        onFailed: args => OnAdFailedToLoad?.Invoke(args.Message));
}

Parameter

Description

age

The user's age in string format, for example: “14”, “18”.

gender

The user's gender: “male”, “female”.

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

location

The user's location as detected by your app.

To use location data, you must obtain the user's consent and then set YandexAds.SetLocationTracking(true) in the SDK.

contextQuery

The user's search query in the app.

For instance, if the user searched for a car, this parameter might look like this: “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 this: “buy a car”, “used car”, “in Moscow”.