-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9122 from LedgerHQ/feat/live-15765-data-tracking-…
…in-the-swap-flow feat(lld): data tracking in the exchange flow
- Loading branch information
Showing
12 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ledger-live-desktop": minor | ||
--- | ||
|
||
Add data tracking in the exchange flow |
116 changes: 116 additions & 0 deletions
116
apps/ledger-live-desktop/src/renderer/analytics/hooks/useTrackExchangeFlow.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { renderHook } from "tests/testUtils"; | ||
import { useTrackExchangeFlow, UseTrackExchangeFlow } from "./useTrackExchangeFlow"; | ||
import { track } from "../segment"; | ||
import { UserRefusedAllowManager, UserRefusedOnDevice } from "@ledgerhq/errors"; | ||
import { CONNECTION_TYPES, HOOKS_TRACKING_LOCATIONS } from "./variables"; | ||
|
||
jest.mock("../segment", () => ({ | ||
track: jest.fn(), | ||
setAnalyticsFeatureFlagMethod: jest.fn(), | ||
})); | ||
|
||
describe("useTrackExchangeFlow", () => { | ||
const deviceMock = { | ||
modelId: "stax", | ||
wired: true, | ||
}; | ||
|
||
const defaultArgs: UseTrackExchangeFlow = { | ||
location: HOOKS_TRACKING_LOCATIONS.exchange, | ||
device: deviceMock, | ||
error: null, | ||
isTrackingEnabled: true, | ||
isRequestOpenAppExchange: null, | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should track 'Open app denied' when UserRefusedOnDevice error is thrown", () => { | ||
const error = new UserRefusedOnDevice(); | ||
|
||
renderHook((props: UseTrackExchangeFlow) => useTrackExchangeFlow(props), { | ||
initialProps: { ...defaultArgs, error }, | ||
}); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Open app denied", | ||
expect.objectContaining({ | ||
deviceType: "stax", | ||
connectionType: CONNECTION_TYPES.USB, | ||
platform: "LLD", | ||
page: HOOKS_TRACKING_LOCATIONS.exchange, | ||
}), | ||
true, | ||
); | ||
}); | ||
|
||
it("should track 'Secure Channel denied' when UserRefusedAllowManager error is thrown", () => { | ||
const error = new UserRefusedAllowManager(); | ||
|
||
renderHook((props: UseTrackExchangeFlow) => useTrackExchangeFlow(props), { | ||
initialProps: { ...defaultArgs, error }, | ||
}); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Secure Channel denied", | ||
expect.objectContaining({ | ||
deviceType: "stax", | ||
connectionType: CONNECTION_TYPES.USB, | ||
platform: "LLD", | ||
page: HOOKS_TRACKING_LOCATIONS.exchange, | ||
}), | ||
true, | ||
); | ||
}); | ||
|
||
it("should track 'Open app performed' when isRequestOpenAppExchange changes from true to false", () => { | ||
const { rerender } = renderHook((props: UseTrackExchangeFlow) => useTrackExchangeFlow(props), { | ||
initialProps: { ...defaultArgs, isRequestOpenAppExchange: true }, | ||
}); | ||
|
||
rerender({ ...defaultArgs, isRequestOpenAppExchange: false }); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Open app performed", | ||
expect.objectContaining({ | ||
deviceType: "stax", | ||
connectionType: CONNECTION_TYPES.USB, | ||
platform: "LLD", | ||
page: HOOKS_TRACKING_LOCATIONS.exchange, | ||
}), | ||
true, | ||
); | ||
}); | ||
|
||
it("should not track events if location is not 'Exchange'", () => { | ||
renderHook((props: UseTrackExchangeFlow) => useTrackExchangeFlow(props), { | ||
//@ts-expect-error location is not 'Exchange' | ||
initialProps: { ...defaultArgs, location: "NOT Exchange" }, | ||
}); | ||
|
||
expect(track).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should correctly determine connection type as 'BLE' when device.wired is false", () => { | ||
const bluetoothDeviceMock = { modelId: "stax", wired: false }; | ||
|
||
const { rerender } = renderHook((props: UseTrackExchangeFlow) => useTrackExchangeFlow(props), { | ||
initialProps: { ...defaultArgs, device: deviceMock }, | ||
}); | ||
|
||
rerender({ ...defaultArgs, device: bluetoothDeviceMock, error: new UserRefusedOnDevice() }); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Open app denied", | ||
expect.objectContaining({ | ||
deviceType: "stax", | ||
connectionType: CONNECTION_TYPES.BLE, | ||
platform: "LLD", | ||
page: HOOKS_TRACKING_LOCATIONS.exchange, | ||
}), | ||
true, | ||
); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
apps/ledger-live-desktop/src/renderer/analytics/hooks/useTrackExchangeFlow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useRef, useEffect } from "react"; | ||
import { UserRefusedAllowManager, UserRefusedOnDevice } from "@ledgerhq/errors"; | ||
import { track } from "../segment"; | ||
import { Device } from "@ledgerhq/types-devices"; | ||
import { LedgerError } from "~/renderer/components/DeviceAction"; | ||
import { CONNECTION_TYPES, HOOKS_TRACKING_LOCATIONS } from "./variables"; | ||
|
||
export type UseTrackExchangeFlow = { | ||
location: HOOKS_TRACKING_LOCATIONS.exchange | undefined; | ||
device: Device; | ||
error: | ||
| (LedgerError & { | ||
name?: string; | ||
managerAppName?: string; | ||
}) | ||
| undefined | ||
| null; | ||
isTrackingEnabled: boolean; | ||
isRequestOpenAppExchange: boolean | null; | ||
}; | ||
|
||
/** | ||
* a custom hook to track events in the Exchange flow. | ||
* tracks user interactions in the Exchange flow based on state changes and errors. | ||
* | ||
* @param location - current location in the app (expected "Exchange" from HOOKS_TRACKING_LOCATIONS enum). | ||
* @param device - the connected device information. | ||
* @param error - current error state. | ||
* @param isTrackingEnabled - flag indicating if tracking is enabled. | ||
* @param isRequestOpenAppExchange - flag indicating if LLD requested to open the exchange app. | ||
*/ | ||
export const useTrackExchangeFlow = ({ | ||
location, | ||
device, | ||
error, | ||
isTrackingEnabled, | ||
isRequestOpenAppExchange, | ||
}: UseTrackExchangeFlow) => { | ||
const previousIsRequestOpenAppExchange = useRef<boolean | null>(null); | ||
|
||
useEffect(() => { | ||
if (location !== HOOKS_TRACKING_LOCATIONS.exchange) return; | ||
|
||
const defaultPayload = { | ||
deviceType: device?.modelId, | ||
connectionType: device?.wired ? CONNECTION_TYPES.USB : CONNECTION_TYPES.BLE, | ||
platform: "LLD", | ||
page: "Exchange", | ||
}; | ||
|
||
if ((error as unknown) instanceof UserRefusedOnDevice) { | ||
// user refused to open exchange app | ||
track("Open app denied", defaultPayload, isTrackingEnabled); | ||
} else if ((error as unknown) instanceof UserRefusedAllowManager) { | ||
// user refused secure channel | ||
track("Secure Channel denied", defaultPayload, isTrackingEnabled); | ||
} | ||
|
||
if (previousIsRequestOpenAppExchange.current === true && isRequestOpenAppExchange === false) { | ||
// user opened exchange app | ||
track("Open app performed", defaultPayload, isTrackingEnabled); | ||
} | ||
|
||
previousIsRequestOpenAppExchange.current = isRequestOpenAppExchange; | ||
}, [error, location, isTrackingEnabled, device, isRequestOpenAppExchange]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters