Skip to main content

Overview

The useErrorTracking hook provides methods for tracking JavaScript errors and custom error events. It also automatically tracks unhandled errors and promise rejections when the component is mounted.

Usage

Automatic Error Tracking

When mounted, useErrorTracking automatically listens for:
  • JavaScript errors (window.error event)
  • Unhandled promise rejections (window.unhandledrejection event)
These errors are automatically tracked with contextual information including filename, line number, and column number.

Returns

function
Track JavaScript Error objects with additional contextSignature: (error: Error, properties?: EventProperties) => void
Tracked Data:
  • message: Error message
  • stack: Stack trace
  • Any additional properties passed
function
Track custom error messages with optional contextSignature: (message: string, properties?: EventProperties) => void
Tracked Data:
  • message: Custom error message
  • Any additional properties passed

Type Definitions

EventProperties

Examples

API Error Tracking

Form Validation Errors

React Error Boundary Integration

Tracking Third-Party Library Errors

Automatic Tracking Details

JavaScript Errors

Automatically tracked with:
  • message: Error message
  • stack: Stack trace
  • filename: File where error occurred
  • lineno: Line number
  • colno: Column number

Unhandled Promise Rejections

Automatically tracked with:
  • message: Rejection reason
  • type: “unhandledrejection”

Notes

Error tracking is automatically enabled when the component mounts and disabled when it unmounts. Only mount this hook in components that persist throughout your app lifecycle, or use it selectively where needed.
Combine error tracking with session data to understand the context in which errors occur and identify patterns in user behavior that lead to errors.
Avoid tracking sensitive information in error properties. Always sanitize user data before including it in error tracking.