Reducing ad frequency for engaged players
Scenario
This document describes how to use a a sample game that illustrates:
- How to display an ad to a player at the time that the player's character is eliminated ("dies").
- How you can 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).
We’ll be working with a Unity project called “Platformer Microgame”, but this use case can be applied to any other game.
Requirements
- Unity 2019.4 and above
- Unity Ads*
- Authentication
- Remote Config
- Sign up to Unity Analytics Beta
Note: Unity Ads will not be covered for this example. However, you can call the ShowAd function within your premade class. See Unity advertising.
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
Log in to the Unity Cloud Dashboard and find the packages (Analytics, Authentication, and Remote Config) required for this use case.
Download and install each package. Add the package directly to your project’s PACKAGES folder. Open the manifest.json file and append the following to the dependencies:
"com.unity.services.authentication": "1.0.0-pre.4",
"com.unity.services.analytics": "3.0.0-pre.2",
"com.unity.remote-config": "3.0.0-pre.3"
Link your game
In the Unity Editor, click 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
There are two ways of setting up a Remote Config key: from the Unity Cloud Dashboard or from the Editor.
In the Unity Cloud Dashboard, open Remote Config.
To add the config key that will be changed for engaged players:
- Click Add Key.
- Enter the key name
adFrequency
. - Select the type
Integer
and enter a value of 1. - Click Add.
This will be the default value for all players.
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’ll initialize an anonymous user. You now have the config value of adFrequency from Remote Config.
For example, if the adFrequency value is changed to 3 then you want to show an ad every third time 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 won’t be displayed.
countDeath++;
if(ugs.adFrequency == countDeath)
{
countDeath = 0;
PlayerPrefs.SetInt("countDeathForAds",countDeath);
ads.ShowAd();
}
Setting up an Override
The initial setup is complete after following the above steps.
Goal for this example: 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.
Click Create Override.
Enter a name for your Override then click Next.
Click Audiences, select the pre-defined Audience “Engaged Players”, then click Next.
- Note: To enable Stateful Audiences ensure Unity Analytics is installed.
Click Choose Content Type, select Config Overrides, and click Done.
Select adFrequency from the Key Name dropdown, enter a value of 3, and click Next.
Schedule the Override to run immediately, indefinitely, with a medium priority, and click Finish. You can adjust the priority to manage potential conflicts between Overrides.
Check your changes and click 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.