Skip to content

Commit 07069d7

Browse files
authored
Merge pull request #1303 from jboolean/datadog-rum
Add Datadog RUM
2 parents 9a87a01 + 8e88c59 commit 07069d7

File tree

8 files changed

+133
-4
lines changed

8 files changed

+133
-4
lines changed

frontend/.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"node": true
88
},
99
"globals": {
10-
"__DEV__": true
10+
"__DEV__": true,
11+
"__DELPOY_ENV__": true,
12+
"__GIT_SHA__": true,
13+
"__BRANCH__": true
1114
},
1215
"settings": {
1316
"import/resolver": {

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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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__);

frontend/src/globals.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ declare let __LOCALE__: string;
33
declare let __API_BASE__: string;
44
declare let __STRIPE_PK__: string;
55
declare let __RECAPTCHA_PK__: string;
6+
declare let __DEPLOY_ENV__:
7+
| 'production'
8+
| 'deploy-preview'
9+
| 'branch-deploy'
10+
| 'dev';
11+
declare let __GIT_SHA__: string;
12+
declare let __BRANCH__: string;
613

714
interface Window {
815
dataLayer?: Record<string, unknown>[];

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;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createBrowserHistory } from 'history';
2+
import { attachRumToHistory } from '../../datadog';
23

34
const history = createBrowserHistory();
45

6+
attachRumToHistory(history);
7+
58
export default history;

frontend/webpack.prod.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const webpack = require('webpack');
33
const merge = require('webpack-merge');
44
const common = require('./webpack.common.js');
55
const HtmlWebpackPlugin = require('html-webpack-plugin');
6-
const SentryWebpackPlugin = require("@sentry/webpack-plugin");
6+
const SentryWebpackPlugin = require('@sentry/webpack-plugin');
77

88
const path = require('path');
99

@@ -22,6 +22,9 @@ module.exports = merge(common, {
2222
__API_BASE__: JSON.stringify(process.env.API_BASE),
2323
__STRIPE_PK__: JSON.stringify(process.env.STRIPE_PK),
2424
__RECAPTCHA_PK__: JSON.stringify(process.env.RECAPTCHA_PK),
25+
__DEPLOY_ENV__: JSON.stringify(process.env.CONTEXT),
26+
__GIT_SHA__: JSON.stringify(process.env.COMMIT_REF),
27+
__BRANCH__: JSON.stringify(process.env.BRANCH),
2528
}),
2629
new HtmlWebpackPlugin({
2730
template: path.join(path.resolve(__dirname, 'src'), 'app.html'),
@@ -32,8 +35,8 @@ module.exports = merge(common, {
3235
},
3336
}),
3437
new SentryWebpackPlugin({
35-
org: "julian-boilen",
36-
project: "fourtiesnyc-frontend",
38+
org: 'julian-boilen',
39+
project: 'fourtiesnyc-frontend',
3740

3841
// Specify the directory containing build artifacts
3942
include: path.resolve(__dirname, 'dist'),

0 commit comments

Comments
 (0)