|
| 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 | +datadogRum.init({ |
| 8 | + applicationId: 'dfabd184-b8ed-47db-90bc-2a4c26828b72', |
| 9 | + clientToken: 'pub2b91b53e324deaa20ce2c3738fc4f9aa', |
| 10 | + site: 'datadoghq.com', |
| 11 | + service: 'fourtiesnyc', |
| 12 | + env: __DEV__ ? 'dev' : 'prod', |
| 13 | + // Explicitly enable manual view tracking for RRv5 |
| 14 | + trackViewsManually: true, |
| 15 | + defaultPrivacyLevel: 'mask-user-input', |
| 16 | + // plugins: [reactPlugin({ router: false })], // Keep disabled until upgrading React Router due to deps conflict |
| 17 | +}); |
| 18 | + |
| 19 | +// --- Datadog RUM manual SPA view tracking for React Router v5 --- |
| 20 | +function currentPath(): string { |
| 21 | + return ( |
| 22 | + window.location.pathname + window.location.search + window.location.hash |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +// Start the initial view |
| 27 | +datadogRum.startView({ name: currentPath() }); |
| 28 | + |
| 29 | +// Patch pushState and replaceState to detect programmatic navigations |
| 30 | +(function patchHistoryForRum() { |
| 31 | + const originalPushState = history.pushState; |
| 32 | + const originalReplaceState = history.replaceState; |
| 33 | + |
| 34 | + history.pushState = function (...args) { |
| 35 | + const ret = originalPushState.apply(this, args as any); |
| 36 | + // Defer to allow location to update |
| 37 | + queueMicrotask(() => datadogRum.startView({ name: currentPath() })); |
| 38 | + return ret; |
| 39 | + } as typeof history.pushState; |
| 40 | + |
| 41 | + history.replaceState = function (...args) { |
| 42 | + const ret = originalReplaceState.apply(this, args as any); |
| 43 | + queueMicrotask(() => datadogRum.startView({ name: currentPath() })); |
| 44 | + return ret; |
| 45 | + } as typeof history.replaceState; |
| 46 | + |
| 47 | + // Back/forward buttons |
| 48 | + window.addEventListener('popstate', () => { |
| 49 | + datadogRum.startView({ name: currentPath() }); |
| 50 | + }); |
| 51 | +})(); |
| 52 | +// --- End Datadog RUM manual SPA view tracking --- |
0 commit comments