A/B testing

A/B testing is a powerful tool to test different variables on various user segments in parallel to see which variable has the most impact on your game. A/B tests can be very useful for fine-tuning retention, monetization, or balancing mechanics. This sample simulates an A/B test to evaluate how much experience it should take to gain a level.

Prerequisites

To use this sample use case, you must download and install the UGS Use Cases project in your Unity project.

Overview

To see this use case in action, open the samples menu and navigate to AB Test Level Difficulty. To open this scene directly and interact with the use case:

  1. In the Unity Editor Project window, select Assets > Use Case Samples > AB Test Level Difficulty, then double-click ABTestLevelDifficultySample.unity to open the sample scene.
  2. Press Play to enter Play mode.

When a player signs in, the screen displays their level and an XP meter that tracks their current XP and the total amount of XP required to level up. In this case, the amount of total XP to level up is either 100, 80, 60, 50, or 30, depending on which test group the player is randomly assigned. For diagnostic purposes, the player's test group is also displayed (A, B, C, D, or E).

Initialization

The ABTestLevelDifficultySceneManager.cs script performs the following initialization tasks in its Start function:

  1. Initializes Unity Services.
  2. Sends a SceneOpened custom event to the Analytics service. This, along with the SceneSessionLength custom event (sent when you click the scene's back button), tracks the player's session length as an example of how you can implement analytics to evaluate the results of your test.
  3. Signs in the player anonymously using the Authentication service. If you’ve previously initialized any of the other sample scenes, Authentication will use your cached Player ID instead of creating a new one. If not, your initial level (1) and XP (0) are saved in Cloud Save instead of loading existing data.
  4. Retrieves and updates the player's currency balances from the Economy service.
  5. Queries the Remote Config service to fetch the appropriate test group name and corresponding XP threshold for leveling up.

All of this updated data is displayed in the scene, and the Gain XP button becomes interactive.

Functionality

Gain XP

When you click the Gain XP button, the simulated player gains 10 XP. When the XP bar is full (according to the test group threshold), the simulated player levels up and receives currency. The following occurs:

  1. The button’s OnClick method sends an ActionButtonPressed custom event to the Analytics service and makes a call to the Cloud Code GainXPAndLevelIfReady.js script.

  2. This server-authoritative call fetches the player's information from the Cloud Save and Remote Config services, increases the XP by the amount specified by Remote Config (in this case, 10), then tests whether the new player XP total equals or exceeds the level-up threshold.

    • If the player did not level up, their new XP is saved in Cloud Save and returned to the client, which updates the values and shows text that indicates how much XP increased.
    • If the player did level up, it calls the Economy service to distribute the level-up rewards (in this case, 100 Coins), and calls Cloud Save to save the increased player level and new player XP before it returns that information to the client code.
  3. The client code opens a level-up dialog and updates the relevant data in the scene UI. Note that this step provides an opportunity to show players different art based on their segmentation, and a convenient way to retrieve additional data for a specific currency at runtime.

Note: In this example, a cross-reference dictionary located in Remote Config and initialized at start-up converts the rewarded currency ID (in this case, COIN) to an Addressables address, which you can use to display the sprite (in this case, Sprites/Currency/Coin). A simpler approach to displaying different art according to a player’s segmentation is to attach additional information to the custom data associated with Currencies in the Economy configuration data. However, here the data was added to the Campaigns to demonstrate the flexibility of the Remote Config service.

Sign In As New Player

Click Sign In As New Player to simulate a new player with a new randomly assigned test group that starts with 0 XP. The following occurs:

  1. The button’s OnClick method deletes the current anonymous player ID, resets all cached values to their empty or default states, and then initiates a new sign-in with the Authentication service that follows the same flow as when the scene first loads.
  2. As with the Gain XP button, it sends an ActionButtonPressed custom event to the Analytics service.
  3. Each time you click the Sign In As New Player button, the Authentication service creates a new anonymous player ID, to which the Remote Config service assigns a new A/B test group.

Back button

If you press the back button (the arrow in the top-left corner of the scene) to return to the "Start Here" or samples menu scene, it triggers a SceneSessionLength custom Analytics event, which captures the amount of time spent in this scene.

Setup

Requirements

To replicate this use case, you'll need the following Unity packages in your project:

PackageRole
AnalyticsSends events that track the player's in-game interactions, retention metrics, and other information that you can use to analyze and improve the game experience.
AuthenticationAutomatically signs in the player as an anonymous user to keep track of their data server-side.
Cloud CodeStores validation logic for increasing XP and leveling up the player server-side.
Cloud SaveProvides a server-authoritative way to save player data and the game state. In this example, the service stores the player's level and XP.
DeploymentProvides a cohesive interface to deploy assets for Cloud Services.
EconomyRetrieves the starting and updated currency balances at runtime.
Game OverridesAssigns players to different groups based on the requested distribution values. Players receive a different configuration of XP needed to level up depending on which group they're in.
Remote ConfigProvides key-value pairs that you can map and change server-side, either manually or based on specific Game Overrides. Also stores data associated with currency icon Addressable addresses.

Note: Although it is listed as a package and requires separate dashboard configuration, Game Overrides doesn't actually have an SDK to install from Package Manager. It is a server-side offering that affects values returned from other services.

To use these services in your game, activate each service for your Organization and project in the Unity Cloud Dashboard.

Unity Cloud services configuration

To replicate this sample scene's setup in your own Unity project, configure the following items:

  • Cloud Code scripts
  • Economy items
  • Remote Config values
  • Remote Config Game Overrides
  • Custom events and parameters for the Analytics service

To configure these items you can use the Deployment package, or manually enter them using the Unity Cloud Dashboard. The recommended best practice is to use the Deployment package as it greatly accelerates this process.

Using the Deployment package

To deploy configurations using the Deployment package:

  1. Open the Deployment window.
  2. Check in Common and AB Test Level Difficulty.
  3. Click Deploy Selection.

This deploys the following items:

  • Cloud Code scripts
  • Economy items
  • Remote Config values

The Deployment package doesn't support the following items:

  • Remote Config Game Overrides
  • Custom events and parameters for the Analytics service

To configure them, refer to Using the Unity Cloud Dashboard.

Using the Unity Cloud Dashboard

You can use the Unity Cloud Dashboard to manually configure your services by project and environment. Refer to the following sections to configure this sample.

Analytics

Important: This sample demonstrates the code that is needed to trigger Analytics events. However, additional code might be necessary to meet legal requirements such as GDPR, CCPA, and PIPL. For more information, see the documentation on managing data privacy.

Configure and enable the following custom Analytics events:

Event nameDescriptionCustom parameters
SceneOpenedSent each time you load the scene.- sceneName
ActionButtonPressedSent each time you click a button in the scene.- abGroup
- buttonName
- buttonNameByABGroup
- buttonNameBySceneName
- buttonNameBySceneNameAndABGroup
- sceneName
SceneSessionLengthSent to indicate the time between loading the scene and clicking the back button (effectively how much time the player spent in the scene). In this example, you could use this data to understand how much real-world time it takes each test group to level up.- abGroup
- sceneName
- timeRange
- timeRangeByABGroup
- timeRangeBySceneName
- timeRangeBySceneNameAndABGroup

Configure the following custom parameters to support your custom events:

Parameter nameTypeDescription
abGroupstringThe A/B group and A/B Test ID the player is assigned to. The string format is A/B group name (A/B test ID).
buttonNamestringThe name of the button that the player clicked.
buttonNameByABGroupstringGroups the name of the button clicked with the player's A/B group. The string format is Button name - A/B group name(A/B test ID).
buttonNameBySceneNamestringGroups the name of the button clicked with the current scene's name. The string format is Button name - Scene name.
buttonNameBySceneNameAndABGroupstringGroups the name of the button clicked with the scene's name and the player's A/B group. The string format is Button name - Scene name - A/B group name(A/B test ID).
sceneNamestringThe name of the scene where the event is triggered.
timeRangestringThe amount of time spent in the scene when the event is triggered.
timeRangeByABGroupstringGroups the time spent in the scene when the event is triggered with the player's A/B group. The string format is Time range - A/B group name(A/B test ID).
timeRangeBySceneNamestringGroups the time spent in the scene when the event is triggered with the scene's name. The string format is Time range - Scene name.
timeRangeBySceneNameAndABGroupstringGroups the time spent in the scene when the event is triggered with the scene's name and the player's A/B group. The string format is Time range - Scene name - A/B group name (A/B test ID).

Note: This extended list of potential parameters allows for a more flexible analysis of different parameter groupings in the Data Explorer in the Unity Cloud Dashboard. Alternatively, you can send only the ungrouped parameters (for example, buttonName or sceneName) and perform any kind of grouped analysis you want by using the Data Export feature within the Data Explorer.

Cloud Code

Publish the following script in the Unity Cloud Dashboard:

ScriptParametersDescriptionLocation in project
GainXPAndLevelIfReadyNoneIncrements the player's XP and checks to see if the cumulative XP exceeds the level-up threshold defined by their A/B test group.Assets/Use Case Samples/AB Test Level Difficulty/Cloud Code/GainXPAndLevelIfReady.js

Note: The Cloud Code scripts included in the Cloud Code folder are local copies because you cannot view the sample project's dashboard. Changes to these scripts do not affect the behavior of this sample because they are not automatically uploaded to the Cloud Code service.

Economy

Configure the following resource in the Unity Cloud Dashboard:

Resource typeResource nameIDDescription
CurrencyCoinCOINThe currency that is distributed as a reward for the player leveling up.

Remote Config

Set up the following config values in the Unity Cloud Dashboard:

KeyTypeDescriptionValue
A_B_TEST_GROUPstringThe identifier for which test user group the player is in.empty default value
A_B_TEST_IDstringThe identifier for which AB Test is actively being run for this user.empty default value
LEVEL_UP_XP_NEEDEDintThe amount of XP needed in order for the player to level up.100
XP_INCREASEintThe amount the player's XP will increase by each time they gain XP.10
CURRENCIESJSONA cross reference from currencyId to spriteAddresses for all currency types.
json
{
	"currencyData": [{
    	"currencyId": "COIN",
        "currencySpec": {
        	"spriteAddress": "Sprites/Currency/Coin"
        }
	},{
    	"currencyId": "GEM",
        "currencySpec": {
        	"spriteAddress": "Sprites/Currency/Gem"
		}
	},{
		"currencyId": "PEARL",
        "currencySpec": {
			"spriteAddress": "Sprites/Currency/Pearl"
    	}
	},{
    	"currencyId": "STAR",
        "currencySpec": {
        	"spriteAddress": "Sprites/Currency/Star"
		}
	}]
}

Game Overrides

Configure the following Overrides in the Unity Cloud Dashboard:

DetailsName the Override “Level Difficulty A/B Test”.
TargetingSelect Audiences with 100% of audiences targeted.
Content

Select Choose content type > Config Overrides.

Enter override values for the following keys for variant 1:

  • A_B_TEST_GROUP: "A"
  • A_B_TEST_ID: "LevelDifficultyTest1"
  • LEVEL_UP_XP_NEEDED: 100

Enter override values for the following keys for variant 2:

  • A_B_TEST_GROUP: "B"
  • A_B_TEST_ID: "LevelDifficultyTest1"
  • LEVEL_UP_XP_NEEDED: 80

Enter override values for the following keys for variant 3:

  • A_B_TEST_GROUP: “C
  • A_B_TEST_ID: “LevelDifficultyTest1
  • LEVEL_UP_XP_NEEDED: 60

Enter override values for the following keys for variant 4:

  • A_B_TEST_GROUP: “D
  • A_B_TEST_ID: “LevelDifficultyTest1
  • LEVEL_UP_XP_NEEDED: 50

Enter override values for the following keys for variant 5:

  • A_B_TEST_GROUP: “E
  • A_B_TEST_ID: “LevelDifficultyTest1
  • LEVEL_UP_XP_NEEDED: 30
Scheduling

Set the following start and end dates:

  • Set Start Date to Update content immediately.
  • Set End Date to Run indefinitely.
Status

After finishing creating the Game Override, click Enable.