Files
sign/packages/lib/client-only/hooks/use-analytics.ts

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-09-20 13:48:30 +10:00
import { posthog } from 'posthog-js';
2025-01-02 15:33:37 +11:00
import { extractPostHogConfig } from '@documenso/lib/constants/feature-flags';
2023-09-20 13:48:30 +10:00
export function useAnalytics() {
2025-01-02 15:33:37 +11:00
// const featureFlags = useFeatureFlags();
2023-09-20 13:48:30 +10:00
const isPostHogEnabled = extractPostHogConfig();
/**
* Capture an analytic event.
*
* @param event The event name.
* @param properties Properties to attach to the event.
*/
const capture = (event: string, properties?: Record<string, unknown>) => {
if (!isPostHogEnabled) {
return;
}
posthog.capture(event, properties);
};
2025-02-25 15:14:45 +11:00
/**
* Capture an analytic event.
*
* @param error The error to capture.
* @param properties Properties to attach to the event.
*/
const captureException = (error: Error, properties?: Record<string, unknown>) => {
if (!isPostHogEnabled) {
return;
}
posthog.captureException(error, properties);
};
2023-09-20 13:48:30 +10:00
/**
* Start the session recording.
*
* @param eventFlag The event to check against feature flags to determine whether tracking is enabled.
*/
const startSessionRecording = (eventFlag?: string) => {
2025-01-02 15:33:37 +11:00
return;
// const isSessionRecordingEnabled = featureFlags.getFlag(FEATURE_FLAG_GLOBAL_SESSION_RECORDING);
// const isSessionRecordingEnabledForEvent = Boolean(eventFlag && featureFlags.getFlag(eventFlag));
2023-09-20 13:48:30 +10:00
2025-01-02 15:33:37 +11:00
// if (!isPostHogEnabled || !isSessionRecordingEnabled || !isSessionRecordingEnabledForEvent) {
// return;
// }
2023-09-20 13:48:30 +10:00
2025-01-02 15:33:37 +11:00
// posthog.startSessionRecording();
2023-09-20 13:48:30 +10:00
};
/**
* Stop the current session recording.
*/
const stopSessionRecording = () => {
2025-01-02 15:33:37 +11:00
return;
// const isSessionRecordingEnabled = featureFlags.getFlag(FEATURE_FLAG_GLOBAL_SESSION_RECORDING);
2023-09-20 13:48:30 +10:00
2025-01-02 15:33:37 +11:00
// if (!isPostHogEnabled || !isSessionRecordingEnabled) {
// return;
// }
2023-09-20 13:48:30 +10:00
2025-01-02 15:33:37 +11:00
// posthog.stopSessionRecording();
2023-09-20 13:48:30 +10:00
};
return {
capture,
2025-02-25 15:14:45 +11:00
captureException,
2023-09-20 13:48:30 +10:00
startSessionRecording,
stopSessionRecording,
};
}