Skip to content

Commit f541bc6

Browse files
committed
Use RUM with history patching and react-router 5.
1 parent 424823a commit f541bc6

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

frontend/package-lock.json

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"author": "Julian Boilen",
1818
"license": "UNLICENSED",
1919
"dependencies": {
20+
"@datadog/browser-rum": "^6.22.0",
2021
"@jboolean/react-zoom-pan-pinch": "^3.7.2",
2122
"@react-hook/window-size": "^3.1.1",
2223
"@sentry/react": "^7.119.2",

frontend/src/datadog.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 ---

frontend/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Sentry.init({
2424
tracesSampleRate: 1.0,
2525
});
2626

27+
import './datadog';
28+
2729
const containerEl = document.getElementById('app-container');
2830

2931
window.netlifyIdentity = netlifyIdentity;

0 commit comments

Comments
 (0)