@@ -35,6 +35,12 @@ import { DEBUG_BUILD } from '../debug-build';
35
35
import { WINDOW } from '../helpers' ;
36
36
import { registerBackgroundTabDetection } from './backgroundtab' ;
37
37
import { defaultRequestInstrumentationOptions , instrumentOutgoingRequests } from './request' ;
38
+ import type { PreviousTraceInfo } from './previousTrace' ;
39
+ import {
40
+ addPreviousTraceSpanLink ,
41
+ getPreviousTraceFromSessionStorage ,
42
+ storePreviousTraceInSessionStorage ,
43
+ } from './previousTrace' ;
38
44
39
45
export const BROWSER_TRACING_INTEGRATION_ID = 'BrowserTracing' ;
40
46
@@ -142,6 +148,25 @@ export interface BrowserTracingOptions {
142
148
*/
143
149
enableHTTPTimings : boolean ;
144
150
151
+ /**
152
+ * If enabled, previously started traces (e.g. pageload or navigation spans) will be linked
153
+ * to the current trace. This lets you navigate across traces within a user journey in the
154
+ * Sentry UI.
155
+ *
156
+ * Set `persistPreviousTrace` to `true` to connect traces across hard page reloads.
157
+ *
158
+ * @default true, this is turned on by default.
159
+ */
160
+ enablePreviousTrace ?: boolean ;
161
+
162
+ /**
163
+ * If set to true, the previous trace will be stored in `sessionStorage`, so that
164
+ * traces can be linked across hard page refreshes.
165
+ *
166
+ * @default false, by default, previous trace data is only stored in-memory.
167
+ */
168
+ persistPreviousTrace ?: boolean ;
169
+
145
170
/**
146
171
* _experiments allows the user to send options to define how this integration works.
147
172
*
@@ -175,6 +200,8 @@ const DEFAULT_BROWSER_TRACING_OPTIONS: BrowserTracingOptions = {
175
200
enableLongTask : true ,
176
201
enableLongAnimationFrame : true ,
177
202
enableInp : true ,
203
+ enablePreviousTrace : true ,
204
+ persistPreviousTrace : false ,
178
205
_experiments : { } ,
179
206
...defaultRequestInstrumentationOptions ,
180
207
} ;
@@ -214,6 +241,8 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
214
241
enableHTTPTimings,
215
242
instrumentPageLoad,
216
243
instrumentNavigation,
244
+ enablePreviousTrace,
245
+ persistPreviousTrace,
217
246
} = {
218
247
...DEFAULT_BROWSER_TRACING_OPTIONS ,
219
248
..._options ,
@@ -245,10 +274,19 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
245
274
source : undefined ,
246
275
} ;
247
276
277
+ let previousTraceInfo : PreviousTraceInfo | undefined ;
278
+ if ( enablePreviousTrace && persistPreviousTrace ) {
279
+ previousTraceInfo = getPreviousTraceFromSessionStorage ( ) ;
280
+ }
281
+
248
282
/** Create routing idle transaction. */
249
283
function _createRouteSpan ( client : Client , startSpanOptions : StartSpanOptions ) : void {
250
284
const isPageloadTransaction = startSpanOptions . op === 'pageload' ;
251
285
286
+ if ( enablePreviousTrace && previousTraceInfo ) {
287
+ previousTraceInfo = addPreviousTraceSpanLink ( previousTraceInfo , startSpanOptions ) ;
288
+ }
289
+
252
290
const finalStartSpanOptions : StartSpanOptions = beforeStartSpan
253
291
? beforeStartSpan ( startSpanOptions )
254
292
: startSpanOptions ;
@@ -292,6 +330,16 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
292
330
} ) ;
293
331
setActiveIdleSpan ( client , idleSpan ) ;
294
332
333
+ if ( enablePreviousTrace ) {
334
+ previousTraceInfo = {
335
+ spanContext : idleSpan . spanContext ( ) ,
336
+ startTimestamp : spanToJSON ( idleSpan ) . start_timestamp ,
337
+ } ;
338
+ if ( persistPreviousTrace ) {
339
+ storePreviousTraceInSessionStorage ( previousTraceInfo ) ;
340
+ }
341
+ }
342
+
295
343
function emitFinish ( ) : void {
296
344
if ( optionalWindowDocument && [ 'interactive' , 'complete' ] . includes ( optionalWindowDocument . readyState ) ) {
297
345
client . emit ( 'idleSpanEnableAutoFinish' , idleSpan ) ;
0 commit comments