Legacy Documentation: Version 5.2
Advanced Integration
Monetization

Custom Events

Custom events can be any specific in-game action your user performs. It allows 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 parameter. Setting parameters on your further allows you to filter data collected at the time the event occurred. Visualization tools for Custom Events occur frequently in the Analytics Dashboard, including in Data Explorer, Funnel Analyzer, and Segment Builder.

// Reference the Unity Analytics namespace
using UnityEngine.Analytics;

//  Use this call for each and every place that a player triggers a custom event
Analytics.CustomEvent(string customEventName,
IDictionary<string, object> eventData);
Analytics.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:

  • At least one non-Null parameter must be passed with each custom event.
    • Passing an integer value parameter will have the lowest effect on your Analysis Points.
    • Custom events with no or a Null parameter will not be processed.
  • 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 they reached the Game Over scene of our game.

// Reference the Collections Generic namespace
  using System.Collections.Generic;

  int totalPotions = 5;
  int totalCoins = 100;
  string weaponID = "Weapon_102";
  Analytics.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. If integration is successful, your test data will display in the table below.

Validate

Advanced Integration
Monetization
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961