From 4755e86949f1930f38e4c05eae20e7e477534fc6 Mon Sep 17 00:00:00 2001 From: Joaquim Verges Date: Fri, 17 Jan 2025 11:28:36 +1300 Subject: [PATCH] feat: Add pay event tracking for transaction modal states --- packages/thirdweb/src/analytics/track/pay.ts | 2 ++ .../hooks/transaction/useSendTransaction.ts | 35 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/thirdweb/src/analytics/track/pay.ts b/packages/thirdweb/src/analytics/track/pay.ts index 5c5121f38c8..6558d4c39c0 100644 --- a/packages/thirdweb/src/analytics/track/pay.ts +++ b/packages/thirdweb/src/analytics/track/pay.ts @@ -17,6 +17,7 @@ export async function trackPayEvent(args: { toAmount?: string; chainId?: number; dstChainId?: number; + error?: string; }) { return track({ client: args.client, @@ -32,6 +33,7 @@ export async function trackPayEvent(args: { amountWei: args.fromAmount, dstTokenAddress: args.toToken, dstChainId: args.chainId, + errorCode: args.error, }, }); } diff --git a/packages/thirdweb/src/react/core/hooks/transaction/useSendTransaction.ts b/packages/thirdweb/src/react/core/hooks/transaction/useSendTransaction.ts index 9c92e413203..45ccd92e258 100644 --- a/packages/thirdweb/src/react/core/hooks/transaction/useSendTransaction.ts +++ b/packages/thirdweb/src/react/core/hooks/transaction/useSendTransaction.ts @@ -1,4 +1,5 @@ import { type UseMutationResult, useMutation } from "@tanstack/react-query"; +import { trackPayEvent } from "../../../../analytics/track/pay.js"; import type { Chain } from "../../../../chains/types.js"; import type { BuyWithCryptoStatus } from "../../../../pay/buyWithCrypto/getStatus.js"; import type { BuyWithFiatStatus } from "../../../../pay/buyWithFiat/getStatus.js"; @@ -146,6 +147,13 @@ export function useSendTransactionCore(args: { } if (!showPayModal) { + trackPayEvent({ + client: tx.client, + walletAddress: account.address, + walletType: wallet?.id, + dstChainId: tx.chain.id, + event: "pay_transaction_modal_disabled", + }); return sendTransaction({ transaction: tx, account, @@ -174,7 +182,17 @@ export function useSendTransactionCore(args: { await Promise.all([ resolvePromisedValue(tx.value), resolvePromisedValue(tx.erc20Value), - fetchBuySupportedDestinations(tx.client).catch(() => null), + fetchBuySupportedDestinations(tx.client).catch((err) => { + trackPayEvent({ + client: tx.client, + walletAddress: account.address, + walletType: wallet?.id, + dstChainId: tx.chain.id, + event: "pay_transaction_modal_pay_api_error", + error: err?.message, + }); + return null; + }), ]); if (!supportedDestinations) { @@ -198,6 +216,14 @@ export function useSendTransactionCore(args: { ), )) ) { + trackPayEvent({ + client: tx.client, + walletAddress: account.address, + walletType: wallet?.id, + dstChainId: tx.chain.id, + event: "pay_transaction_modal_chain_token_not_supported", + error: `chain ${tx.chain.id} ${_erc20Value ? `/ token ${_erc20Value?.tokenAddress}` : ""} not supported`, + }); // chain/token not supported, just send the tx sendTx(); return; @@ -241,6 +267,13 @@ export function useSendTransactionCore(args: { resolveTx: resolve, }); } else { + trackPayEvent({ + client: tx.client, + walletAddress: account.address, + walletType: wallet?.id, + dstChainId: tx.chain.id, + event: "pay_transaction_modal_has_enough_funds", + }); sendTx(); } } catch (e) {