Skip to content

Commit

Permalink
Merge pull request #165 from immutable/feat/track
Browse files Browse the repository at this point in the history
[DX-2650] feat: add track function, track init passport
  • Loading branch information
nattb8 authored Feb 8, 2024
2 parents 6c0c312 + 274b256 commit f7eede5
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 22 deletions.
2 changes: 1 addition & 1 deletion sample/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ PlayerSettings:
allowUnsafeCode: 0
useDeterministicCompilation: 1
enableRoslynAnalyzers: 1
selectedPlatform: 2
selectedPlatform: 0
additionalIl2CppArgs:
scriptingRuntimeVersion: 1
gcIncremental: 1
Expand Down
6 changes: 3 additions & 3 deletions src/Packages/Passport/Runtime/Resources/index.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Immutable.Passport.Event
{
public static class PassportAnalytics
{
public const string TRACK = "track";
public const string MODULE_NAME = "unitySdk";

public static class EventName
{
public const string START_INIT_PASSPORT = "startedInitialisePassport";
public const string INIT_PASSPORT = "initialisedPassport";
}

public static class Properties
{
public const string SUCCESS = "succeeded";
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Immutable.Passport.Helpers
Expand Down Expand Up @@ -51,6 +54,40 @@ private static string ReplaceLast(this string source, string search, string repl
return source.Remove(place, search.Length).Insert(place, replace);
}

public static string ToJson(this IDictionary<string, object> dictionary)
{
// JsonUtility cannot serialise dictionary, but not using newtonsoft json as it doesn't
// work properly with older unity versions so doing it manually
StringBuilder sb = new StringBuilder("{");
for (int i = 0; i < dictionary.Count; i++)
{
object value = dictionary.ElementAt(i).Value;
if (value is string || value is int || value is long || value is double || value is bool)
{
string key = dictionary.ElementAt(i).Key;
sb = sb.Append("\"").Append(key).Append("\":");
if (value is string)
{
sb = sb.Append($"\"{value}\"");
}
else if (value is int || value is long || value is double)
{
sb = sb.Append(value);
}
else if (value is bool)
{
sb = sb.Append(value.ToString().ToLower());
}
}
if (i < dictionary.Count - 1)
{
sb = sb.Append(",");
}
}
sb = sb.Append("}");
return sb.ToString();
}

[Serializable]
private class Wrapper<T>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System;

namespace Immutable.Passport.Model
{
[Serializable]
internal class TrackData
{
public string moduleName;
public string eventName;
public string? properties;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions src/Packages/Passport/Runtime/Scripts/Private/PassportConstants.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public static class PassportFunction
public const string CONNECT_CONFIRM_CODE = "connectConfirmCode";
public const string GET_ACCESS_TOKEN = "getAccessToken";
public const string GET_ID_TOKEN = "getIdToken";
public const string GET_ADDRESS = "getAddress";
public const string LOGOUT = "logout";
public const string GET_EMAIL = "getEmail";

public static class IMX
{
public const string GET_ADDRESS = "getAddress";
public const string IS_REGISTERED_OFFCHAIN = "isRegisteredOffchain";
public const string REGISTER_OFFCHAIN = "registerOffchain";
public const string TRANSFER = "imxTransfer";
Expand Down
29 changes: 28 additions & 1 deletion src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public PassportImpl(IBrowserCommunicationsManager communicationsManager)

public async UniTask Init(string clientId, string environment, string redirectUri = null, string logoutRedirectUri = null, string deeplink = null)
{
Track(PassportAnalytics.EventName.START_INIT_PASSPORT);

this.redirectUri = redirectUri;
this.logoutRedirectUri = logoutRedirectUri;
this.communicationsManager.OnAuthPostMessage += OnDeepLinkActivated;
Expand Down Expand Up @@ -97,12 +99,19 @@ public async UniTask Init(string clientId, string environment, string redirectUr

if (initResponse.success == false)
{
Track(PassportAnalytics.EventName.INIT_PASSPORT, new Dictionary<string, object>(){
{PassportAnalytics.Properties.SUCCESS, false}
});
throw new PassportException(initResponse.error ?? "Unable to initialise Passport");
}
else if (deeplink != null)
{
OnDeepLinkActivated(deeplink);
}

Track(PassportAnalytics.EventName.INIT_PASSPORT, new Dictionary<string, object>(){
{PassportAnalytics.Properties.SUCCESS, true}
});
}

public async UniTask<bool> Login(bool useCachedSession = false, Nullable<long> timeoutMs = null)
Expand Down Expand Up @@ -473,7 +482,7 @@ public void OnDeeplinkResult(string url)

public async UniTask<string> GetAddress()
{
string response = await communicationsManager.Call(PassportFunction.GET_ADDRESS);
string response = await communicationsManager.Call(PassportFunction.IMX.GET_ADDRESS);
return response.GetStringResult();
}

Expand Down Expand Up @@ -759,6 +768,24 @@ public void ClearStorage()
communicationsManager.ClearStorage();
}
#endif

private async void Track(string eventName, IDictionary<string, object>? properties = null)
{
try
{
string json = JsonUtility.ToJson(new TrackData()
{
moduleName = PassportAnalytics.MODULE_NAME,
eventName = eventName,
properties = properties != null ? properties.ToJson() : null
});
await communicationsManager.Call(PassportAnalytics.TRACK, json);
}
catch (Exception ex)
{
// Ignore tracking errors
}
}
}

#if UNITY_ANDROID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System;
using NUnit.Framework;
using Immutable.Passport.Helpers;

namespace Immutable.Passport.Core
{
[TestFixture]
public class JsonHelpersTests
{
[Test]
public void DictionaryToJson()
{
var properties = new Dictionary<string, object>(){
{"boolean", true},
{"string", "immutable"},
{"int", 1},
{"long", (long) 2},
{"double", (double) 3}
};
Assert.AreEqual("{\"boolean\":true,\"string\":\"immutable\",\"int\":1,\"long\":2,\"double\":3}", properties.ToJson());

properties = new Dictionary<string, object>() { { "boolean", false } };
Assert.AreEqual("{\"boolean\":false}", properties.ToJson());

properties = new Dictionary<string, object>();
Assert.AreEqual("{}", properties.ToJson());
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ public async Task GetAddress_Success()
var address = await passport.GetAddress();

Assert.AreEqual(ADDRESS, address);
Assert.AreEqual(PassportFunction.GET_ADDRESS, communicationsManager.fxName);
Assert.AreEqual(PassportFunction.IMX.GET_ADDRESS, communicationsManager.fxName);
Assert.True(String.IsNullOrEmpty(communicationsManager.data));
}

Expand All @@ -841,7 +841,7 @@ public async Task GetAddress_Failed()
var address = await passport.GetAddress();

Assert.Null(address);
Assert.AreEqual(PassportFunction.GET_ADDRESS, communicationsManager.fxName);
Assert.AreEqual(PassportFunction.IMX.GET_ADDRESS, communicationsManager.fxName);
Assert.True(String.IsNullOrEmpty(communicationsManager.data));
}

Expand Down

0 comments on commit f7eede5

Please sign in to comment.