Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 494dc15

Browse files
Merge pull request #713 from github-for-unity/enhancements/session-based-metrics
Per session usage metrics of GitHub for Unity
2 parents 8049cb2 + fe35fa7 commit 494dc15

File tree

5 files changed

+54
-29
lines changed

5 files changed

+54
-29
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,22 @@ public void RestartRepository()
187187
}
188188
}
189189

190-
protected void SetupMetrics(string unityVersion, bool firstRun)
190+
protected void SetupMetrics(string unityVersion, bool firstRun, Guid instanceId)
191191
{
192192
//Logger.Trace("Setup metrics");
193193

194194
var usagePath = Environment.UserCachePath.Combine(Constants.UsageFile);
195195

196-
string id = null;
196+
string userId = null;
197197
if (UserSettings.Exists(Constants.GuidKey))
198198
{
199-
id = UserSettings.Get(Constants.GuidKey);
199+
userId = UserSettings.Get(Constants.GuidKey);
200200
}
201201

202-
if (String.IsNullOrEmpty(id))
202+
if (String.IsNullOrEmpty(userId))
203203
{
204-
id = Guid.NewGuid().ToString();
205-
UserSettings.Set(Constants.GuidKey, id);
204+
userId = Guid.NewGuid().ToString();
205+
UserSettings.Set(Constants.GuidKey, userId);
206206
}
207207

208208
#if ENABLE_METRICS
@@ -212,7 +212,7 @@ protected void SetupMetrics(string unityVersion, bool firstRun)
212212
Environment.NodeJsExecutablePath,
213213
Environment.OctorunScriptPath);
214214

215-
UsageTracker = new UsageTracker(metricsService, UserSettings, usagePath, id, unityVersion);
215+
UsageTracker = new UsageTracker(metricsService, UserSettings, usagePath, userId, unityVersion, instanceId.ToString());
216216

