useAnalytics.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {useEffect} from 'react';
  2. import type {Organization} from 'sentry/types/organization';
  3. import {trackAnalytics} from 'sentry/utils/analytics';
  4. import type {UseApiQueryResult} from 'sentry/utils/queryClient';
  5. import type RequestError from 'sentry/utils/requestError/requestError';
  6. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  7. import type {Visualize} from './useVisualizes';
  8. export function useAnalytics({
  9. resultLength,
  10. resultMissingRoot,
  11. resultMode,
  12. resultStatus,
  13. visualizes,
  14. organization,
  15. columns,
  16. userQuery,
  17. }: {
  18. columns: string[];
  19. organization: Organization;
  20. resultLength: number | undefined;
  21. resultMode: 'span samples' | 'trace samples' | 'aggregates';
  22. resultStatus: UseApiQueryResult<any, RequestError>['status'];
  23. userQuery: string;
  24. visualizes: Visualize[];
  25. resultMissingRoot?: number;
  26. }) {
  27. useEffect(() => {
  28. if (resultStatus === 'pending') {
  29. return;
  30. }
  31. const search = new MutableSearch(userQuery);
  32. const params = {
  33. organization,
  34. columns,
  35. columns_count: columns.filter(Boolean).length,
  36. query_status: resultStatus,
  37. result_length: resultLength || 0,
  38. result_missing_root: resultMissingRoot || 0,
  39. result_mode: resultMode,
  40. user_queries: search.formatString(),
  41. user_queries_count: search.tokens.length,
  42. visualizes,
  43. visualizes_count: visualizes.length,
  44. };
  45. trackAnalytics('trace.explorer.metadata', params);
  46. }, [
  47. organization,
  48. resultLength,
  49. resultMissingRoot,
  50. resultMode,
  51. resultStatus,
  52. visualizes,
  53. columns,
  54. userQuery,
  55. ]);
  56. }