Skip to content

Commit

Permalink
feat(tracing): Add beforeNavigate option (#2691)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
AbhiPrasad committed Jun 22, 2020
1 parent eb4846e commit fb56978
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,8 @@
- [react] ref: Refactor Profiler to account for update and render (#2677)
- [apm] feat: Add ability to get span from activity using `getActivitySpan` (#2677)
- [apm] fix: Check if `performance.mark` exists before calling it (#2680)
- [tracing] feat: Add `beforeNavigate` option (#2691)
- [tracing] ref: Create navigation transactions using `window.location.pathname` instead of `window.location.href` (#2691)

## 5.17.0

Expand Down
19 changes: 15 additions & 4 deletions packages/apm/src/integrations/tracing.ts
Expand Up @@ -107,6 +107,15 @@ export interface TracingOptions {
writeAsBreadcrumbs: boolean;
spanDebugTimingInfo: boolean;
};

/**
* beforeNavigate is called before a pageload/navigation transaction is created and allows for users
* to set a custom navigation transaction name based on the current `window.location`. Defaults to returning
* `window.location.pathname`.
*
* @param location the current location before navigation span is created
*/
beforeNavigate(location: Location): string;
}

/** JSDoc */
Expand Down Expand Up @@ -177,6 +186,9 @@ export class Tracing implements Integration {
Tracing._trackLCP();
}
const defaults = {
beforeNavigate(location: Location): string {
return location.pathname;
},
debug: {
spanDebugTimingInfo: false,
writeAsBreadcrumbs: false,
Expand Down Expand Up @@ -221,10 +233,9 @@ export class Tracing implements Integration {
}

// Starting pageload transaction
if (global.location && global.location.href && Tracing.options && Tracing.options.startTransactionOnPageLoad) {
// Use `${global.location.href}` as transaction name
if (global.location && Tracing.options && Tracing.options.startTransactionOnPageLoad) {
Tracing.startIdleTransaction({
name: global.location.href,
name: Tracing.options.beforeNavigate(window.location),
op: 'pageload',
});
}
Expand Down Expand Up @@ -997,7 +1008,7 @@ function historyCallback(_: { [key: string]: any }): void {
if (Tracing.options.startTransactionOnLocationChange && global && global.location) {
Tracing.finishIdleTransaction(timestampWithMs());
Tracing.startIdleTransaction({
name: global.location.href,
name: Tracing.options.beforeNavigate(window.location),
op: 'navigation',
});
}
Expand Down

0 comments on commit fb56978

Please sign in to comment.