217217
if (firstRun)
218218
{

src/GitHub.Api/Metrics/UsageModel.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45

56
namespace GitHub.Unity
67
{
78
public class Usage
89
{
10+
public string InstanceId { get; set; }
911
public Dimensions Dimensions { get; set; } = new Dimensions();
1012
public Measures Measures { get; set; } = new Measures();
1113
}
@@ -42,7 +44,7 @@ class UsageModel
4244

4345
private Usage currentUsage;
4446

45-
public Usage GetCurrentUsage(string appVersion, string unityVersion)
47+
public Usage GetCurrentUsage(string appVersion, string unityVersion, string instanceId)
4648
{
4749
Guard.ArgumentNotNullOrWhiteSpace(appVersion, "appVersion");
4850
Guard.ArgumentNotNullOrWhiteSpace(unityVersion, "unityVersion");
@@ -51,26 +53,21 @@ public Usage GetCurrentUsage(string appVersion, string unityVersion)
5153
if (currentUsage == null)
5254
{
5355
currentUsage = Reports
54-
.FirstOrDefault(usage => usage.Dimensions.Date == date
55-
&& usage.Dimensions.AppVersion == appVersion
56-
&& usage.Dimensions.UnityVersion == unityVersion);
56+
.FirstOrDefault(usage => usage.InstanceId == instanceId);
5757
}
5858

59-
if (currentUsage?.Dimensions.Date == date)
60-
{
61-
// update any fields that might be missing, if we've changed the format
62-
if (currentUsage.Dimensions.Guid != Guid)
63-
currentUsage.Dimensions.Guid = Guid;
64-
}
65-
else
59+
if (currentUsage == null)
6660
{
6761
currentUsage = new Usage
6862
{
63+
InstanceId = instanceId,
6964
Dimensions = {
7065
Date = date,
7166
Guid = Guid,
7267
AppVersion = appVersion,
73-
UnityVersion = unityVersion
68+
UnityVersion = unityVersion,
69+
Lang = CultureInfo.InstalledUICulture.IetfLanguageTag,
70+
CurrentLang = CultureInfo.CurrentCulture.IetfLanguageTag
7471
}
7572
};
7673
Reports.Add(currentUsage);

src/GitHub.Api/Metrics/UsageTracker.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ class UsageTracker : IUsageTracker
1717
private readonly NPath storePath;
1818
private readonly ISettings userSettings;
1919
private readonly IMetricsService metricsService;
20-
private readonly string guid;
20+
private readonly string userId;
2121
private readonly string unityVersion;
22+
private readonly string instanceId;
2223
private Timer timer;
2324

24-
public UsageTracker(IMetricsService metricsService, ISettings userSettings, NPath storePath, string guid, string unityVersion)
25+
public UsageTracker(IMetricsService metricsService, ISettings userSettings, NPath storePath, string userId, string unityVersion, string instanceId)
2526
{
2627
this.userSettings = userSettings;
2728
this.metricsService = metricsService;
28-
this.guid = guid;
29+
this.userId = userId;
2930
this.storePath = storePath;
3031
this.unityVersion = unityVersion;
32+
this.instanceId = instanceId;
3133

32-
Logger.Trace("guid:{0}", guid);
34+
Logger.Trace("userId:{0} instanceId:{1}", userId, instanceId);
3335
if (Enabled)
3436
RunTimer(3*60);
3537
}
@@ -66,7 +68,7 @@ private UsageStore LoadUsage()
6668
result = new UsageStore();
6769

6870
if (String.IsNullOrEmpty(result.Model.Guid))
69-
result.Model.Guid = guid;
71+
result.Model.Guid = userId;
7072

7173
return result;
7274
}
@@ -154,10 +156,7 @@ private async Task SendUsage()
154156

155157
private Usage GetCurrentUsage(UsageStore usageStore)
156158
{
157-
var usage = usageStore.Model.GetCurrentUsage(ApplicationConfiguration.AssemblyName.Version.ToString(), unityVersion);
158-
usage.Dimensions.Lang = CultureInfo.InstalledUICulture.IetfLanguageTag;
159-
usage.Dimensions.CurrentLang = CultureInfo.CurrentCulture.IetfLanguageTag;
160-
return usage;
159+
return usageStore.Model.GetCurrentUsage(ApplicationConfiguration.AssemblyName.Version.ToString(), unityVersion, instanceId);
161160
}
162161

163162
public void IncrementNumberOfStartups()

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ sealed class ApplicationCache : ScriptObjectSingleton<ApplicationCache>
1414
{
1515
[SerializeField] private bool firstRun = true;
1616
[SerializeField] public string firstRunAtString;
17+
[SerializeField] public string instanceIdString;
18+
[NonSerialized] private Guid? instanceId;
1719
[NonSerialized] private bool? firstRunValue;
1820
[NonSerialized] public DateTimeOffset? firstRunAtValue;
1921

@@ -60,6 +62,33 @@ private void EnsureFirstRun()
6062
Save(true);
6163
}
6264
}
65+
66+
public Guid InstanceId
67+
{
68+
get
69+
{
70+
EnsureInstanceId();
71+
return instanceId.Value;
72+
}
73+
}
74+
75+
private void EnsureInstanceId()
76+
{
77+
if (instanceId.HasValue)
78+
{
79+
return;
80+
}
81+
82+
if (string.IsNullOrEmpty(instanceIdString))
83+
{
84+
instanceId = Guid.NewGuid();
85+
instanceIdString = instanceId.ToString();
86+
}
87+
else
88+
{
89+
instanceId = new Guid(instanceIdString);
90+
}
91+
}
6392
}
6493

6594
sealed class EnvironmentCache : ScriptObjectSingleton<EnvironmentCache>

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ApplicationManager(IMainThreadSynchronizationContext synchronizationConte
2323

2424
protected override void SetupMetrics()
2525
{
26-
SetupMetrics(Environment.UnityVersion, ApplicationCache.Instance.FirstRun);
26+
SetupMetrics(Environment.UnityVersion, ApplicationCache.Instance.FirstRun, ApplicationCache.Instance.InstanceId);
2727
}
2828

2929
protected override void InitializeUI()

0 commit comments

Comments
 (0)