use-editor-analytics.ts 1.2 KB

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