Skip to content

Commit fb56978

Browse files
authored
feat(tracing): Add beforeNavigate option (#2691)
Adds a beforeNavigate option to the Tracing integration. It allows for users of the integration to change the transaction name before a navigation/pageload transaction has been created. Currently it defaults to setting the transaction name to `location.pathname` (changed from `location.href` because we don't want to include query params).
1 parent eb4846e commit fb56978

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
- [react] ref: Refactor Profiler to account for update and render (#2677)
1616
- [apm] feat: Add ability to get span from activity using `getActivitySpan` (#2677)
1717
- [apm] fix: Check if `performance.mark` exists before calling it (#2680)
18+
- [tracing] feat: Add `beforeNavigate` option (#2691)
19+
- [tracing] ref: Create navigation transactions using `window.location.pathname` instead of `window.location.href` (#2691)
1820

1921
## 5.17.0
2022

packages/apm/src/integrations/tracing.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ export interface TracingOptions {
107107
writeAsBreadcrumbs: boolean;
108108
spanDebugTimingInfo: boolean;
109109
};
110+
111+
/**
112+
* beforeNavigate is called before a pageload/navigation transaction is created and allows for users
113+
* to set a custom navigation transaction name based on the current `window.location`. Defaults to returning
114+
* `window.location.pathname`.
115+
*
116+
* @param location the current location before navigation span is created
117+
*/
118+
beforeNavigate(location: Location): string;
110119
}
111120

112121
/** JSDoc */
@@ -177,6 +186,9 @@ export class Tracing implements Integration {
177186
Tracing._trackLCP();
178187
}
179188
const defaults = {
189+
beforeNavigate(location: Location): string {
190+
return location.pathname;
191+
},
180192
debug: {
181193
spanDebugTimingInfo: false,
182194
writeAsBreadcrumbs: false,
@@ -221,10 +233,9 @@ export class Tracing implements Integration {
221233
}
222234

223235
// Starting pageload transaction
224-
if (global.location && global.location.href && Tracing.options && Tracing.options.startTransactionOnPageLoad) {
225-
// Use `${global.location.href}` as transaction name
236+
if (global.location && Tracing.options && Tracing.options.startTransactionOnPageLoad) {
226237
Tracing.startIdleTransaction({
227-
name: global.location.href,
238+
name: Tracing.options.beforeNavigate(window.location),
228239
op: 'pageload',
229240
});
230241
}
@@ -997,7 +1008,7 @@ function historyCallback(_: { [key: string]: any }): void {
9971008
if (Tracing.options.startTransactionOnLocationChange && global && global.location) {
9981009
Tracing.finishIdleTransaction(timestampWithMs());
9991010
Tracing.startIdleTransaction({
1000-
name: global.location.href,
1011+
name: Tracing.options.beforeNavigate(window.location),
10011012
op: 'navigation',
10021013
});
10031014
}

0 commit comments

Comments
 (0)