useButtonTracking.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {useCallback, useContext} from 'react';
  2. import type {ButtonProps} from 'sentry/components/button';
  3. import {useRoutes} from 'sentry/utils/useRoutes';
  4. import {OrganizationContext} from 'sentry/views/organizationContext';
  5. import rawTrackAnalyticsEvent from 'getsentry/utils/rawTrackAnalyticsEvent';
  6. import {convertToReloadPath, getEventPath} from 'getsentry/utils/routeAnalytics';
  7. type Props = ButtonProps;
  8. export default function useButtonTracking({
  9. analyticsEventName,
  10. analyticsEventKey,
  11. analyticsParams,
  12. 'aria-label': ariaLabel,
  13. }: Props) {
  14. const organization = useContext(OrganizationContext);
  15. const routes = useRoutes();
  16. const trackButton = useCallback(() => {
  17. const considerSendingAnalytics = organization && routes;
  18. if (considerSendingAnalytics) {
  19. const routeString = getEventPath(routes);
  20. const reloadPath = convertToReloadPath(routeString);
  21. // optional way to override the event name for Reload and Amplitude
  22. // note null means something different than undefined for eventName so
  23. // checking for that explicitly
  24. const eventKey =
  25. analyticsEventKey !== undefined
  26. ? analyticsEventKey
  27. : `button_click.${reloadPath}`;
  28. const eventName = analyticsEventName !== undefined ? analyticsEventName : null;
  29. rawTrackAnalyticsEvent({
  30. eventKey,
  31. eventName,
  32. organization,
  33. // pass in the parameterized path as well
  34. parameterized_path: reloadPath,
  35. text: ariaLabel,
  36. ...analyticsParams,
  37. });
  38. }
  39. }, [
  40. analyticsEventKey,
  41. analyticsEventName,
  42. analyticsParams,
  43. ariaLabel,
  44. organization,
  45. routes,
  46. ]);
  47. return trackButton;
  48. }