|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-return */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 3 | +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
| 4 | + |
| 5 | +import { datadogRum } from '@datadog/browser-rum'; |
| 6 | + |
| 7 | +// --- Datadog RUM + React Router v5 wiring (no monkeypatching) --- |
| 8 | +import type { Location } from 'history'; |
| 9 | + |
| 10 | +function pathFromLocation( |
| 11 | + loc: Location | { pathname: string; search?: string; hash?: string } |
| 12 | +): string { |
| 13 | + const search = loc.search ?? ''; |
| 14 | + const hash = loc.hash ?? ''; |
| 15 | + return `${loc.pathname}${search}${hash}`; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Call this once, right after you create your app's history and before rendering the <Router>. |
| 20 | + * Example: |
| 21 | + * // history.ts |
| 22 | + * export const history = createBrowserHistory(); |
| 23 | + * |
| 24 | + * // index.tsx |
| 25 | + * import { history } from './history'; |
| 26 | + * import { attachRumToHistory } from './datadog'; |
| 27 | + * attachRumToHistory(history); |
| 28 | + */ |
| 29 | +export function attachRumToHistory(history: { |
| 30 | + location: Location; |
| 31 | + listen: (cb: (loc: Location) => void) => void; |
| 32 | +}): void { |
| 33 | + // Start initial view |
| 34 | + datadogRum.startView({ name: pathFromLocation(history.location) }); |
| 35 | + // Track subsequent route changes |
| 36 | + history.listen((location) => { |
| 37 | + datadogRum.startView({ name: pathFromLocation(location) }); |
| 38 | + }); |
| 39 | +} |
| 40 | +// --- End Datadog RUM + React Router v5 wiring --- |
| 41 | + |
| 42 | +datadogRum.init({ |
| 43 | + applicationId: 'dfabd184-b8ed-47db-90bc-2a4c26828b72', |
| 44 | + clientToken: 'pub2b91b53e324deaa20ce2c3738fc4f9aa', |
| 45 | + site: 'datadoghq.com', |
| 46 | + service: 'fourtiesnyc', |
| 47 | + env: __DEPLOY_ENV__ || 'dev', |
| 48 | + version: __GIT_SHA__, |
| 49 | + // Explicitly enable manual view tracking for RRv5 |
| 50 | + trackViewsManually: true, |
| 51 | + defaultPrivacyLevel: 'mask-user-input', |
| 52 | + // plugins: [reactPlugin({ router: false })], // Keep disabled until upgrading React Router due to deps conflict |
| 53 | +}); |
| 54 | +datadogRum.setGlobalContextProperty('branch', __BRANCH__); |
0 commit comments