@@ -33,22 +33,27 @@ import { SharedState } from '../state';
33
33
import { localStorageAccessible } from '../detectors' ;
34
34
import { LOG , Payload } from '@snowplow/tracker-core' ;
35
35
import { PAYLOAD_DATA_SCHEMA } from './schemata' ;
36
+ import { EventMethod } from './types' ;
36
37
37
38
export interface OutQueue {
38
39
enqueueRequest : ( request : Payload , url : string ) => void ;
39
40
executeQueue : ( ) => void ;
40
41
setUseLocalStorage : ( localStorage : boolean ) => void ;
41
42
setAnonymousTracking : ( anonymous : boolean ) => void ;
42
43
setCollectorUrl : ( url : string ) => void ;
43
- setBufferSize : ( bufferSize : number ) => void ;
44
+ setBufferSize : ( newBufferSize : number ) => void ;
45
+ /**
46
+ * Returns the currently used queue name or if the `eventMethod` argument is provided, the queue name for the eventMethod.
47
+ */
48
+ getName : ( eventMethod ?: Exclude < EventMethod , 'beacon' > ) => string ;
44
49
}
45
50
46
51
/**
47
52
* Object handling sending events to a collector.
48
53
* Instantiated once per tracker instance.
49
54
*
50
55
* @param id - The Snowplow function name (used to generate the localStorage key)
51
- * @param sharedSate - Stores reference to the outbound queue so it can unload the page when all queues are empty
56
+ * @param sharedState - Stores reference to the outbound queue so it can unload the page when all queues are empty
52
57
* @param useLocalStorage - Whether to use localStorage at all
53
58
* @param eventMethod - if null will use 'beacon' otherwise can be set to 'post', 'get', or 'beacon' to force.
54
59
* @param postPath - The path where events are to be posted
@@ -67,7 +72,7 @@ export interface OutQueue {
67
72
*/
68
73
export function OutQueueManager (
69
74
id : string ,
70
- sharedSate : SharedState ,
75
+ sharedState : SharedState ,
71
76
useLocalStorage : boolean ,
72
77
eventMethod : string | boolean ,
73
78
postPath : string ,
@@ -114,7 +119,7 @@ export function OutQueueManager(
114
119
// Resolve all options and capabilities and decide path
115
120
path = usePost ? postPath : '/i' ,
116
121
// Different queue names for GET and POST since they are stored differently
117
- queueName = `snowplowOutQueue_ ${ id } _ ${ usePost ? 'post2 ' : 'get' } ` ;
122
+ queueName = getQueueName ( id , usePost ? 'post ' : 'get' ) ;
118
123
119
124
// Ensure we don't set headers when beacon is the requested eventMethod as we might fallback to POST
120
125
// and end up sending them in older browsers which don't support beacon leading to inconsistencies
@@ -128,7 +133,9 @@ export function OutQueueManager(
128
133
try {
129
134
const localStorageQueue = window . localStorage . getItem ( queueName ) ;
130
135
outQueue = localStorageQueue ? JSON . parse ( localStorageQueue ) : [ ] ;
131
- } catch ( e ) { }
136
+ } catch ( e ) {
137
+ LOG . error ( 'Failed to access window.localStorage queue.' ) ;
138
+ }
132
139
}
133
140
134
141
// Initialize to and empty array if we didn't get anything out of localStorage
@@ -137,16 +144,20 @@ export function OutQueueManager(
137
144
}
138
145
139
146
// Used by pageUnloadGuard
140
- sharedSate . outQueues . push ( outQueue ) ;
147
+ sharedState . outQueues . push ( outQueue ) ;
141
148
142
149
if ( useXhr && bufferSize > 1 ) {
143
- sharedSate . bufferFlushers . push ( function ( sync ) {
150
+ sharedState . bufferFlushers . push ( function ( sync ) {
144
151
if ( ! executingQueue ) {
145
152
executeQueue ( sync ) ;
146
153
}
147
154
} ) ;
148
155
}
149
156
157
+ function getQueueName ( id : string , method : Exclude < EventMethod , 'beacon' > ) {
158
+ return `snowplowOutQueue_${ id } _${ method === 'get' ? 'get' : 'post2' } ` ;
159
+ }
160
+
150
161
/*
151
162
* Convert a dictionary to a querystring
152
163
* The context field is the last in the querystring
@@ -535,6 +546,7 @@ export function OutQueueManager(
535
546
setBufferSize : ( newBufferSize : number ) => {
536
547
bufferSize = newBufferSize ;
537
548
} ,
549
+ getName : ( method ?: Exclude < EventMethod , 'beacon' > ) => ( method ? getQueueName ( id , method ) : queueName ) ,
538
550
} ;
539
551
540
552
function hasWebKitBeaconBug ( useragent : string ) {
0 commit comments