-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Introduce telemetry to wizard
- Loading branch information
1 parent
551368c
commit 33dfd90
Showing
4 changed files
with
283 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Metrics } from '../../internal/metrics'; | ||
import { WizardProps } from '../interfaces'; | ||
|
||
const prefix = 'csa_wizard'; | ||
|
||
const createEventType = (eventType: string) => `${prefix}_${eventType}`; | ||
const createEventContext = (stepIndex = 0) => `${prefix}_step${stepIndex + 1}`; | ||
const createEventDetail = (stepIndex = 0) => `step${stepIndex + 1}`; | ||
|
||
// A custom time cache is used to not clear the timer between navigation attempts | ||
// This allows us the ability to track time to attempt each step as well as the time to complete | ||
// each step | ||
const timeCache: Record<string, number> = {}; | ||
const timeStart = (key = 'current') => { | ||
timeCache[key] = Date.now(); | ||
}; | ||
|
||
const timeEnd = (key = 'current', clear = false) => { | ||
const start = timeCache[key]; | ||
// No start time is available when starting the first step | ||
if (!start) { | ||
return undefined; | ||
} | ||
|
||
if (clear) { | ||
delete timeCache[key]; | ||
} | ||
|
||
return (Date.now() - start) / 1000; // Convert to seconds | ||
}; | ||
|
||
export const useWizardAnalytics = () => { | ||
const trackStartStep = (stepIndex?: number) => { | ||
const eventContext = createEventContext(stepIndex); | ||
|
||
// Track the starting time of the wizard | ||
if (stepIndex === undefined) { | ||
timeStart(prefix); | ||
} | ||
|
||
// End the timer of the previous step | ||
const time = timeEnd(); | ||
|
||
// Start a new timer of the current step | ||
timeStart(); | ||
|
||
Metrics.sendPanoramaMetric('trackStartStep', { | ||
eventContext, | ||
eventDetail: createEventDetail(stepIndex), | ||
eventType: createEventType('step'), | ||
...(time !== undefined && { eventValue: time.toString() }), | ||
}); | ||
}; | ||
|
||
const trackNavigate = (activeStepIndex: number, requestedStepIndex: number, reason: WizardProps.NavigationReason) => { | ||
const eventContext = createEventContext(activeStepIndex); | ||
const time = timeEnd(); | ||
|
||
Metrics.sendPanoramaMetric('trackNavigate', { | ||
eventContext, | ||
eventDetail: createEventDetail(requestedStepIndex), | ||
eventType: createEventType('navigate'), | ||
eventValue: { reason, ...(time !== undefined && { time }) }, | ||
}); | ||
}; | ||
|
||
const trackSubmit = (stepIndex: number) => { | ||
const eventContext = createEventContext(stepIndex); | ||
// End the timer of the wizard | ||
const time = timeEnd(prefix); | ||
|
||
Metrics.sendPanoramaMetric('trackSubmit', { | ||
eventContext, | ||
eventDetail: createEventDetail(stepIndex), | ||
eventType: createEventType('submit'), | ||
...(time !== undefined && { eventValue: time.toString() }), | ||
}); | ||
}; | ||
|
||
return { trackStartStep, trackNavigate, trackSubmit }; | ||
}; |