Custom events can be any specific in-game action your user performs. They allow you to track player behavior Unity Analytics does not track automatically, such as level achievement, scene change, entering a store, or interacting with game objects. Each custom event can have its own parameters. Setting parameters on your event allows you to filter data collected at the time the event occurred. Visualization tools for Custom Events can be viewed in the Analytics Dashboard, including Data Explorer, Funnel Analyzer, and Segment Builder.
// Reference the Unity Analytics SDK namespace
using UnityEngine.Cloud.Analytics;
// Use this call for wherever a player triggers a custom event
UnityAnalytics.CustomEvent(string customEventName,
IDictionary<string, object> eventData);
UnityAnalytics.CustomEvent Input Parameters
Name
Type
Description
customEventName
string
Name of custom event. Name cannot include the prefix “unity.” — This is a reserved keyword.
eventData
dictionary
Additional parameters sent to Unity Analytics at the time the custom event was triggered. eventData key cannot include the prefix “unity.” — This is a reserved keyword.
A few considerations with regards to the custom events:
Be consistent! Maintain consistent data types for each parameter in your event data. For example, don’t send a level parameter as a number, then change it to be a string. Doing so can lead to erroneous behavior, making your data difficult to interpret.
Boolean (true/false)
String (characters)
Numbers (int, float, etc.).
Default limit of 10 parameters per custom event.
If there are more parameters passed, the call will fail with a return value of AnalyticsResult.TooManyItems
Default limit of 500 characters for the dictionary content.
If more than 500 characters are passed, the call will fail with return value of AnalyticsResult.SizeLimitReached
Default limit of 100 custom events per hour, per user.
If more than 100 events per hour are called, the call will fail with return value of AnalyticsResult.TooManyRequests
Consider how parameters are parsed by the Analytics system.
All numbers, ints, floats, etc., even if sent as strings, are parsed as numbers.
Only strings and Booleans are considered ‘categorizable’.
Consequently, if you want something to be summed or averaged, send it as a number (e.g., 51 or ‘51’). If you want it to be categorized, as you would with a level or option, make sure it will parse as a string (e.g., ‘Level51’).
In the example below we are interested in knowing what our user had in their inventory at the time the game ended.
// Reference the Collections Generic namespace
using System.Collections.Generic;
int totalPotions = 5;
int totalCoins = 100;
string weaponID = "Weapon_102";
UnityAnalytics.CustomEvent("gameOver", new Dictionary<string, object>
{
{ "potions", totalPotions },
{ "coins", totalCoins },
{ "activeWeapon", weaponID }
});
Press Play
To send test Custom Event data to our servers and validate your integration, trigger your Custom Event during Editor Play mode.
Did you find this page useful? Please give it a rating:
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!