use-editor-analytics.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { useIsNewEditorEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
  2. import {
  3. Segmentation,
  4. sendMB,
  5. sendMBOnce,
  6. sendMBSampled,
  7. } from '@/infrastructure/event-tracking'
  8. import { useCallback } from 'react'
  9. export const useEditorAnalytics = () => {
  10. const editorRedesign = useIsNewEditorEnabled()
  11. const populateSegmentation = useCallback(
  12. (segmentation: Segmentation | undefined = {}): Segmentation => {
  13. return editorRedesign
  14. ? { ...segmentation, 'editor-redesign': 'enabled' }
  15. : segmentation
  16. },
  17. [editorRedesign]
  18. )
  19. const sendEvent: typeof sendMB = useCallback(
  20. (key, segmentation) => {
  21. sendMB(key, populateSegmentation(segmentation))
  22. },
  23. [populateSegmentation]
  24. )
  25. const sendEventOnce: typeof sendMBOnce = useCallback(
  26. (key, segmentation) => {
  27. sendMBOnce(key, populateSegmentation(segmentation))
  28. },
  29. [populateSegmentation]
  30. )
  31. const sendEventSampled: typeof sendMBSampled = useCallback(
  32. (key, segmentation, rate) => {
  33. sendMBSampled(key, populateSegmentation(segmentation), rate)
  34. },
  35. [populateSegmentation]
  36. )
  37. return { sendEvent, sendEventOnce, sendEventSampled }
  38. }