Skip to content

Commit 063e5c8

Browse files
update
Finalizing the first round pass of gathering analytics.
1 parent a17194f commit 063e5c8

File tree

8 files changed

+96
-10
lines changed

8 files changed

+96
-10
lines changed

com.unity.netcode.gameobjects/Editor/Analytics.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using UnityEngine.Analytics;
4+
5+
namespace Unity.Netcode.Editor
6+
{
7+
internal class AnalyticsHandler<T> : IAnalytic where T : IAnalytic.IData
8+
{
9+
private T m_Data;
10+
11+
public AnalyticsHandler(T data)
12+
{
13+
m_Data = data;
14+
}
15+
public bool TryGatherData(out IAnalytic.IData data, out Exception error)
16+
{
17+
data = m_Data;
18+
error = null;
19+
return data != null;
20+
}
21+
}
22+
}
23+
#endif

com.unity.netcode.gameobjects/Editor/Analytics/AnalyticsHandler.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.netcode.gameobjects/Editor/Analytics/NetworkManagerAnalytics.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
#if UNITY_EDITOR
2+
using System;
23
using System.Text;
34
using UnityEngine;
5+
using UnityEngine.Analytics;
46

5-
namespace Unity.Netcode
7+
namespace Unity.Netcode.Editor
68
{
7-
internal struct NetworkManagerAnalytics
9+
[Serializable]
10+
internal struct NetworkManagerAnalytics : IAnalytic.IData
811
{
912
public bool IsUsingMultiplayerSDK;
1013
public bool UsedCMBService;
1114
public string NetworkTopology;
1215
public string NetworkTransport;
1316
public bool PlayerPrefabSet;
1417
public bool ConnectionApproval;
15-
public float ClientConnectionBufferTimeout;
18+
public int ClientConnectionBufferTimeout;
1619
public bool EnsureNetworkVariableLengthSafety;
1720
public bool EnableSceneManagement;
18-
public float LoadSceneTimeOut;
21+
public int LoadSceneTimeOut;
1922
public float SpawnTimeout;
2023
public bool ForceSamePrefabs;
2124
public bool RecycleNetworkIds;
2225
public float NetworkIdRecycleDelay;
2326
public int RpcHashSize;
2427
public bool EnableTimeResync;
25-
public float TimeResyncInterval;
28+
public int TimeResyncInterval;
2629
public int TickRate;
2730
public bool IsUsingMultiplayerTools;
2831
public bool NetworkMessageMetrics;
2932
public bool NetworkProfilingMetrics;
3033
public bool WasServer;
3134
public bool WasClient;
3235
public float SessionDuration;
33-
internal void LogAnalytics(int sessionNumber)
36+
37+
public override string ToString()
3438
{
3539
var message = new StringBuilder();
36-
message.AppendLine($"{nameof(NetworkManagerAnalytics)}-{sessionNumber} Session Duration: {SessionDuration} Sever: {WasServer} Client: {WasClient}");
40+
message.AppendLine($"{nameof(WasServer)}: {WasServer}");
41+
message.AppendLine($"{nameof(WasClient)}: {WasClient}");
42+
message.AppendLine($"{nameof(SessionDuration)}: {SessionDuration}");
3743
message.AppendLine($"{nameof(IsUsingMultiplayerSDK)}: {IsUsingMultiplayerSDK}");
3844
message.AppendLine($"{nameof(UsedCMBService)}: {UsedCMBService}");
3945
message.AppendLine($"{nameof(NetworkTopology)}: {NetworkTopology}");
@@ -55,7 +61,11 @@ internal void LogAnalytics(int sessionNumber)
5561
message.AppendLine($"{nameof(IsUsingMultiplayerTools)}: {IsUsingMultiplayerTools}");
5662
message.AppendLine($"{nameof(NetworkMessageMetrics)}: {NetworkMessageMetrics}");
5763
message.AppendLine($"{nameof(NetworkProfilingMetrics)}: {NetworkProfilingMetrics}");
58-
Debug.Log($"{message}");
64+
return message.ToString();
65+
}
66+
internal void LogAnalytics(int sessionNumber)
67+
{
68+
Debug.Log($"{ToString()}");
5969
}
6070
}
6171
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#if UNITY_EDITOR
2+
using UnityEngine.Analytics;
3+
4+
namespace Unity.Netcode.Editor
5+
{
6+
[AnalyticInfo("NGO_NetworkManager", "unity.netcode", 3, 100, 1000)]
7+
internal class NetworkManagerAnalyticsHandler : AnalyticsHandler<NetworkManagerAnalytics>
8+
{
9+
public NetworkManagerAnalyticsHandler(NetworkManagerAnalytics networkManagerAnalytics) : base(networkManagerAnalytics) { }
10+
}
11+
}
12+
#endif

com.unity.netcode.gameobjects/Editor/Analytics/NetworkManagerAnalyticsHandler.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,40 @@ public bool NotifyUserOfNestedNetworkManager(NetworkManager networkManager, bool
227227
}
228228

229229
private const bool k_EnableAnalyticsLogging = true;
230-
230+
/// <summary>
231+
/// Invoked from within <see cref="NetworkManager.ModeChanged"/> when exiting play mode.
232+
/// </summary>
231233
public void UpdateAnalytics()
232234
{
235+
// Exit early if analytics is disabled
233236
if (!EditorAnalytics.enabled)
234237
{
235238
return;
236239
}
240+
241+
// Parse through all of the recent network sessions to generate and send NetworkManager analytics
237242
for (int i = 0; i < NetworkManager.RecentSessions.Count; i++)
238243
{
239244
var networkManagerAnalytics = GetNetworkManagerAnalytics(NetworkManager.RecentSessions[i]);
240245
if (k_EnableAnalyticsLogging)
241246
{
242247
networkManagerAnalytics.LogAnalytics(NetworkManager.RecentSessions[i].SessionIndex);
243248
}
249+
250+
var result = EditorAnalytics.SendAnalytic(new NetworkManagerAnalyticsHandler(networkManagerAnalytics));
251+
252+
if (result != AnalyticsResult.Ok)
253+
{
254+
Debug.LogWarning($"[Analytics] Problem sending analytics: {result}");
255+
}
244256
}
245257
}
246258

247-
259+
/// <summary>
260+
/// Generates a <see cref="NetworkManagerAnalytics"/> based on the <see cref="NetworkManager.NetworkSessionInfo"/> passed in
261+
/// </summary>
262+
/// <param name="networkSession">Represents a network session with the used NetworkManager configuration</param>
263+
/// <returns></returns>
248264
private NetworkManagerAnalytics GetNetworkManagerAnalytics(NetworkManager.NetworkSessionInfo networkSession)
249265
{
250266
var multiplayerSDKInstalled = false;

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,12 @@ private void ModeChanged(PlayModeStateChange change)
10611061
}
10621062
}
10631063

