@@ -2,15 +2,13 @@ import { EventProcessor, Hub, Integration, Scope, Span, SpanContext } from '@sen
2
2
3
3
/** JSDoc */
4
4
interface TransactionActivityOptions {
5
- // onLocationChange: (info) => {
6
- // // info holds the location change api.
7
- // // if this returns `null` there is no transaction started.
8
- // return info.state.transaction || info.url;
9
- // },
10
- // onActivity?: (info) => {
11
- // return info.type !== 'xhr' || !info.url.match(/zendesk/);
12
- // },
13
5
idleTimeout : number ;
6
+ patchHistory : boolean ;
7
+ /**
8
+ * Called when an history change happend
9
+ */
10
+ onLocationChange ( state : any ) : string ;
11
+ startTransactionOnLocationChange : boolean ;
14
12
}
15
13
16
14
/** JSDoc */
@@ -53,6 +51,9 @@ export class TransactionActivity implements Integration {
53
51
public constructor (
54
52
public readonly _options : TransactionActivityOptions = {
55
53
idleTimeout : 500 ,
54
+ onLocationChange : ( ) => window . location . href ,
55
+ patchHistory : true ,
56
+ startTransactionOnLocationChange : true ,
56
57
} ,
57
58
) {
58
59
TransactionActivity . _options = _options ;
@@ -63,6 +64,31 @@ export class TransactionActivity implements Integration {
63
64
*/
64
65
public setupOnce ( _ : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
65
66
TransactionActivity . _getCurrentHub = getCurrentHub ;
67
+ if ( this . _options . patchHistory ) {
68
+ // tslint:disable: no-unsafe-any
69
+ // tslint:disable-next-line: typedef only-arrow-functions
70
+ ( function ( history : any ) {
71
+ const pushState = history . pushState ;
72
+ // tslint:disable-next-line: typedef only-arrow-functions
73
+ history . pushState = function ( state : any ) {
74
+ if ( typeof history . onpushstate === 'function' ) {
75
+ history . onpushstate ( { state } ) ;
76
+ }
77
+ // ... whatever else you want to do
78
+ // maybe call onhashchange e.handler
79
+ return pushState . apply ( history , arguments ) ;
80
+ } ;
81
+ } ) ( window . history ) ;
82
+ window . onpopstate = ( history as any ) . onpushstate = ( _state : any ) => {
83
+ if ( this . _options . startTransactionOnLocationChange ) {
84
+ TransactionActivity . startIdleTransaction ( `${ window . location . href } ` , {
85
+ op : 'navigation' ,
86
+ sampled : true ,
87
+ } ) ;
88
+ }
89
+ } ;
90
+ // tslint:enable: no-unsafe-any
91
+ }
66
92
}
67
93
68
94
/**
@@ -124,6 +150,14 @@ export class TransactionActivity implements Integration {
124
150
( activeTransaction as any ) . transaction = name ;
125
151
}
126
152
153
+ public static finishIdleTransaction ( ) : void {
154
+ const active = TransactionActivity . _activeTransaction ;
155
+ if ( active ) {
156
+ // true = use timestamp of last span
157
+ active . finish ( true ) ;
158
+ }
159
+ }
160
+
127
161
/**
128
162
* Starts tracking for a specifc activity
129
163
*/
@@ -165,11 +199,8 @@ export class TransactionActivity implements Integration {
165
199
if ( count === 0 ) {
166
200
const timeout = TransactionActivity . _options && TransactionActivity . _options . idleTimeout ;
167
201
TransactionActivity . _debounce = ( setTimeout ( ( ) => {
168
- const active = TransactionActivity . _activeTransaction ;
169
- if ( active ) {
170
- active . finish ( true ) ;
171
- }
172
- } , timeout ) as any ) as number ; // TODO 500
202
+ TransactionActivity . finishIdleTransaction ( ) ;
203
+ } , timeout ) as any ) as number ;
173
204
}
174
205
}
175
206
}
0 commit comments