Unity and Analytics
I started using Fabric, then changed to the inbuilt unity package for analytics.
Fabric
I received an email from Twitter about their Fabic api. Though I'd try it out. Since I'm not makeing mobile build it's pretty useless. It requires running on a device to complete the setup.
UnityEngine.Analytic
Requires a pretty basic setup using your unity account.
To Start open Unity's `Services` tab (
C-0
orWindow
->Services
) and follow the prompt
Unity's API
The UnityEngine.Analytics api is super simple, but I still put a static wrapper class over the top.
using System; using System.Collections.Generic; using System.Text; using UnityEngine; namespace CustomUnityHelpers { public static class Analytics { public static bool EnableLog { get;set; } public static void AsAnalyticsEvent(this object obj, string eventName) { IDictionary<string,object> logDict = default(Dictionary<string,object>); var analyticsObject = obj as IToAnalytics; if (analyticsObject != null) logDict = analyticsObject.ToAnalytics(); var dictObject = obj as IDictionary<string, object>; if (logDict == null && dictObject != null) logDict = dictObject; if (logDict == null) logDict = new Dictionary<string, object>; if (EnableLog) Debug.LogFormat("[Analytic::eventName] - {0}\n[Analytic::dict] - {1}", eventName, logDict.ToCustomString()); UnityEngine.Analytics.Analytics.CustomEvent(eventName, logDict); } public static void SendUserDetails(string userId, UnityEngine.Analytics.Gender gender, int birthYear) { if (EnableLog) Debug.LogFormat("[Analytic::userId] - {0}\n[Analytic::gender] - {1}\n[Analytic::birthYear] - {2}", userId, gender, birthYear); if (!String.IsNullOrEmpty(userId)) UnityEngine.Analytics.Analytics.SetUserId(userId); UnityEngine.Analytics.Analytics.SetUserGender(gender); UnityEngine.Analytics.Analytics.SetUserBirthYear(birthYear); } } }
Why does this work?
Don't forget the check your data on the dashboard. This will show the analytics while you're developing (ie. Play in the Editor)
- On the your project dashboard (https://analytics.cloud.unity3d.com/integration/<INSERT-PROJECT-GUID>?unityversion=52)
- On the
Integration Tab
- Select
Play To Validate
- Scroll down to validate
Validate
Don't use Awake, Start or Quit for Analytics events. there is no guarentee the anayltics to connected, or that it is still running (it stops in the quite methods).