@@ -2,13 +2,48 @@ import * as Sentry from "@sentry/nextjs"
2
2
3
3
const environment = process . env . NEXT_PUBLIC_CONTEXT || "development"
4
4
5
- if ( environment === "production" ) {
6
- Sentry . init ( {
7
- dsn : process . env . NEXT_PUBLIC_SENTRY_DSN ,
8
- tracesSampleRate : 0.1 ,
9
- debug : false ,
10
- environment,
11
- } )
5
+ /**
6
+ * Finds the closest element (including the element itself) that has an id attribute
7
+ * @param element - The starting element to search from
8
+ * @param maxDepth - Maximum number of parent levels to search (default: 3)
9
+ * @returns The first found attribute value in priority order, null otherwise
10
+ */
11
+ function findClosestElementId (
12
+ element : Element | null | undefined ,
13
+ maxDepth : number = 3
14
+ ) : string | null {
15
+ if ( ! element || maxDepth < 0 ) return null
16
+
17
+ const sentryId = element . getAttribute ( "data-testid" )
18
+ if ( sentryId ) return sentryId
19
+
20
+ const ariaLabel = element . getAttribute ( "aria-label" )
21
+ if ( ariaLabel ) return ariaLabel
22
+
23
+ const id = element . getAttribute ( "id" )
24
+ if ( id ) return id
25
+
26
+ // Recursively check parent elements up to maxDepth
27
+ return findClosestElementId ( element . parentElement , maxDepth - 1 )
12
28
}
13
29
30
+ Sentry . init ( {
31
+ dsn : process . env . NEXT_PUBLIC_SENTRY_DSN ,
32
+ tracesSampleRate : 0.1 ,
33
+ environment,
34
+ enabled : environment === "production" ,
35
+ beforeBreadcrumb ( breadcrumb , hint ) {
36
+ if ( breadcrumb . category === "ui.click" ) {
37
+ const element = hint ?. event ?. target
38
+
39
+ const id = findClosestElementId ( element )
40
+ if ( id ) {
41
+ breadcrumb . message = id + " (" + breadcrumb . message + ")"
42
+ }
43
+ }
44
+
45
+ return breadcrumb
46
+ } ,
47
+ } )
48
+
14
49
export const onRouterTransitionStart = Sentry . captureRouterTransitionStart
0 commit comments