SDK Event class
The Unity Analytics SDK is designed to help you match your game code with your event schemas as defined in the dashboard. While Analytics provides helper classes for all of the standard events that you might encounter, you must make your own helper classes to support the custom events that are unique to your game. This is done through making sub-classes of the abstract Event
class, giving you type safety when working with the same event across multiple locations, and can help you to prevent validation errors when the events are uploaded.
While the Event
class is primarily a container for passing data into the Analytics SDK, it also offers some advanced functions. You may also provide additional functionality through extending the Validate()
and Reset()
methods.
Validate
The Validate()
method is called just before event serialization and can be used during development to check locally whether an event matches its schema or not. For example, many of the standard events offered by the SDK display warnings if they are missing required fields using this method. You can also prevent an invalid event from being recorded at all by throwing an exception inside Validate()
.
Note that events that are recorded and uploaded are validated more thoroughly by the back-end, so take care in allowing invalid events to get through so that they are visible outside the application itself. This mechanism is primarily intended to provide fast feedback to assist initial implementation and debugging, where a loud failure may be useful.
protected override void Validate()
{
base.Validate();
if (!ParameterHasBeenSet("requiredParameter"))
{
Debug.LogWarning("A value for requiredParameter must be set!");
// Event will be recorded but a warning will be displayed in the log.
}
if (!ParameterHasBeenSet("doubleRequiredParameter"))
{
throw new Exception("A value for doubleRequiredParameter REALLY must be set!");
// Event will not be recorded at all.
}
}
Reset
The Event
class is designed to be pooled, so you can create a single instance and re-use it for the entire application lifecycle. Once an Event
has been serialized to the Analytics buffer, the Reset
method is invoked to clear the Event
of all its parameters, returning it to a blank state for re-use.
If you added any extra logic or fields of your own to your sub-class and want to make use of object pooling, you must extend the Reset
method to clear up any additional state you might have accumulated.
public override void Reset()
{
base.Reset();
m_MyExtraSystemValue = null;
}