diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts index 06e3b4d16f9e..16865d705120 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts @@ -114,52 +114,61 @@ export const handlers = ({ uiHooks: ExchangeUiHooks; }) => ({ - "custom.exchange.start": customWrapper< - ExchangeStartParams | ExchangeStartSwapParams | ExchangeStartSellParams, - ExchangeStartResult - >(async params => { - tracking.startExchangeRequested(manifest); - - if (!params) { - tracking.startExchangeNoParams(manifest); - return { transactionId: "" }; - } - - let exchangeParams: ExchangeStartParamsUiRequest; - - // Use `if else` instead of switch to leverage TS type narrowing and avoid `params` force cast. - if (params.exchangeType == "SWAP") { - exchangeParams = extractSwapStartParam(params, accounts); - } else if (params.exchangeType == "SELL") { - exchangeParams = extractSellStartParam(params, accounts); - } else { - exchangeParams = { + "custom.exchange.start": customWrapper( + async params => { + if (!params) { + tracking.startExchangeNoParams(manifest); + return { transactionId: "" }; + } + + const trackingParams = { + // @ts-expect-error ExchangeStartFundParams does not yet have the provider. Will be added in another iteration after a bugfix is confirmed + // TODO: expect-error to be deleted after + provider: params.provider, exchangeType: params.exchangeType, }; - } - return new Promise((resolve, reject) => - uiExchangeStart({ - exchangeParams, - onSuccess: (nonce: string, device) => { - tracking.startExchangeSuccess(manifest); - resolve({ transactionId: nonce, device }); - }, - onCancel: error => { - tracking.completeExchangeFail(manifest); - reject(error); - }, - }), - ); - }), + tracking.startExchangeRequested(trackingParams); + + let exchangeParams: ExchangeStartParamsUiRequest; + + // Use `if else` instead of switch to leverage TS type narrowing and avoid `params` force cast. + if (params.exchangeType == "SWAP") { + exchangeParams = extractSwapStartParam(params, accounts); + } else if (params.exchangeType == "SELL") { + exchangeParams = extractSellStartParam(params, accounts); + } else { + exchangeParams = { + exchangeType: params.exchangeType, + }; + } + + return new Promise((resolve, reject) => + uiExchangeStart({ + exchangeParams, + onSuccess: (nonce: string, device) => { + tracking.startExchangeSuccess(trackingParams); + resolve({ transactionId: nonce, device }); + }, + onCancel: error => { + tracking.startExchangeFail(trackingParams); + reject(error); + }, + }), + ); + }, + ), "custom.exchange.complete": customWrapper( async params => { - tracking.completeExchangeRequested(manifest); - if (!params) { tracking.completeExchangeNoParams(manifest); return { transactionHash: "" }; } + const trackingParams = { + provider: params.provider, + exchangeType: params.exchangeType, + }; + tracking.completeExchangeRequested(trackingParams); const realFromAccountId = getAccountIdFromWalletAccountId(params.fromAccountId); if (!realFromAccountId) { @@ -286,11 +295,14 @@ export const handlers = ({ magnitudeAwareRate, }, onSuccess: (transactionHash: string) => { - tracking.completeExchangeSuccess(manifest); + tracking.completeExchangeSuccess({ + ...trackingParams, + currency: params.rawTransaction.family, + }); resolve({ transactionHash }); }, onCancel: error => { - tracking.completeExchangeFail(manifest); + tracking.completeExchangeFail(trackingParams); reject(error); }, }), diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/tracking.ts b/libs/ledger-live-common/src/wallet-api/Exchange/tracking.ts index 01fbcda19e17..f3b7897bf931 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/tracking.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/tracking.ts @@ -1,3 +1,4 @@ +import { LiveAppManifest } from "../../platform/types"; import type { AppManifest } from "../types"; /** @@ -7,16 +8,15 @@ import type { AppManifest } from "../types"; */ type TrackExchange = ( event: string, - properties: Record | null, + properties: Record | null, mandatory: boolean | null, ) => void; -/** - * Obtain Event data from WalletAPI App manifest - * - * @param {AppManifest} manifest - * @returns Object - event data - */ +interface TrackEventPayload { + exchangeType: "SELL" | "FUND" | "SWAP"; + provider: string; +} + function getEventData(manifest: AppManifest) { return { walletAPI: manifest.name }; } @@ -30,46 +30,51 @@ function getEventData(manifest: AppManifest) { // in order to get the exact type matching the tracking wrapper API // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export default function trackingWrapper(trackCall: TrackExchange) { - const track = (event: string, properties: Record | null) => - trackCall(event, properties, null); + const track = (event: string, properties: Record | null) => { + return trackCall(event, properties, null); + }; return { // Generate Exchange nonce modal open - startExchangeRequested: (manifest: AppManifest) => { - track("WalletAPI start Exchange Nonce request", getEventData(manifest)); + startExchangeRequested: ({ provider, exchangeType }: TrackEventPayload) => { + track("WalletAPI start Exchange Nonce request", { provider, exchangeType }); }, // Successfully generated an Exchange app nonce - startExchangeSuccess: (manifest: AppManifest) => { - track("WalletAPI start Exchange Nonce success", getEventData(manifest)); + startExchangeSuccess: ({ provider, exchangeType }: TrackEventPayload) => { + track("WalletAPI start Exchange Nonce success", { provider, exchangeType }); }, // Failed to generate an Exchange app nonce - startExchangeFail: (manifest: AppManifest) => { - track("WalletAPI start Exchange Nonce fail", getEventData(manifest)); + startExchangeFail: ({ provider, exchangeType }: TrackEventPayload) => { + track("WalletAPI start Exchange Nonce fail", { provider, exchangeType }); }, // No Params to generate an Exchange app nonce - startExchangeNoParams: (manifest: AppManifest) => { + startExchangeNoParams: (manifest: LiveAppManifest) => { track("WalletAPI start Exchange no params", getEventData(manifest)); }, - completeExchangeRequested: (manifest: AppManifest) => { - track("WalletAPI complete Exchange requested", getEventData(manifest)); + completeExchangeRequested: ({ provider, exchangeType }: TrackEventPayload) => { + track("WalletAPI complete Exchange requested", { provider, exchangeType }); }, // Successfully completed an Exchange - completeExchangeSuccess: (manifest: AppManifest) => { - track("WalletAPI complete Exchange success", getEventData(manifest)); + completeExchangeSuccess: ({ + provider, + exchangeType, + currency, + }: TrackEventPayload & { currency: string }) => { + track("WalletAPI complete Exchange success", { provider, exchangeType, currency }); }, // Failed to complete an Exchange - completeExchangeFail: (manifest: AppManifest) => { - track("WalletAPI complete Exchange Nonce fail", getEventData(manifest)); + completeExchangeFail: ({ provider, exchangeType }: TrackEventPayload) => { + track("WalletAPI complete Exchange Nonce fail", { provider, exchangeType }); }, // No Params to complete an Exchange - completeExchangeNoParams: (manifest: AppManifest) => { + completeExchangeNoParams: (manifest: LiveAppManifest) => { track("WalletAPI complete Exchange no params", getEventData(manifest)); }, } as const;