react-tracking is a declarative and imperative tracking library specifically designed for React applications. It provides an analytics platform-agnostic solution, empowering developers to manage and dispatch user interaction data effectively. The library's core philosophy is to compartmentalize tracking concerns directly within individual components, thereby preventing data leakage and ensuring a clean separation of concerns across the entire application. It offers a dual API approach, supporting both the traditional Higher-Order Component (HoC) or decorator pattern and a modern React Hooks API, which was fully introduced in version 8.1.0 for functional components. The current stable version, 9.3.2, has recently addressed compatibility issues with React Native environments and re-enabled support for Node.js 16.9+. Developed by The New York Times, react-tracking is actively maintained, with a consistent cadence of patch and minor releases addressing bug fixes and introducing new features like `mergeOptions` (v9.3.0) and `deepmerge` re-export (v9.2.0). Its key differentiators include its highly declarative nature, flexibility to integrate seamlessly with virtually any analytics backend, and comprehensive support for both class-based and modern functional React components, making it a versatile choice for instrumenting React UIs.
npm install react-trackingVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use the `useTracking` hook to declare component-level tracking data, imperatively dispatch events with `trackEvent`, access contextual data with `getTrackingData`, and use the `<Track>` component to propagate context to child components. It also shows options like `dispatchOnMount` and `mergeOptions`.
Ensure your application's build process or entry points include necessary polyfills (e.g., via `core-js`, Babel, or polyfill.io) if your target browsers require them.
For decorator usage, pass `{ forwardRef: true }` as the second argument: `@track({}, { forwardRef: true })`. This allows `ref` to correctly point to the decorated component instance.Upgrade to `react-tracking@7.3.0` or newer. If stuck on older versions, ensure decorated async methods explicitly return their original promise or handle `trackEvent` calls carefully.
Ensure the `dispatchOnMount` function is optimized for performance and idempotent, as it runs on every initial render. Avoid unnecessary complex logic within this function.
Upgrade to `react-tracking@9.3.1` or newer if your project relies on Node.js 16.9+ environments.
Upgrade `react-tracking` to version `7.3.0` or newer. If unable to upgrade, ensure your decorated async methods handle potential `null` or `undefined` values when calling `trackEvent`.
Ensure `useTracking` is only invoked directly within the body of a React functional component or another custom React Hook function. Do not call Hooks from regular JavaScript functions, class components, or event handlers directly.
Run `npm install --save react-tracking` or `yarn add react-tracking`. Double-check the import statement for any typos, ensuring it is `import { useTracking } from 'react-tracking';` or `import track from 'react-tracking';`.