Sending custom analytics

If you have used the analytics service previously, you may have your own best practices for how to track your game usage; if so, feel free to use that. The method that we will present here is intended as a start for how you can go about setting up and sending custom analytics for training, or even for tracking player usage.

Let's begin by opening the Unity editor and following the next exercise:

  1. Create a new C# script called TestingAnalytics in the HoDLG Scripts folder.
  2. Open and edit the TestingAnalytics script in your editor, and enter the following code:
using UnityEngine;

namespace Packt.HoDLG
{
public class TestingAnalytics : Singleton<TestingAnalytics>
{
private TestingAcademy academy;
private TestingAgent[] agents;
private void Start()
{
academy = FindObjectOfType<TestingAcademy>();
agents = FindObjectsOfType<TestingAgent>();
}
public string CurrentGameState
{
get
{
var state = string.Empty;
foreach (var agent in agents)
{
foreach (var goal in academy.goals)
{
var distance = Vector3.Distance(goal.transform.position, agent.transform.position);
state += agent.name + " distance to goal " + distance + "/n";
}
}
return state;
}
}
}
}
  1. All this code does is collect the current position of the goals and how close they are to the agents. That is what we care about currently. Also, notice that we made this a public property, so that it can be called like a method, and not just a field. This will be important later on.
  2. Save the file and return to the editor. Confirm that there are no compiler errors.
  1. Create a new empty game object in the scene, and call it TestingAnalytics. Drag the new TestingAnalytics script on to the object to set it as a scene component. While the class is a singleton, we still want to add it as a dependency in the scene (essentially, as a reminder). However, there is another trick that we can also use to program prefabs.
  2. Drag the TestingAnalytics object into the HoDLG | Prefabs folder. This will make the object a prefab, which is now accessible by all of the other prefabs.
  3. Double-click on the goal prefab located in the HoDLG | Prefabs folder to open the object in its own mini editor.
  4. Use the Add Component button to add an Analytics Event Tracker component to the object and configure it, as shown in the following screenshot:

Setting up the Analytics Event Tracker
  1. Configure the component as follows:
    • When: Lifecycle
    • Lifecycle Event: On Destroy
    • Send Event:
      • Name: Goal Destroyed Event
      • Parameters: 1/10:
        • Name: Status
        • Value: Dynamic
        • Object: TestingAnalytics (Prefab)
        • Method: CurrentGameState
  2. Switch the scene back to the player mode by altering the Academy and Agent configuration.  
  3. Save the scene and the project.
  4. Run the scene by pressing Play, and drive over a goal. As you hit the goal, check the Analytics dashboard and note how the event is tracked.

At this stage, the analytics only report when a goal is destroyed, and they report how close each agent is to a goal. So, for one agent and three goals, they would report three distances when a goal was destroyed by driving over it or when the object was reset. By following these stats, you can generally view how each agent testing session is going overall, for better or for worse. Of course, you can add any manner of analytics that you want; it is easy to get carried away. Who knows; in the future, Unity may offer a self-testing platform driven by ML-Agents that provides testing analytics.

We are coming to the end of another chapter, and, of course, we are approaching your favorite section, Exercises.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset