-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (live-15768): Data tracking in the send flow
- Loading branch information
1 parent
4c07450
commit 7d8ff18
Showing
8 changed files
with
209 additions
and
19 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 send flow |
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
109 changes: 109 additions & 0 deletions
109
apps/ledger-live-desktop/src/renderer/analytics/hooks/useTrackSendFlow.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,109 @@ | ||
import { renderHook } from "tests/testUtils"; | ||
import { useTrackSendFlow, UseTrackSendFlow } from "./useTrackSendFlow"; | ||
import { track } from "../segment"; | ||
import { UserRefusedOnDevice } from "@ledgerhq/errors"; | ||
import { CONNECTION_TYPES, HOOKS_TRACKING_LOCATIONS } from "./variables"; | ||
|
||
jest.mock("../segment", () => ({ | ||
track: jest.fn(), | ||
setAnalyticsFeatureFlagMethod: jest.fn(), | ||
})); | ||
|
||
describe("useTrackSendFlow", () => { | ||
const deviceMock = { | ||
modelId: "europa", | ||
wired: false, | ||
}; | ||
|
||
const defaultArgs: UseTrackSendFlow = { | ||
location: HOOKS_TRACKING_LOCATIONS.sendModal, | ||
device: deviceMock, | ||
error: null, | ||
inWrongDeviceForAccount: null, | ||
isTrackingEnabled: true, | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should track 'Open app denied' when UserRefusedOnDevice error is thrown", () => { | ||
const error = new UserRefusedOnDevice(); | ||
|
||
renderHook((props: UseTrackSendFlow) => useTrackSendFlow(props), { | ||
initialProps: { ...defaultArgs, error }, | ||
}); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Open app denied", | ||
expect.objectContaining({ | ||
deviceType: "europa", | ||
connectionType: CONNECTION_TYPES.BLE, | ||
platform: "LLD", | ||
page: "Send", | ||
}), | ||
true, | ||
); | ||
}); | ||
|
||
it("should track 'Wrong device association' when inWrongDeviceForAccount is provided", () => { | ||
renderHook((props: UseTrackSendFlow) => useTrackSendFlow(props), { | ||
initialProps: { ...defaultArgs, inWrongDeviceForAccount: { accountName: "Test Account" } }, | ||
}); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Wrong device association", | ||
expect.objectContaining({ | ||
deviceType: "europa", | ||
connectionType: CONNECTION_TYPES.BLE, | ||
platform: "LLD", | ||
page: "Send", | ||
}), | ||
true, | ||
); | ||
}); | ||
|
||
it("should not track events if location is not 'Send Modal'", () => { | ||
renderHook((props: UseTrackSendFlow) => useTrackSendFlow(props), { | ||
initialProps: { ...defaultArgs, location: "Other Modal" }, | ||
}); | ||
|
||
expect(track).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should include correct connection type based on device.wired", () => { | ||
const wiredDeviceMock = { ...deviceMock, wired: true }; | ||
|
||
renderHook((props: UseTrackSendFlow) => useTrackSendFlow(props), { | ||
initialProps: { ...defaultArgs, device: wiredDeviceMock, error: new UserRefusedOnDevice() }, | ||
}); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Open app denied", | ||
expect.objectContaining({ | ||
deviceType: "europa", | ||
connectionType: CONNECTION_TYPES.USB, | ||
platform: "LLD", | ||
page: "Send", | ||
}), | ||
true, | ||
); | ||
}); | ||
|
||
it("should handle no tracking when isTrackingEnabled is false", () => { | ||
renderHook((props: UseTrackSendFlow) => useTrackSendFlow(props), { | ||
initialProps: { ...defaultArgs, isTrackingEnabled: false, error: new UserRefusedOnDevice() }, | ||
}); | ||
|
||
expect(track).toHaveBeenCalledWith( | ||
"Open app denied", | ||
expect.objectContaining({ | ||
deviceType: "europa", | ||
connectionType: CONNECTION_TYPES.BLE, | ||
platform: "LLD", | ||
page: "Send", | ||
}), | ||
false, | ||
); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
apps/ledger-live-desktop/src/renderer/analytics/hooks/useTrackSendFlow.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,63 @@ | ||
import { useEffect } from "react"; | ||
import { track } from "../segment"; | ||
import { Device } from "@ledgerhq/types-devices"; | ||
import { UserRefusedOnDevice } from "@ledgerhq/errors"; | ||
import { CONNECTION_TYPES, HOOKS_TRACKING_LOCATIONS } from "./variables"; | ||
import { LedgerError } from "~/renderer/components/DeviceAction"; | ||
|
||
export type UseTrackSendFlow = { | ||
location: HOOKS_TRACKING_LOCATIONS.sendModal | undefined; | ||
device: Device; | ||
error: | ||
| (LedgerError & { | ||
name?: string; | ||
managerAppName?: string; | ||
}) | ||
| undefined | ||
| null; | ||
inWrongDeviceForAccount: | ||
| { | ||
accountName: string; | ||
} | ||
| null | ||
| undefined; | ||
isTrackingEnabled: boolean; | ||
}; | ||
|
||
/** | ||
* a custom hook to track events in the Send modal. | ||
* tracks user interactions with the Send modal based on state changes and errors. | ||
* | ||
* @param location - current location in the app (expected "Send Modal" from HOOKS_TRACKING_LOCATIONS enum). | ||
* @param device - the connected device information. | ||
* @param error - current error state. | ||
* @param inWrongDeviceForAccount - error from verifying address. | ||
* @param isTrackingEnabled - flag indicating if tracking is enabled. | ||
*/ | ||
export const useTrackSendFlow = ({ | ||
location, | ||
device, | ||
error = null, | ||
inWrongDeviceForAccount = null, | ||
isTrackingEnabled, | ||
}: UseTrackSendFlow) => { | ||
useEffect(() => { | ||
if (location !== HOOKS_TRACKING_LOCATIONS.sendModal) return; | ||
|
||
const defaultPayload = { | ||
deviceType: device?.modelId, | ||
connectionType: device?.wired ? CONNECTION_TYPES.USB : CONNECTION_TYPES.BLE, | ||
platform: "LLD", | ||
page: "Send", | ||
}; | ||
|
||
if ((error as unknown) instanceof UserRefusedOnDevice) { | ||
// user refused to open app | ||
track("Open app denied", defaultPayload, isTrackingEnabled); | ||
} | ||
if (inWrongDeviceForAccount) { | ||
// device used is not associated with the account | ||
track("Wrong device association", defaultPayload, isTrackingEnabled); | ||
} | ||
}, [error, location, isTrackingEnabled, device, inWrongDeviceForAccount]); | ||
}; |
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