Skip to content

Commit f7eede5

Browse files
authored
Merge pull request #165 from immutable/feat/track
[DX-2650] feat: add track function, track init passport
2 parents 6c0c312 + 274b256 commit f7eede5

File tree

13 files changed

+158
-22
lines changed

13 files changed

+158
-22
lines changed

sample/ProjectSettings/ProjectSettings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ PlayerSettings:
792792
allowUnsafeCode: 0
793793
useDeterministicCompilation: 1
794794
enableRoslynAnalyzers: 1
795-
selectedPlatform: 2
795+
selectedPlatform: 0
796796
additionalIl2CppArgs:
797797
scriptingRuntimeVersion: 1
798798
gcIncremental: 1

src/Packages/Passport/Runtime/Resources/index.html

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Immutable.Passport.Event
2+
{
3+
public static class PassportAnalytics
4+
{
5+
public const string TRACK = "track";
6+
public const string MODULE_NAME = "unitySdk";
7+
8+
public static class EventName
9+
{
10+
public const string START_INIT_PASSPORT = "startedInitialisePassport";
11+
public const string INIT_PASSPORT = "initialisedPassport";
12+
}
13+
14+
public static class Properties
15+
{
16+
public const string SUCCESS = "succeeded";
17+
}
18+
}
19+
}

src/Packages/Passport/Runtime/Scripts/Private/PassportConstants.cs.meta renamed to src/Packages/Passport/Runtime/Scripts/Private/Event/AnalyticsEvent.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Packages/Passport/Runtime/Scripts/Private/Helpers/JsonHelpers.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using System.Collections.Generic;
12
using System;
3+
using System.Linq;
4+
using System.Text;
25
using UnityEngine;
36

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

57+
public static string ToJson(this IDictionary<string, object> dictionary)
58+
{
59+
// JsonUtility cannot serialise dictionary, but not using newtonsoft json as it doesn't
60+
// work properly with older unity versions so doing it manually
61+
StringBuilder sb = new StringBuilder("{");
62+
for (int i = 0; i < dictionary.Count; i++)
63+
{
64+
object value = dictionary.ElementAt(i).Value;
65+
if (value is string || value is int || value is long || value is double || value is bool)
66+
{
67+
string key = dictionary.ElementAt(i).Key;
68+
sb = sb.Append("\"").Append(key).Append("\":");
69+
if (value is string)
70+
{
71+
sb = sb.Append($"\"{value}\"");
72+
}
73+
else if (value is int || value is long || value is double)
74+
{
75+
sb = sb.Append(value);
76+
}
77+
else if (value is bool)
78+
{
79+
sb = sb.Append(value.ToString().ToLower());
80+
}
81+
}
82+
if (i < dictionary.Count - 1)
83+
{
84+
sb = sb.Append(",");
85+
}
86+
}
87+
sb = sb.Append("}");
88+
return sb.ToString();
89+
}
90+
5491
[Serializable]
5592
private class Wrapper<T>
5693
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
using System;
3+
4+
namespace Immutable.Passport.Model
5+
{
6+
[Serializable]
7+
internal class TrackData
8+
{
9+
public string moduleName;
10+
public string eventName;
11+
public string? properties;
12+
}
13+
}
14+

src/Packages/Passport/Runtime/Scripts/Private/Model/Request/TrackData.cs.meta

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

src/Packages/Passport/Runtime/Scripts/Private/PassportConstants.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Packages/Passport/Runtime/Scripts/Private/PassportFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public static class PassportFunction
1414
public const string CONNECT_CONFIRM_CODE = "connectConfirmCode";
1515
public const string GET_ACCESS_TOKEN = "getAccessToken";
1616
public const string GET_ID_TOKEN = "getIdToken";
17-
public const string GET_ADDRESS = "getAddress";
1817
public const string LOGOUT = "logout";
1918
public const string GET_EMAIL = "getEmail";
2019

