React
TracewayProvider

TracewayProvider

The TracewayProvider component initializes Traceway and provides context to child components.

Basic Usage

import { TracewayProvider } from "@tracewayapp/react";
 
function App() {
  return (
    <TracewayProvider connectionString="your-token@https://traceway.example.com/api/report">
      <YourApp />
    </TracewayProvider>
  );
}

Props

PropTypeRequiredDescription
connectionStringstringYesYour Traceway connection string
optionsobjectNoConfiguration options
childrenReactNodeYesChild components

Options

OptionTypeDefaultDescription
debugbooleanfalseLog events to console
debounceMsnumber1500Batch delay in milliseconds
retryDelayMsnumber10000Retry delay for failed uploads
versionstringundefinedYour application version
ignoreErrorsArray<string | RegExp>DEFAULT_IGNORE_PATTERNSError patterns to ignore. Pass [] to capture all errors. See Error Filtering
beforeCapture(exception) => booleanundefinedReturn false to suppress an error. See Error Filtering
sessionRecordingbooleantrueEnable the rrweb session recorder
sessionRecordingSegmentDurationnumber30000rrweb segment length in ms
recordAllSessionsbooleanfalseAlways-on session recording. See Sessions
captureLogsbooleantrueMirror console.* calls into the rolling log buffer
captureNetworkbooleantrueRecord fetch / XHR calls as network actions
captureNavigationbooleantrueRecord History API push / replace / pop transitions
eventsWindowMsnumber10000 (30000 w/ recordAllSessions)Rolling log/action buffer window
eventsMaxCountnumber200 (600 w/ recordAllSessions)Hard cap on log/action buffer entries

Custom Attributes

Use <TracewayAttributes> or the useTracewayAttributes hook to bind a reactive map of attributes (userId, tenant, feature flags, etc.) to the SDK's global scope. The hook diffs against the previous map on every render and pushes only the deltas; on unmount, every key it currently owns is removed.

import { TracewayAttributes, useTracewayAttributes } from "@tracewayapp/react";
 
// As a component:
<TracewayAttributes attributes={user ? { userId: user.id, tenant: org.id } : null} />
 
// Or as a hook:
function App() {
  useTracewayAttributes({ userId: user?.id, tenant: org?.id });
  return <Routes />;
}

Both accept null / undefined as "empty map" — useful while user data loads or after logout. New object reference with the same content does not trigger SDK calls.

For imperative use (background workers, init scripts), the same primitives are exported as plain functions:

import { setAttribute, setAttributes, removeAttribute, clearAttributes } from "@tracewayapp/react";
 
setAttribute("build_channel", import.meta.env.VITE_CHANNEL ?? "dev");
setAttributes({ tenant: "acme", plan: "pro" });
clearAttributes(); // on logout

Layering on each event: defaults < global scope < per-call. See Sessions for the full attribute model.

Example with Options

<TracewayProvider
  connectionString="your-token@https://traceway.example.com/api/report"
  options={{
    debug: process.env.NODE_ENV === "development",
    version: process.env.REACT_APP_VERSION,
    debounceMs: 1000,
  }}
>
  <YourApp />
</TracewayProvider>

Capture All Errors

By default, 4xx HTTP errors, network errors, and timeouts are ignored. To capture everything:

<TracewayProvider
  connectionString="your-token@https://traceway.example.com/api/report"
  options={{ ignoreErrors: [] }}
>
  <YourApp />
</TracewayProvider>

Environment-Specific Setup

function App() {
  const connectionString = process.env.NODE_ENV === "production"
    ? process.env.REACT_APP_TRACEWAY_PROD
    : process.env.REACT_APP_TRACEWAY_DEV;
 
  return (
    <TracewayProvider connectionString={connectionString}>
      <YourApp />
    </TracewayProvider>
  );
}

Placement

Place TracewayProvider as high as possible in your component tree, typically in your root App component or index.js:

// index.js
import React from "react";
import ReactDOM from "react-dom/client";
import { TracewayProvider } from "@tracewayapp/react";
import App from "./App";
 
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <TracewayProvider connectionString="your-token@...">
      <App />
    </TracewayProvider>
  </React.StrictMode>
);

TracewayContext

For advanced use cases, you can access the context directly:

import { TracewayContext } from "@tracewayapp/react";
import { useContext } from "react";
 
function MyComponent() {
  const traceway = useContext(TracewayContext);
  // Use traceway.captureException, etc.
}

However, the useTraceway hook is preferred for most use cases.