Skip to content

Commit 7306743

Browse files
committed
Usage Metrics
1 parent 1a77f77 commit 7306743

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

Assets/Thirdweb/Runtime/Unity/ThirdwebManager.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public class ThirdwebManager : MonoBehaviour
9292
[field: SerializeField]
9393
private bool ShowDebugLogs { get; set; } = true;
9494

95+
[field: SerializeField]
96+
private bool OptOutUsageAnalytics { get; set; } = false;
97+
9598
[field: SerializeField, Header("Wallet Settings")]
9699
private ulong[] SupportedChains { get; set; } = new ulong[] { 421614 };
97100

@@ -270,7 +273,14 @@ public async Task<IThirdwebWallet> ConnectWallet(WalletOptions walletOptions)
270273
var address = await wallet.GetAddress();
271274
ThirdwebDebug.Log($"Wallet address: {address}");
272275

273-
if (walletOptions.SmartWalletOptions != null)
276+
var isSmartWallet = walletOptions.SmartWalletOptions != null;
277+
278+
if (!OptOutUsageAnalytics)
279+
{
280+
TrackUsage("connectWallet", "connect", isSmartWallet ? "smartWallet" : walletOptions.Provider.ToString()[..1].ToLower() + walletOptions.Provider.ToString()[1..], address);
281+
}
282+
283+
if (isSmartWallet)
274284
{
275285
ThirdwebDebug.Log("Upgrading to SmartWallet.");
276286
return await UpgradeToSmartWallet(wallet, walletOptions.ChainId, walletOptions.SmartWalletOptions);
@@ -322,5 +332,36 @@ public async Task<SmartWallet> UpgradeToSmartWallet(IThirdwebWallet personalWall
322332

323333
return wallet;
324334
}
335+
336+
private async void TrackUsage(string source, string action, string walletType, string walletAddress)
337+
{
338+
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(action) || string.IsNullOrEmpty(walletType) || string.IsNullOrEmpty(walletAddress))
339+
{
340+
ThirdwebDebug.LogWarning("Invalid usage analytics parameters.");
341+
return;
342+
}
343+
344+
try
345+
{
346+
var content = new System.Net.Http.StringContent(
347+
Newtonsoft.Json.JsonConvert.SerializeObject(
348+
new
349+
{
350+
source,
351+
action,
352+
walletAddress,
353+
walletType,
354+
}
355+
),
356+
System.Text.Encoding.UTF8,
357+
"application/json"
358+
);
359+
_ = await Client.HttpClient.PostAsync("https://c.thirdweb.com/event", content);
360+
}
361+
catch
362+
{
363+
ThirdwebDebug.LogWarning($"Failed to report usage analytics.");
364+
}
365+
}
325366
}
326367
}

0 commit comments

Comments
 (0)