Unity SDK tutorials

The tutorials show how to use the Analytics SDK 5.0.0+ to record analytics events in Unity.

Initial setup

Use the UnityServices.InitializeAsync() code at the start of your game to initialize the Analytics SDK. You must get consent from the player to collect their data. Once you confirm you have player consent, call AnalyticsService.Instance.StartDataCollection() to enable data collection.

As the game developer, you are responsible for the privacy and consent of your players. Data won’t be collected unless you inform the SDK that a player has consented. See the privacy page.

  • If a player wants to opt out, call the StopDataCollection() method.
  • If a player wants to delete their data, call the RequestDataDeletion() method.
  • If a player wants to opt in, call the StartDataCollection() method.

> Note: For more information about obtaining player consent, see Data Privacy.

The example below shows a setup method to initialize the Analytics SDK. Note that StartDataCollection() must be called at the start of each session to enable data collection. The SDK won't remember the player's consent status.

Ensure you use the latest SDK (5.0.2, though 5.0.0 is acceptable).

using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Analytics;

public class InitWithDefault : MonoBehaviour
{
    async void Start()
    {
		await UnityServices.InitializeAsync();

		AskForConsent();
    }

	void AskForConsent()
	{
		// ... show the player a UI element that asks for consent.
	}

	void ConsentGiven()
	{
		AnalyticsService.Instance.StartDataCollection();
	}
}

This is the minimum amount of code required to initialize the SDK and send events. The SDK automatically records the newPlayer event the first time StartDataCollection() is called for the given user ID, the gameStarted and clientDevice events each time StartDataCollection() is called, and the gameRunning event every minute. See Event Manager.

The Analytics SDK automatically populates every event with common parameters, such as userID, sessionID, eventTimestamp, eventUUID, platform and sdkVersion.

The names of parameters in events are case sensitive, and their values must match the types defined in the Event Manager. You can inspect the parameters that each event expects, their types, and any other restrictions in the Event Manager.

Send events behavior

Events are recorded into a buffer, which is flushed to the server every 60 seconds and on app shutdown. This period is not configurable.

There might be occasions where the SDK fails to send a batch of events, for example, when the Analytics service is down or the player is offline. Events are stored in memory until they're successfully uploaded or the app is shut down. If there's still no internet connection when the app is shut down, and file system access is available, up to 5MB of events is cached to disk so that they can be uploaded during the next session.

Events are always uploaded with the data they had when they were recorded, including common parameters like platform and session ID. For example, if an event was recorded in a previous session but only flushed days later, it'll keep its association with its original session.

You can also force events to be uploaded immediately by calling the AnalyticsService.Instance.Flush() method. You won’t need to do this in normal use.

Next step: Check the Event Browser in the Dashboard to confirm your events send successfully. They take up to 10 minutes to appear.