Reducing ad frequency for engaged players
Scenario
This topic describes how to use a sample game that illustrates:
- How to display an ad to a player when their character is eliminated ("dies").
- How to target engaged players.
- How to change the frequency of engaged players they see. (An engaged player is a player who has played every day for the past seven days).
This example uses a Unity project called Platformer Microgame, but you can apply this use case to any other game.
Requirements
- Unity 2019.4 and above
- Unity Ads*
- Authentication
- Remote Config
- Unity Analytics
Note: This example doesn't cover Unity Ads. However, you can call the ShowAd
function within your premade class.
Instructions
Here's an overview of the steps. Each step is described in more detail below.
- Install Authentication, Remote Config, and Analytics.
- Link your game to Analytics in the Unity Editor.
- Set up a Remote Config key with a default value.
- Retrieve values from Remote Config in your game.
- Decide when to show ads.
- Set up an Override to adjust your Remote Config key for an Audience.
Install Analytics, Authentication and Remote Config packages
In the Unity Editor, install the Analytics, Authentication, and Remote Config packages.
- In the Unity Editor, open Window > Package Manager.
- In the Package Manager, select the Unity Registry list view.
- Select and install the following packages:
- Analytics (com.unity.services.analytics)
- Authentication (com.unity.services.authentication)
- Remote Config (com.unity.remote-config)
Link your game
In the Unity Editor, select Edit > Project Settings > Services. Choose the relevant project and link it. This will link your project to the Unity Cloud Dashboard.
Set up a Remote Config key
You can set up a Remote Config key in the Unity Cloud Dashboard or in the Editor.
In the Unity Cloud Dashboard, open Remote Config.
To add the config key to change for engaged players:
- Select Add Key.
- Enter the Name
adFrequency
. - Select the Type
Integer
. - Select Next.
- Add an Integer value of 1. This will be the default value for all players.
- Select Submit.
- In the Remote Config overview, select Publish to publish your key and make it available for other services to use.
Alternatively, you can update these config values in the Unity Editor. Click Window in the menu bar, then Remote Config: it has a similar editing panel as the Unity Cloud Dashboard.
Setting up your game
Use the following code to fetch the adFrequency
data from Remote Config. Attach this code to an empty object in the scene.
using Unity.RemoteConfig;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
public class UGS : MonoBehaviour
{
public struct userAttributes { }
public struct appAttributes { }
public int adFrequency = 1; //Default is 1 ad on death
async void Start()
{
await UnityServices.InitializeAsync();
// remote config requires authentication for managing environment information
if (!AuthenticationService.Instance.IsSignedIn)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
ConfigManager.FetchCompleted += ConfigManager_FetchCompleted;
ConfigManager.FetchConfigs(new userAttributes(), new appAttributes());
}
void ConfigManager_FetchCompleted(ConfigResponse configResponse)
{
switch (configResponse.requestOrigin)
{
case ConfigOrigin.Default:
Debug.Log("Default values will be returned");
break;
case ConfigOrigin.Cached:
Debug.Log("Cached values loaded");
break;
case ConfigOrigin.Remote:
Debug.Log("Remote Values loaded");
adFrequency = ConfigManager.appConfig.GetInt("adFrequency");
break;
}
}
}
When the object loads, it initializes an anonymous user. You now have the config value of adFrequency
from Remote Config. This defines how often your game displays an ad when a player dies or fails the level.
The death logic counts the number of times a player dies. If the death count is equal to the frequency, the ad is displayed. If not, the ad isn't displayed.
countDeath++;
if(ugs.adFrequency == countDeath)
{
countDeath = 0;
PlayerPrefs.SetInt("countDeathForAds",countDeath);
ads.ShowAd();
}
The initial setup is complete after following the above steps.
Setting up an Override
This example explains how to create an Override to target engaged players, lowering adFrequency
to show an ad on every third death. Non-engaged players will continue seeing an ad every time they die.
In the Unity Cloud Dashboard, open Game Overrides.
Select Create Override.
Enter a name for your Override then select Next.
Select Audiences, select the pre-defined Audience Engaged Players, then select Next.
- Note: To enable Stateful Audiences ensure Unity Analytics is installed.
Select Choose Content Type, select Config Overrides, and select Done.
Select adFrequency from the Key Name dropdown, enter a value of 3, and select Next.
Schedule the Override to run immediately, indefinitely, with a medium priority, and select Finish. You can adjust the priority to manage potential conflicts between Overrides.
Check your changes and select Enable.
Your Override is now active and engaged players will receive an ad on every third death.
If you have any questions, open a support ticket.