Skip to content

Commit ac24fe6

Browse files
committed
Analytics
1 parent 84c033e commit ac24fe6

20 files changed

Lines changed: 395 additions & 30 deletions

Packages/com.walletconnect.web3modal/Runtime/Connectors/Connector.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ protected virtual void OnAccountConnected(AccountConnectedEventArgs e)
122122
protected virtual void OnAccountDisconnected(AccountDisconnectedEventArgs e)
123123
{
124124
AccountDisconnected?.Invoke(this, e);
125+
126+
Web3Modal.EventsController.SendEvent(new Event
127+
{
128+
name = "DISCONNECT_SUCCESS"
129+
});
125130
}
126131

127132
protected virtual void OnAccountChanged(AccountChangedEventArgs e)

Packages/com.walletconnect.web3modal/Runtime/Connectors/WalletConnect/WalletConnectConnectionProposal.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ private void OnSessionConnectionErrored(object sender, Exception e)
3434
{
3535
Web3Modal.NotificationController.Notify(NotificationType.Error, e.Message);
3636
RefreshConnection();
37+
38+
Web3Modal.EventsController.SendEvent(new Event
39+
{
40+
name = "CONNECT_ERROR",
41+
properties = new System.Collections.Generic.Dictionary<string, object>
42+
{
43+
{ "message", e.Message }
44+
}
45+
});
3746
}
3847

3948
private IEnumerator RefreshOnIntervalRoutine()

Packages/com.walletconnect.web3modal/Runtime/Connectors/WalletConnect/WalletConnectConnector.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,18 @@ protected override ConnectionProposal ConnectCore()
123123

124124
protected override async Task DisconnectAsyncCore()
125125
{
126-
await WalletConnectInstance.DisconnectAsync();
126+
try
127+
{
128+
await WalletConnectInstance.DisconnectAsync();
129+
}
130+
catch (Exception)
131+
{
132+
Web3Modal.EventsController.SendEvent(new Event
133+
{
134+
name = "DISCONNECT_ERROR"
135+
});
136+
throw;
137+
}
127138
}
128139

129140
protected override async Task ChangeActiveChainAsyncCore(Chain chain)
@@ -154,15 +165,15 @@ protected override Task<Account[]> GetAccountsCore()
154165
var ciapAddresses = WalletConnectInstance.SignClient.AddressProvider.AllAddresses();
155166
return Task.FromResult(ciapAddresses.Select(ciapAddress => new Account(ciapAddress.Address, ciapAddress.ChainId)).ToArray());
156167
}
157-
168+
158169
private Account GetCurrentAccount()
159170
{
160171
var ciapAddress = WalletConnectInstance.SignClient.AddressProvider.CurrentAddress();
161172
return new Account(ciapAddress.Address, ciapAddress.ChainId);
162173
}
163174

