Skip to content

Commit

Permalink
fix(app): Fix run start/finish protocol analytics (#17118)
Browse files Browse the repository at this point in the history
Closes EXEC-850 and EXEC-805
  • Loading branch information
mjhuff authored Dec 17, 2024
1 parent d69ca2b commit 98afdcf
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ import { useProtocolDetailsForRun } from '/app/resources/runs'
import { getFallbackRobotSerialNumber } from '../utils'
import {
ANALYTICS_PROTOCOL_PROCEED_TO_RUN,
ANALYTICS_PROTOCOL_RUN_ACTION,
useTrackEvent,
} from '/app/redux/analytics'
import {
useRobotAnalyticsData,
useTrackProtocolRunEvent,
} from '/app/redux-resources/analytics'
import { useRobot, useRobotType } from '/app/redux-resources/robots'

import type { AttachedModule, RunStatus, Run } from '@opentrons/api-client'
import type { UseErrorRecoveryResult } from '/app/organisms/ErrorRecoveryFlows'
import type {
Expand Down Expand Up @@ -71,14 +77,20 @@ export function useRunHeaderModalContainer({
const robot = useRobot(robotName)
const robotSerialNumber = getFallbackRobotSerialNumber(robot)
const trackEvent = useTrackEvent()
const { trackProtocolRunEvent } = useTrackProtocolRunEvent(runId, robotName)
const robotType = useRobotType(robotName)
const robotAnalyticsData = useRobotAnalyticsData(robotName)

function handleProceedToRunClick(): void {
navigate(`/devices/${robotName}/protocol-runs/${runId}/run-preview`)
trackEvent({
name: ANALYTICS_PROTOCOL_PROCEED_TO_RUN,
properties: { robotSerialNumber },
})
trackProtocolRunEvent({
name: ANALYTICS_PROTOCOL_RUN_ACTION.START,
properties: robotAnalyticsData ?? {},
})
protocolRunControls.play()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ export function useRunAnalytics({

useEffect(() => {
const areReportConditionsValid =
isRunCurrent &&
runId != null &&
robotAnalyticsData != null &&
isTerminalRunStatus(runStatus)
isRunCurrent && runId != null && isTerminalRunStatus(runStatus)

if (areReportConditionsValid) {
trackProtocolRunEvent({
name: ANALYTICS_PROTOCOL_RUN_ACTION.FINISH,
properties: robotAnalyticsData,
properties: robotAnalyticsData ?? undefined,
})
}
}, [runStatus, isRunCurrent, runId, robotAnalyticsData])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ describe('ProtocolSetup', () => {
render(`/runs/${RUN_ID}/setup/`)

fireEvent.click(screen.getByRole('button', { name: 'play' }))
expect(mockTrackProtocolRunEvent).toBeCalledTimes(1)
expect(mockTrackProtocolRunEvent).toHaveBeenCalledWith({
name: ANALYTICS_PROTOCOL_RUN_ACTION.START,
properties: {},
Expand Down
8 changes: 8 additions & 0 deletions app/src/pages/ODD/ProtocolSetup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,19 @@ export function ProtocolSetup(): JSX.Element {
robotType,
protocolName
)

const { trackProtocolRunEvent } = useTrackProtocolRunEvent(runId, robotName)
const robotAnalyticsData = useRobotAnalyticsData(robotName)

const handleProceedToRunClick = (): void => {
trackEvent({
name: ANALYTICS_PROTOCOL_PROCEED_TO_RUN,
properties: { robotSerialNumber },
})
trackProtocolRunEvent({
name: ANALYTICS_PROTOCOL_RUN_ACTION.START,
properties: robotAnalyticsData ?? {},
})
play()
}
const configBypassHeaterShakerAttachmentConfirmation = useSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as React from 'react'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import { QueryClient, QueryClientProvider } from 'react-query'
import { vi, it, expect, describe, beforeEach, afterEach } from 'vitest'
import { vi, it, expect, describe, beforeEach } from 'vitest'
import { when } from 'vitest-when'
import { waitFor, renderHook } from '@testing-library/react'

Expand Down Expand Up @@ -63,10 +63,6 @@ describe('useTrackProtocolRunEvent hook', () => {
})
})

afterEach(() => {
vi.resetAllMocks()
})

it('returns trackProtocolRunEvent function', () => {
const { result } = renderHook(
() => useTrackProtocolRunEvent(RUN_ID, ROBOT_NAME),
Expand All @@ -92,7 +88,7 @@ describe('useTrackProtocolRunEvent hook', () => {
)
expect(mockTrackEvent).toHaveBeenCalledWith({
name: ANALYTICS_PROTOCOL_RUN_ACTION.START,
properties: PROTOCOL_PROPERTIES,
properties: { ...PROTOCOL_PROPERTIES, transactionId: RUN_ID },
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export function useTrackProtocolRunEvent(
...properties,
...protocolRunAnalyticsData,
runTime,
// It's sometimes unavoidable (namely on the desktop app) to prevent sending an event multiple times.
// In these circumstances, we need an idempotency key to accurately filter events in Mixpanel.
transactionId: runId,
},
})
})
Expand Down

0 comments on commit 98afdcf

Please sign in to comment.