Skip to content

Commit

Permalink
task(device): persist device id in fallback (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-bugsnag authored Mar 2, 2022
1 parent 82ecd8a commit 5a15377
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Enhancements

* Added event and session persistence for Windows and WebGL builds [#512](https://github.com/bugsnag/bugsnag-unity/pull/512) [#509](https://github.com/bugsnag/bugsnag-unity/pull/509)
* Added event, session and device id persistence for Windows and WebGL builds [#512](https://github.com/bugsnag/bugsnag-unity/pull/512) [#509](https://github.com/bugsnag/bugsnag-unity/pull/509) [#514](https://github.com/bugsnag/bugsnag-unity/pull/514)


## 6.1.0 (2022-02-08)

Expand Down
12 changes: 12 additions & 0 deletions features/desktop/persistence.feature
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,17 @@ Feature: Unity Persistence
And the event "metaData.Persist Section.Persist Key" equals "Persist Value"
And I wait for 5 seconds

Scenario: Persist Device Id
When I run the game in the "PersistDeviceId" state
And I wait to receive an error
And the exception "message" equals "PersistDeviceId"
And the error payload field "events.0.device.id" is stored as the value "device_id"
And the error payload field "events.0.user.id" equals the stored value "device_id"
And I discard the oldest error
And I run the game in the "PersistDeviceId" state
And I wait to receive an error
And the exception "message" equals "PersistDeviceId"
And the error payload field "events.0.device.id" equals the stored value "device_id"
And the error payload field "events.0.user.id" equals the stored value "device_id"


2 changes: 2 additions & 0 deletions features/fixtures/maze_runner/Assets/Scripts/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ void RunScenario(string scenario)
{
switch (scenario)
{
case "PersistDeviceId":
throw new Exception("PersistDeviceId");
case "FeatureFlagsAfterInitClearAll":
Bugsnag.AddFeatureFlag("testName1", "testVariant1");
Bugsnag.AddFeatureFlag("testName2", "testVariant1");
Expand Down
2 changes: 1 addition & 1 deletion src/BugsnagUnity/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void InitUserObject()
else
{
// otherwise create one
_cachedUser = new User { Id = SystemInfo.deviceUniqueIdentifier };
_cachedUser = new User { Id = FileManager.GetDeviceId() };
// see if a native user is avaliable
NativeClient.PopulateUser(_cachedUser);
}
Expand Down
43 changes: 43 additions & 0 deletions src/BugsnagUnity/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,49 @@ private static string _eventsDirectory

private static string[] _cachedEvents => Directory.GetFiles(_eventsDirectory, "*.event");

private static string _deviceIdFile = _cacheDirectory + "/deviceId.txt";

[Serializable]
private class DeviceIdModel
{
public string DeviceId;
}

internal static string GetDeviceId()
{
try
{
var deviceId = string.Empty;
if (File.Exists(_deviceIdFile))
{

var deviceIdStore = JsonUtility.FromJson<DeviceIdModel>(File.ReadAllText(_deviceIdFile));
deviceId = deviceIdStore.DeviceId;
}
if (string.IsNullOrEmpty(deviceId) && _configuration.GenerateAnonymousId)
{
deviceId = Guid.NewGuid().ToString();
StoreDeviceId(deviceId);
}
return deviceId;
}
catch
{
// not possible in unit tests
return string.Empty;
}

}

private static void StoreDeviceId(string deviceId)
{
var model = new DeviceIdModel()
{
DeviceId = deviceId
};
var json = JsonUtility.ToJson(model);
File.WriteAllText(_deviceIdFile, json);
}

internal static void InitFileManager(Configuration configuration)
{
Expand Down
17 changes: 2 additions & 15 deletions src/BugsnagUnity/Payload/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public class Device : PayloadContainer, IDevice
private const string TOTAL_MEMORY_KEY = "totalMemory";
private const string USER_AGENT_KEY = "userAgent";

//Player prefs id for Bugsnag generated device id
private const string GENERATED_ID_KEY = "GENERATED_ID_KEY";


public string? BrowserName
{
Expand Down Expand Up @@ -126,21 +123,11 @@ internal Device(Configuration configuration)
Locale = CultureInfo.CurrentCulture.ToString();
AddOsInfo();
AddRuntimeVersions(configuration);
if (configuration.GenerateAnonymousId)
{
Id = GetGeneratedDeviceId();
}
Id = FileManager.GetDeviceId();
Model = SystemInfo.deviceModel;
}

private string GetGeneratedDeviceId()
{
if (!PlayerPrefs.HasKey(GENERATED_ID_KEY))
{
PlayerPrefs.SetString(GENERATED_ID_KEY, Guid.NewGuid().ToString());
}
return PlayerPrefs.GetString(GENERATED_ID_KEY);
}


private void AddOsInfo()
{
Expand Down
19 changes: 19 additions & 0 deletions tests/UnityEngine/JsonUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
namespace UnityEngine
{
public class JsonUtility
{
public JsonUtility()
{
}
public static T FromJson<T>(string json) {
var t = new object();
return (T)t;
}
public static string ToJson(object obj) {
return string.Empty;
}


}
}

0 comments on commit 5a15377

Please sign in to comment.