Initial setup

Events can be recorded with the Analytics SDK immediately after initializing the service by adding the UnityServices.InitializeAsync() code at the start of your game.

However, for events to be sent to the service, you need to check if any privacy consents are required using the AnalyticsService.Instance.CheckForRequiredConsents() method. See Data Privacy.

The example below shows a sample setup method that can be used before calling it into the Analytics SDK.

Copy
using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Analytics;
using System.Collections.Generic;

public class InitWithDefault : MonoBehaviour
{
    async void Start()
    {
        try
        {
            await UnityServices.InitializeAsync();
            List<string> consentIdentifiers = await AnalyticsService.Instance.CheckForRequiredConsents();
        }
        catch (ConsentCheckException e)
        {
          // Something went wrong when checking the GeoIP, check the e.Reason and handle appropriately.
        }
    }
}

This is the minimum amount of code required to initialize the SDK and start sending events. It'll automatically send the newPlayer event the first time the SDK is run, the gameStarted and clientDevice event each time the game runs, and the gameRunning event every minute. See Event Manager.

The Analytics SDK will automatically populate the userID, sessionID, eventTimestamp, eventUUID, platform and sdkVersion parameters in the code for every event you send. All parameters added to an events are triggered inside the eventParams object.

The order of the parameters in your JSON event doesn't matter, but they're case sensitive and need to match the type defined in the Event Manager. You can inspect the parameters that each event expects, their type, and any formatting in the Event Manager.