2120
public static class IMX
2221
{
22+
public const string GET_ADDRESS = "getAddress";
2323
public const string IS_REGISTERED_OFFCHAIN = "isRegisteredOffchain";
2424
public const string REGISTER_OFFCHAIN = "registerOffchain";
2525
public const string TRANSFER = "imxTransfer";

src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public PassportImpl(IBrowserCommunicationsManager communicationsManager)
5555

5656
public async UniTask Init(string clientId, string environment, string redirectUri = null, string logoutRedirectUri = null, string deeplink = null)
5757
{
58+
Track(PassportAnalytics.EventName.START_INIT_PASSPORT);
59+
5860
this.redirectUri = redirectUri;
5961
this.logoutRedirectUri = logoutRedirectUri;
6062
this.communicationsManager.OnAuthPostMessage += OnDeepLinkActivated;
@@ -97,12 +99,19 @@ public async UniTask Init(string clientId, string environment, string redirectUr
9799

98100
if (initResponse.success == false)
99101
{
102+
Track(PassportAnalytics.EventName.INIT_PASSPORT, new Dictionary<string, object>(){
103+
{PassportAnalytics.Properties.SUCCESS, false}
104+
});
100105
throw new PassportException(initResponse.error ?? "Unable to initialise Passport");
101106
}
102107
else if (deeplink != null)
103108
{
104109
OnDeepLinkActivated(deeplink);
105110
}
111+
112+
Track(PassportAnalytics.EventName.INIT_PASSPORT, new Dictionary<string, object>(){
113+
{PassportAnalytics.Properties.SUCCESS, true}
114+
});
106115
}
107116

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

474483
public async UniTask<string> GetAddress()
475484
{
476-
string response = await communicationsManager.Call(PassportFunction.GET_ADDRESS);
485+
string response = await communicationsManager.Call(PassportFunction.IMX.GET_ADDRESS);
477486
return response.GetStringResult();
478487
}
479488

@@ -759,6 +768,24 @@ public void ClearStorage()
759768
communicationsManager.ClearStorage();
760769
}
761770
#endif
771+
772+
private async void Track(string eventName, IDictionary<string, object>? properties = null)
773+
{
774+
try
775+
{
776+
string json = JsonUtility.ToJson(new TrackData()
777+
{
778+
moduleName = PassportAnalytics.MODULE_NAME,
779+
eventName = eventName,
780+
properties = properties != null ? properties.ToJson() : null
781+
});
782+
await communicationsManager.Call(PassportAnalytics.TRACK, json);
783+
}
784+
catch (Exception ex)
785+
{
786+
// Ignore tracking errors
787+
}
788+
}
762789
}
763790

764791
#if UNITY_ANDROID
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
using System;
3+
using NUnit.Framework;
4+
using Immutable.Passport.Helpers;
5+
6+
namespace Immutable.Passport.Core
7+
{
8+
[TestFixture]
9+
public class JsonHelpersTests
10+
{
11+
[Test]
12+
public void DictionaryToJson()
13+
{
14+
var properties = new Dictionary<string, object>(){
15+
{"boolean", true},
16+
{"string", "immutable"},
17+
{"int", 1},
18+
{"long", (long) 2},
19+
{"double", (double) 3}
20+
};
21+
Assert.AreEqual("{\"boolean\":true,\"string\":\"immutable\",\"int\":1,\"long\":2,\"double\":3}", properties.ToJson());
22+
23+
properties = new Dictionary<string, object>() { { "boolean", false } };
24+
Assert.AreEqual("{\"boolean\":false}", properties.ToJson());
25+
26+
properties = new Dictionary<string, object>();
27+
Assert.AreEqual("{}", properties.ToJson());
28+
}
29+
}
30+
}

src/Packages/Passport/Tests/Runtime/Scripts/Helpers/JsonHelpersTests.cs.meta

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

src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ public async Task GetAddress_Success()
831831
var address = await passport.GetAddress();
832832

833833
Assert.AreEqual(ADDRESS, address);
834-
Assert.AreEqual(PassportFunction.GET_ADDRESS, communicationsManager.fxName);
834+
Assert.AreEqual(PassportFunction.IMX.GET_ADDRESS, communicationsManager.fxName);
835835
Assert.True(String.IsNullOrEmpty(communicationsManager.data));
836836
}
837837

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

843843
Assert.Null(address);
844-
Assert.AreEqual(PassportFunction.GET_ADDRESS, communicationsManager.fxName);
844+
Assert.AreEqual(PassportFunction.IMX.GET_ADDRESS, communicationsManager.fxName);
845845
Assert.True(String.IsNullOrEmpty(communicationsManager.data));
846846
}
847847

0 commit comments

Comments
 (0)