Unity Analytics

Out of the box, Unity Analytics is something you should have on anytime you release a game. It is essential to provide feedback about your players, the distribution of your game, and many other metrics. We will cover some of the basics of Unity Analytics in this section and then take a quick deep dive into using the tool to track critical errors and exceptions. Being able to access this information will provide you with better support for your game during off-site testing or release.

Follow the given instructions to enable Unity Analytics on your project:

  1. From the menu, select Window | Services. The Services window will open, typically over the Inspector window.

    Tip

    Performance Reporting will also report on errors, but that requires an upgraded Unity account.

  2. Locate the Analytics group in the list and set the toggle button on the right to the On position, as shown in the following screenshot:

    Unity Analytics

    Turning Unity Analytics on

  3. Click on the Analytics panel in the Services window. You may get prompted to confirm the age guidelines of your game. If you do, just select an age greater than 13 and continue.
  4. The Analytics page will load in the Services window. Click on the Go to Dashboard button just below the main text. This will open your default browser and take you to the Unity Analytics site. You may have to log in with your Unity account, just do so when prompted.
  5. Once the page loads, you should see something close to the following screenshot:

    Unity Analytics

The Unity Analytics project page

Right now, your players, sessions, and other metrics will likely be sitting at 0. This is expected since you likely just enabled analytics on your project. Now, by itself, Unity Analytics won't track logging messages, but we can use some of the custom event mechanisms to track errors or exception logs. Fortunately, all the work for this has been encapsulated into a script called AnalyticsLogHandler.

Note

Unity Analytics does not run in real time, which means that you will typically have to wait 12-14 hours to see results. While this makes it essentially useless for debugging, it can be a useful metric when deployed to players globally.

Follow the given directions to set up this script:

  1. From the menu, select GameObject | Create Empty. Rename the object to AnalyticsLogHandler and reset the transform to zero.
  2. Drag the AnalyticsLogHandler script from the Assets/Chapter 10/Scripts folder in the Project window onto the AnalyticsLogHandler object.
  3. The RotateObject script that is attached to the Cube in the sample Main scene logs an error message every time the object completes an entire rotation.
  4. Press Play to run the scene in the editor. Wait for a few of those rotation error messages to log to the status bar, Console window, or CUDLR window. Unfortunately, you will have to wait an additional 12-14 hours before the message will appear in the Unity Analytics dashboard page.
  5. After 14 hours, return to the analytics dashboard and review the event viewer messages by selecting the Event Manager tab, as shown in the following screenshot:

    Unity Analytics

    The Event Manager tab on the Unity Analytics dashboard

Let's take a look at the AnalyticsLogHandler script, as illustrated:

using UnityEngine; 
using UnityEngine.Analytics; 
 
public class AnlyticsLogHandler : MonoBehaviour 
{ 
    public LogType logLevel = LogType.Error; 
    void Awake() 
    { 
        Application.logMessageReceived += Application_logMessageReceived; 
    } 
 
    private void Application_logMessageReceived(string condition, string stackTrace, LogType type) 
    { 
        if (type == logLevel) 
        { 
            Analytics.CustomEvent("LOG", new Dictionary<string, object> 
            { 
                { "msg", condition }, 
                { "type", type.ToString() } 
            }); 
        } 
    } 
} 

This script is simpler, but the implementation is similar to the CustomLogHandler script we looked at earlier. There is one major difference and that is the use of the Analytics object to create a CustomEvent that then automatically gets sent to Unity Analytics. In this case, we are using the method to track logging messages or other error conditions. Of course, you could add that snippet of code anywhere you wanted to track a custom event in your game.

Note

The Event Manager uses a system of points to determine how many events you can track and perform other analytics on. Every project starts with 1,000 points, which should cover most of your tracking needs.

Once you start collecting custom events, be it from errors or other regular game activity, you can plot these metrics against other metrics. For instance, you could plot the number of errors per user or as per the number of game sessions, which would tell you how critical or benign an error is and how quickly you may need to respond. Setting up those plots is outside the scope of this book but worth some investigation on the Unity site when you have time to do so.

As you saw, analytics can be a great tool for not only tracking player activity, but also other activities, such as errors or other custom events. In the next and final section of the book, we will look at other issues you may have encountered during the course of this book, with possible solutions, of course.

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

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