164175
private static bool ActiveSessionSupportsMethod(string method)
165-
{
176+
{
166177
var @namespace = WalletConnectInstance.SignClient.AddressProvider.DefaultNamespace;
167178
var activeSession = WalletConnectInstance.ActiveSession;
168179
return activeSession.Namespaces[@namespace].Methods.Contains(method);

Packages/com.walletconnect.web3modal/Runtime/Controllers/ApiController.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using UnityEngine.Networking;
67
using WalletConnect.Web3Modal.Http;
@@ -12,18 +13,19 @@ public class ApiController
1213
{
1314
private const string BasePath = "https://api.web3modal.com/";
1415
private const int TimoutSeconds = 5;
15-
16+
1617
private readonly string _includedWalletIdsString = Web3Modal.Config.includedWalletIds is { Length: > 0 }
1718
? string.Join(",", Web3Modal.Config.includedWalletIds)
1819
: null;
20+
1921
private readonly string _excludedWalletIdsString = Web3Modal.Config.excludedWalletIds is { Length: > 0 }
2022
? string.Join(",", Web3Modal.Config.excludedWalletIds)
2123
: null;
2224

2325
private readonly UnityHttpClient _httpClient = new(new Uri(BasePath), TimeSpan.FromSeconds(TimoutSeconds),
2426
new Web3ModalApiHeaderDecorator()
2527
);
26-
28+
2729
private const string Platform =
2830
#if UNITY_ANDROID
2931
"android";
@@ -44,13 +46,24 @@ public async Task<GetWalletsResponse> GetWallets(int page, int count, string sea
4446

4547
return await _httpClient.GetAsync<GetWalletsResponse>("getWallets", new Dictionary<string, string>()
4648
{
47-
{"page", page.ToString()},
48-
{"entries", count.ToString()},
49-
{"search", search},
50-
{"platform", Platform},
51-
{"include", _includedWalletIdsString},
52-
{"exclude", _excludedWalletIdsString}
49+
{ "page", page.ToString() },
50+
{ "entries", count.ToString() },
51+
{ "search", search },
52+
{ "platform", Platform },
53+
{ "include", _includedWalletIdsString },
54+
{ "exclude", _excludedWalletIdsString }
5355
});
5456
}
57+
58+
public async Task<ApiGetAnalyticsConfigResponse> GetAnalyticsConfigAsync()
59+
{
60+
return await _httpClient.GetAsync<ApiGetAnalyticsConfigResponse>("getAnalyticsConfig");
61+
}
62+
}
63+
64+
public class ApiGetAnalyticsConfigResponse
65+
{
66+
public bool isAnalyticsEnabled { get; set; }
67+
public bool isAppKitAuthEnabled { get; set; }
5568
}
5669
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Newtonsoft.Json;
5+
using UnityEngine;
6+
using WalletConnect.Web3Modal.Http;
7+
using WalletConnectUnity.Core;
8+
9+
namespace WalletConnect.Web3Modal
10+
{
11+
public class EventsController
12+
{
13+
private const string BasePath = "https://pulse.walletconnect.org/";
14+
private const int TimoutSeconds = 5;
15+
16+
private readonly UnityHttpClient _httpClient = new(new Uri(BasePath), TimeSpan.FromSeconds(TimoutSeconds), new Web3ModalApiHeaderDecorator());
17+
18+
// private Queue<Event> _eventsQueue = new();
19+
private AnalyticsState _state = AnalyticsState.Loading;
20+
21+
public async Task InitializeAsync(Web3ModalConfig config, ApiController apiController)
22+
{
23+
#if UNITY_WEBGL && !UNITY_EDITOR
24+
return;
25+
#endif
26+
27+
if (!Web3Modal.Config.enableAnalytics)
28+
{
29+
_state = AnalyticsState.Disabled;
30+
return;
31+
}
32+
33+
await LoadRemoteAnalyticsConfig(apiController);
34+
}
35+
36+
37+
private async Task LoadRemoteAnalyticsConfig(ApiController apiController)
38+
{
39+
try
40+
{
41+
var response = await apiController.GetAnalyticsConfigAsync();
42+
43+
_state = response.isAnalyticsEnabled
44+
? AnalyticsState.Enabled
45+
: AnalyticsState.Disabled;
46+
}
47+
catch (Exception e)
48+
{
49+
Debug.LogException(e);
50+
_state = AnalyticsState.Disabled;
51+
}
52+
}
53+
54+
public async void SendEvent(Event @event)
55+
{
56+
try
57+
{
58+
if (_state == AnalyticsState.Disabled)
59+
return;
60+
61+
var request = new EventRequest
62+
{
63+
eventId = Guid.NewGuid().ToString(),
64+
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
65+
bundleId = Application.identifier,
66+
props = @event
67+
};
68+
69+
var requestJson = JsonConvert.SerializeObject(request);
70+
71+
Debug.Log($"[EventsController] Sending event: {@event.name}");
72+
73+
await _httpClient.PostAsync("e", requestJson);
74+
}
75+
catch (Exception e)
76+
{
77+
Debug.LogException(e);
78+
}
79+
}
80+
81+
private enum AnalyticsState
82+
{
83+
Loading,
84+
Enabled,
85+
Disabled
86+
}
87+
}
88+
89+
[Serializable]
90+
internal struct EventRequest
91+
{
92+
public string eventId;
93+
public long timestamp;
94+
public string bundleId;
95+
public Event props;
96+
}
97+
98+
[Serializable]
99+
public struct Event
100+
{
101+
[JsonProperty("type")]
102+
public const string type = "track";
103+
104+
[JsonProperty("event")]
105+
public string name;
106+
107+
[JsonProperty("properties")]
108+
public IDictionary<string, object> properties;
109+
}
110+
}

Packages/com.walletconnect.web3modal/Runtime/Controllers/EventsController.cs.meta

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

Packages/com.walletconnect.web3modal/Runtime/Controllers/ModalController/ModalControllerUtk.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Linq;
23
using System.Threading.Tasks;
34
using UnityEngine;
@@ -17,7 +18,7 @@ public class ModalControllerUtk : ModalController
1718

1819
public Modal Modal { get; private set; }
1920

20-
public VisualElement Web3Modal { get; private set; }
21+
public VisualElement Web3ModalElement { get; private set; }
2122

2223
public RouterController RouterController { get; private set; }
2324

@@ -28,12 +29,12 @@ public class ModalControllerUtk : ModalController
2829

2930
protected override Task InitializeAsyncCore()
3031
{
31-
var web3Modal = WalletConnect.Web3Modal.Web3Modal.Instance;
32+
var web3Modal = Web3Modal.Instance;
3233
UIDocument = web3Modal.GetComponentInChildren<UIDocument>(true);
3334

34-
Web3Modal = UIDocument.rootVisualElement.Children().First();
35+
Web3ModalElement = UIDocument.rootVisualElement.Children().First();
3536

36-
Modal = Web3Modal.Q<Modal>();
37+
Modal = Web3ModalElement.Q<Modal>();
3738

3839
RouterController = new RouterController(Modal.body);
3940
RouterController.ViewChanged += ViewChangedHandler;
@@ -55,18 +56,36 @@ private void ViewChangedHandler(object _, ViewChangedEventArgs args)
5556

5657
protected override void OpenCore(ViewType view)
5758
{
58-
Web3Modal.visible = true;
59+
Web3ModalElement.visible = true;
5960
RouterController.OpenView(view);
6061
WCLoadingAnimator.Instance.ResumeAnimation();
6162
OnOpenStateChanged(_openStateChangedEventArgsTrueOnOpen);
63+
64+
Web3Modal.EventsController.SendEvent(new Event
65+
{
66+
name = "MODAL_OPEN",
67+
properties = new Dictionary<string, object>
68+
{
69+
{ "connected", Web3Modal.IsAccountConnected }
70+
}
71+
});
6272
}
6373

6474
protected override void CloseCore()
6575
{
66-
Web3Modal.visible = false;
76+
Web3ModalElement.visible = false;
6777
WCLoadingAnimator.Instance.PauseAnimation();
6878
RouterController.CloseAllViews();
6979
OnOpenStateChanged(_openStateChangedEventArgsTrueOnClose);
80+
81+
Web3Modal.EventsController.SendEvent(new Event
82+
{
83+
name = "MODAL_CLOSE",
84+
properties = new Dictionary<string, object>
85+
{
86+
{ "connected", Web3Modal.IsAccountConnected }
87+
}
88+
});
7089
}
7190

7291
private void TickHandler()

Packages/com.walletconnect.web3modal/Runtime/Controllers/NetworkController/NetworkControllerCore.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ protected override async Task ChangeActiveChainAsyncCore(Chain chain)
2626
await Web3Modal.ConnectorController.ChangeActiveChainAsync(chain);
2727
else
2828
ActiveChain = chain;
29+
30+
Web3Modal.EventsController.SendEvent(new Event
31+
{
32+
name = "SWITCH_NETWORK",
33+
properties = new Dictionary<string, object>
34+
{
35+
{ "network", chain.ChainId }
36+
}
37+
});
2938
}
3039

3140
protected override void ConnectorChainChangedHandlerCore(object sender, Connector.ChainChangedEventArgs e)
@@ -43,7 +52,7 @@ protected override async void ConnectorAccountConnectedHandlerCore(object sender
4352
if (ActiveChain == null)
4453
{
4554
var defaultAccount = await e.GetAccount();
46-
55+
4756
if (Chains.TryGetValue(defaultAccount.ChainId, out var defaultAccountChain))
4857
{
4958
ActiveChain = defaultAccountChain;

Packages/com.walletconnect.web3modal/Runtime/Http/UnityHttpClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class UnityHttpClient : HttpClientDecorator
9595
public UnityHttpClient(params HttpClientDecorator[] decorators) : this(null, TimeSpan.FromSeconds(5), decorators)
9696
{
9797
}
98-
98+
9999
public UnityHttpClient(Uri basePath, TimeSpan timeout, params HttpClientDecorator[] decorators)
100100
{
101101
_basePath = basePath;
@@ -117,6 +117,14 @@ private Task<HttpResponseContext> InvokeRecursive(HttpRequestContext context, Ca
117117
.SendAsync(context, cancellationToken, _next);
118118
}
119119

120+
public async Task PostAsync(string path, string value, IDictionary<string, string> parameters = null, IDictionary<string, string> headers = null)
121+
{
122+
path = path.AppendQueryString(parameters);
123+
124+
var request = new HttpRequestContext(path, "POST", value, "application/json", headers, _decorators);
125+
await InvokeRecursive(request, CancellationToken.None);
126+
}
127+
120128
public async Task<T> PostAsync<T>(string path, string value, IDictionary<string, string> parameters = null, IDictionary<string, string> headers = null)
121129
{
122130
path = path.AppendQueryString(parameters);

Packages/com.walletconnect.web3modal/Runtime/Presenters/AccountPresenter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ protected virtual async void OnDisconnectButtonClick()
172172
protected virtual void OnNetworkButtonClick()
173173
{
174174
Router.OpenView(ViewType.NetworkSearch);
175+
176+
Web3Modal.EventsController.SendEvent(new Event
177+
{
178+
name = "CLICK_NETWORKS"
179+
});
175180
}
176181

177182
protected virtual void OnBlockExplorerButtonClick()

0 commit comments

Comments
 (0)