1064+
/// <summary>
1065+
/// If analytics is enabled, this will create a new <see cref="NetworkSessionInfo"/> struct.
1066+
/// </summary>
1067+
/// <remarks>
1068+
/// Invoked when NetworkManager is started.
1069+
/// </remarks>
10641070
private void BeginNetworkSession()
10651071
{
10661072
if (!EditorAnalytics.enabled)
@@ -1080,6 +1086,12 @@ private void BeginNetworkSession()
10801086
RecentSessions.Add(newSession);
10811087
}
10821088

1089+
/// <summary>
1090+
/// If analytics is enabled, this will finalize the current <see cref="NetworkSessionInfo"/> struct.
1091+
/// </summary>
1092+
/// <remarks>
1093+
/// Invoked when NetworkManager is stopped or upon exiting play mode.
1094+
/// </remarks>
10831095
private void EndNetworkSession()
10841096
{
10851097
// If analytics is disabled, then exit early
@@ -1091,6 +1103,7 @@ private void EndNetworkSession()
10911103
{
10921104
var lastIndex = RecentSessions.Count - 1;
10931105
var recentSession = RecentSessions[lastIndex];
1106+
// If the session has already been finalized, then exit early.
10941107
if (recentSession.SessionStopped)
10951108
{
10961109
return;

0 commit comments

Comments
 (0)