Customizing events through code

It's great to have a way to do the events through the editor, but for those who are more comfortable working with code, it's also just as easy to do so.

One additional thing that we may want to track is how far players get before they lose. Let's take a look at how to do that now:

  1. First, we will need to open up the ObstacleBehaviour script to modify what happens when the game ends.
  2. To utilize Unity Analytics at the top of the file, we will add the following using declarations:
using UnityEngine.Analytics; // Analytics
using System.Collections.Generic; // Dictionary

The top option is obvious, but we are also adding System.Collections.Generic in order to get access to the Dictionary class, which we will use in the next piece of code.

  1. Next, we will update the OnCollisionEnter function to the following:
    void OnCollisionEnter(Collision collision)
{
var playerBehaviour =
collision.gameObject.GetComponent<PlayerBehaviour>();

// First check if we collided with the player
if (playerBehaviour != null)
{
// Destroy the player
collision.gameObject.SetActive(false);

player = collision.gameObject;

var eventData = new Dictionary<string, object>
{
{ "score", playerBehaviour.Score }
};

Analytics.CustomEvent("Game Over", eventData);

// Call the function ResetGame after waitTime has passed
Invoke("ResetGame", waitTime);
}
}

We've done a number of things within this script. To start off with, we have rewritten our check for the player to use the component as a variable now so that we don't have to call GetComponent again for the same thing.

Aside from that, the main addition is the calling of the Analytics.SendEvent function. This function takes in two parameters. The first is a string, which is the name that you wish for the event to have. The second parameter (which is optional) is a dictionary, which we haven't discussed yet.

A dictionary is a class that represents a pair of keys and values. The key is an identifier of some sort, which allows us to have a reference to obtain the value. This is most often used with strings as the key so that you can refer to some other data type.

For more information on dictionaries, check out http://csharp.net-informations.com/collection/dictionary.htm.
  1. Save the script and return to the Unity Editor.
  2. Play the game and lose it. You will note in the Validator that now you are sending a Game Over event with your score value:

You may also dive into the Dashboard to see the information as well, but it may take up to 6 hours before it will be visible. Events typically take a few hours to cycle. You will see them instantly on the Validator (to help you confirm that your code is working), but they don't populate into analytics until backend calculations have been processed on Unity's end due to all of the events they recieve.

  1. After you've waited, go ahead to the Analytics tab and click on the Go to Dashboard button once again.

We can use the Data Explorer like previously, but, in our case, we may just want to see all of the data from times it was called. For that, we can make use of the Event Manager.

  1. Once there, go to the More tab and click on it. From the dropdown that pops up, click on Event Manager:

The Event Manager is the location where users can see the custom events that were received from users playing the game as well as the parameters that were passed to them.

  1. Scroll down to the Custom Events header--you'll see a list of all of them under it, with the Paused option and the Game Over event created in this section:

If you click on the dropdown, you should be able to see the score property, and clicking on that will show all the values received whenever it was called.

Instead of a single value, we can pass up to 10 parameters into the dictionary. However, the value must be one of the following types:

Remember that you can always convert an object to a string in C# using the ToString function.

You can only send 100 custom events per hour per user, so you should not be doing too many custom events in the game. I suggest that you create custom events for whenever a user reaches an important milestone, for example, when they level up or if they make an  In-App Purchase (IAP).

For more information on Analytics.CustomEvent and other ways it can be called, check out https://docs.unity3d.com/ScriptReference/Analytics.Analytics.CustomEvent.html.