Skip to content

Commit

Permalink
refactor(app): consolidate initialAction and errorEvent recovery anal…
Browse files Browse the repository at this point in the history
…ytics (#15992)
  • Loading branch information
mjhuff authored Aug 14, 2024
1 parent 286f361 commit 0dac337
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 50 deletions.
6 changes: 3 additions & 3 deletions app/src/organisms/ErrorRecoveryFlows/RunPausedSplash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function RunPausedSplash(
const title = useErrorName(errorKind)

const { proceedToRouteAndStep } = routeUpdateActions
const { reportInitialActionEvent } = analytics
const { reportErrorEvent } = analytics

const buildTitleHeadingDesktop = (): JSX.Element => {
return (
Expand All @@ -91,14 +91,14 @@ export function RunPausedSplash(
// Do not launch error recovery, but do utilize the wizard's cancel route.
const onCancelClick = (): Promise<void> => {
return toggleERWizAsActiveUser(true, false).then(() => {
reportInitialActionEvent('cancel-run')
reportErrorEvent(failedCommand?.byRunRecord ?? null, 'cancel-run')
void proceedToRouteAndStep(RECOVERY_MAP.CANCEL_RUN.ROUTE)
})
}

const onLaunchERClick = (): Promise<void> => {
return toggleERWizAsActiveUser(true, true).then(() => {
reportInitialActionEvent('launch-recovery')
reportErrorEvent(failedCommand?.byRunRecord ?? null, 'launch-recovery')
})
}

Expand Down
1 change: 0 additions & 1 deletion app/src/organisms/ErrorRecoveryFlows/__fixtures__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export const mockRecoveryContentProps: RecoveryContentProps = {
reportErrorEvent: () => {},
reportViewErrorDetailsEvent: () => {},
reportActionSelectedEvent: () => {},
reportInitialActionEvent: () => {},
reportActionSelectedResult: () => {},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,4 @@ describe('ErrorRecoveryFlows', () => {
render(props)
expect(screen.queryByText('MOCK RUN PAUSED SPLASH')).not.toBeInTheDocument()
})

it('calls reportErrorEvent with failedCommand on mount and when failedCommand changes', () => {
const mockReportErrorEvent = vi.fn()
vi.mocked(useRecoveryAnalytics).mockReturnValue({
reportErrorEvent: mockReportErrorEvent,
} as any)

const { rerender } = render(props)
expect(mockReportErrorEvent).toHaveBeenCalledWith(mockFailedCommand)

const newProps = {
...props,
failedCommandByRunRecord: null,
}
rerender(<ErrorRecoveryFlows {...newProps} />)
expect(mockReportErrorEvent).toHaveBeenCalledWith(
newProps.failedCommandByRunRecord
)
})
})
7 changes: 5 additions & 2 deletions app/src/organisms/ErrorRecoveryFlows/hooks/useERUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useRecoveryOptionCopy } from './useRecoveryOptionCopy'
import { useRecoveryActionMutation } from './useRecoveryActionMutation'
import { useRunningStepCounts } from '../../../resources/protocols/hooks'
import { useRecoveryToasts } from './useRecoveryToasts'
import { useRecoveryAnalytics } from './useRecoveryAnalytics'

import type { PipetteData } from '@opentrons/api-client'
import type { RobotType } from '@opentrons/shared-data'
Expand All @@ -40,7 +41,6 @@ export type ERUtilsProps = Omit<ErrorRecoveryFlowsProps, 'failedCommand'> & {
hasLaunchedRecovery: boolean
isOnDevice: boolean
robotType: RobotType
analytics: UseRecoveryAnalyticsResult
failedCommand: ReturnType<typeof useRetainedFailedCommandBySource>
}

Expand All @@ -59,6 +59,7 @@ export interface ERUtilsResults {
stepCounts: StepCounts
commandsAfterFailedCommand: ReturnType<typeof getNextSteps>
subMapUtils: SubMapUtils
analytics: UseRecoveryAnalyticsResult
}

const SUBSEQUENT_COMMAND_DEPTH = 2
Expand All @@ -71,7 +72,6 @@ export function useERUtils({
protocolAnalysis,
isOnDevice,
robotType,
analytics,
}: ERUtilsProps): ERUtilsResults {
const { data: attachedInstruments } = useInstrumentsQuery()
const { data: runRecord } = useNotifyRunQuery(runId)
Expand All @@ -88,6 +88,8 @@ export function useERUtils({

const stepCounts = useRunningStepCounts(runId, runCommands)

const analytics = useRecoveryAnalytics()

const {
recoveryMap,
setRM,
Expand Down Expand Up @@ -172,5 +174,6 @@ export function useERUtils({
getRecoveryOptionCopy,
stepCounts,
commandsAfterFailedCommand,
analytics,
}
}
24 changes: 9 additions & 15 deletions app/src/organisms/ErrorRecoveryFlows/hooks/useRecoveryAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ANALYTICS_RECOVERY_ACTION_RESULT,
ANALYTICS_RECOVERY_ACTION_SELECTED,
ANALYTICS_RECOVERY_ERROR_EVENT,
ANALYTICS_RECOVERY_INITIAL_ACTION,
ANALYTICS_RECOVERY_RUN_RESULT,
ANALYTICS_RECOVERY_VIEW_ERROR_DETAILS,
useTrackEvent,
Expand All @@ -18,9 +17,10 @@ type CommandResult = 'succeeded' | 'failed'

export interface UseRecoveryAnalyticsResult {
/* Report the error which occurs error recovery is currently handling. */
reportErrorEvent: (failedCommand: FailedCommand | null) => void
/* Report which action the user selected on the recovery splash screen. */
reportInitialActionEvent: (initialAction: InitialActionType) => void
reportErrorEvent: (
failedCommand: FailedCommand | null,
initialAction: InitialActionType
) => void
/* Report which recovery option the user selected. */
reportActionSelectedEvent: (selectedRecoveryOption: RecoveryRoute) => void
/* Report when the user views the error details and where they currently are in Error Recovery. */
Expand All @@ -40,27 +40,22 @@ export interface UseRecoveryAnalyticsResult {
export function useRecoveryAnalytics(): UseRecoveryAnalyticsResult {
const doTrackEvent = useTrackEvent()

const reportErrorEvent = (failedCommand: FailedCommand | null): void => {
const reportErrorEvent = (
failedCommand: FailedCommand | null,
initialAction: InitialActionType
): void => {
if (failedCommand != null) {
doTrackEvent({
name: ANALYTICS_RECOVERY_ERROR_EVENT,
properties: {
errorEvent: failedCommand.commandType,
errorString: failedCommand.error?.detail,
initialAction,
},
})
}
}

const reportInitialActionEvent = (initialAction: InitialActionType): void => {
doTrackEvent({
name: ANALYTICS_RECOVERY_INITIAL_ACTION,
properties: {
initialAction,
},
})
}

const reportActionSelectedEvent = (
selectedRecoveryOption: RecoveryRoute
): void => {
Expand Down Expand Up @@ -120,7 +115,6 @@ export function useRecoveryAnalytics(): UseRecoveryAnalyticsResult {
reportActionSelectedEvent,
reportActionSelectedResult,
reportErrorEvent,
reportInitialActionEvent,
reportViewErrorDetailsEvent,
reportRecoveredRunResult,
}
Expand Down
9 changes: 0 additions & 9 deletions app/src/organisms/ErrorRecoveryFlows/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { RecoveryTakeover } from './RecoveryTakeover'
import {
useCurrentlyRecoveringFrom,
useERUtils,
useRecoveryAnalytics,
useRecoveryTakeover,
useRetainedFailedCommandBySource,
useShowDoorInfo,
Expand Down Expand Up @@ -124,11 +123,6 @@ export function ErrorRecoveryFlows(
protocolAnalysis
)

const analytics = useRecoveryAnalytics()
React.useEffect(() => {
analytics.reportErrorEvent(failedCommandByRunRecord)
}, [failedCommandByRunRecord?.error?.detail])

const { hasLaunchedRecovery, toggleERWizard, showERWizard } = useERWizard()
const isOnDevice = useSelector(getIsOnDevice)
const robotType = protocolAnalysis?.robotType ?? OT2_ROBOT_TYPE
Expand All @@ -150,7 +144,6 @@ export function ErrorRecoveryFlows(
toggleERWizAsActiveUser,
isOnDevice,
robotType,
analytics,
failedCommand: failedCommandBySource,
})

Expand All @@ -171,7 +164,6 @@ export function ErrorRecoveryFlows(
robotType={robotType}
isOnDevice={isOnDevice}
isDoorOpen={isDoorOpen}
analytics={analytics}
failedCommand={failedCommandBySource}
/>
) : null}
Expand All @@ -183,7 +175,6 @@ export function ErrorRecoveryFlows(
robotName={robotName}
isOnDevice={isOnDevice}
toggleERWizAsActiveUser={toggleERWizAsActiveUser}
analytics={analytics}
failedCommand={failedCommandBySource}
/>
) : null}
Expand Down
1 change: 0 additions & 1 deletion app/src/redux/analytics/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export const ANALYTICS_ROBOT_UPDATE_CHANGE_LOG_VIEW = 'robotUpdateChangeLogView'
*/

export const ANALYTICS_RECOVERY_ERROR_EVENT = 'recoveryErrorEvent'
export const ANALYTICS_RECOVERY_INITIAL_ACTION = 'recoveryInitialAction'
export const ANALYTICS_RECOVERY_ACTION_SELECTED =
'recoverySelectedRecoveryAction'
export const ANALYTICS_RECOVERY_VIEW_ERROR_DETAILS = 'recoveryViewErrorDetails'
Expand Down

0 comments on commit 0dac337

Please sign in to comment.