@@ -26,19 +26,6 @@ import { GtagCommand } from './constants';
26
26
import { AnalyticsError } from './errors' ;
27
27
import { logger } from './logger' ;
28
28
29
- async function handleInitializationPromiseErrors (
30
- initializationPromise : Promise < string >
31
- ) : Promise < string | null > {
32
- try {
33
- return await initializationPromise ;
34
- } catch ( e ) {
35
- if ( e . message . includes ( AnalyticsError . INVALID_ANALYTICS_CONTEXT ) ) {
36
- logger . warn ( e . message ) ;
37
- return null ;
38
- }
39
- }
40
- return null ;
41
- }
42
29
/**
43
30
* Logs an analytics event through the Firebase SDK.
44
31
*
@@ -53,21 +40,24 @@ export async function logEvent(
53
40
eventParams ?: EventParams ,
54
41
options ?: AnalyticsCallOptions
55
42
) : Promise < void > {
56
- if ( options && options . global ) {
57
- gtagFunction ( GtagCommand . EVENT , eventName , eventParams ) ;
58
- return ;
59
- } else {
60
- const measurementId = await handleInitializationPromiseErrors (
61
- initializationPromise
62
- ) ;
63
- if ( measurementId == null ) {
43
+ try {
44
+ if ( options && options . global ) {
45
+ gtagFunction ( GtagCommand . EVENT , eventName , eventParams ) ;
64
46
return ;
47
+ } else {
48
+ const measurementId = await initializationPromise ;
49
+ const params : EventParams | ControlParams = {
50
+ ...eventParams ,
51
+ 'send_to' : measurementId
52
+ } ;
53
+ gtagFunction ( GtagCommand . EVENT , eventName , params ) ;
54
+ }
55
+ } catch ( e ) {
56
+ if ( e . message . includes ( AnalyticsError . INVALID_ANALYTICS_CONTEXT ) ) {
57
+ logger . warn ( e . message ) ;
58
+ } else {
59
+ throw e ;
65
60
}
66
- const params : EventParams | ControlParams = {
67
- ...eventParams ,
68
- 'send_to' : measurementId
69
- } ;
70
- gtagFunction ( GtagCommand . EVENT , eventName , params ) ;
71
61
}
72
62
}
73
63
@@ -83,20 +73,23 @@ export async function setCurrentScreen(
83
73
screenName : string | null ,
84
74
options ?: AnalyticsCallOptions
85
75
) : Promise < void > {
86
- if ( options && options . global ) {
87
- gtagFunction ( GtagCommand . SET , { 'screen_name' : screenName } ) ;
88
- return Promise . resolve ( ) ;
89
- } else {
90
- const measurementId = await handleInitializationPromiseErrors (
91
- initializationPromise
92
- ) ;
93
- if ( measurementId == null ) {
94
- return ;
76
+ try {
77
+ if ( options && options . global ) {
78
+ gtagFunction ( GtagCommand . SET , { 'screen_name' : screenName } ) ;
79
+ return Promise . resolve ( ) ;
80
+ } else {
81
+ const measurementId = await initializationPromise ;
82
+ gtagFunction ( GtagCommand . CONFIG , measurementId , {
83
+ update : true ,
84
+ 'screen_name' : screenName
85
+ } ) ;
86
+ }
87
+ } catch ( e ) {
88
+ if ( e . message . includes ( AnalyticsError . INVALID_ANALYTICS_CONTEXT ) ) {
89
+ logger . warn ( e . message ) ;
90
+ } else {
91
+ throw e ;
95
92
}
96
- gtagFunction ( GtagCommand . CONFIG , measurementId , {
97
- update : true ,
98
- 'screen_name' : screenName
99
- } ) ;
100
93
}
101
94
}
102
95
@@ -112,20 +105,23 @@ export async function setUserId(
112
105
id : string | null ,
113
106
options ?: AnalyticsCallOptions
114
107
) : Promise < void > {
115
- if ( options && options . global ) {
116
- gtagFunction ( GtagCommand . SET , { 'user_id' : id } ) ;
117
- return Promise . resolve ( ) ;
118
- } else {
119
- const measurementId = await handleInitializationPromiseErrors (
120
- initializationPromise
121
- ) ;
122
- if ( measurementId == null ) {
123
- return ;
108
+ try {
109
+ if ( options && options . global ) {
110
+ gtagFunction ( GtagCommand . SET , { 'user_id' : id } ) ;
111
+ return Promise . resolve ( ) ;
112
+ } else {
113
+ const measurementId = await initializationPromise ;
114
+ gtagFunction ( GtagCommand . CONFIG , measurementId , {
115
+ update : true ,
116
+ 'user_id' : id
117
+ } ) ;
118
+ }
119
+ } catch ( e ) {
120
+ if ( e . message . includes ( AnalyticsError . INVALID_ANALYTICS_CONTEXT ) ) {
121
+ logger . warn ( e . message ) ;
122
+ } else {
123
+ throw e ;
124
124
}
125
- gtagFunction ( GtagCommand . CONFIG , measurementId , {
126
- update : true ,
127
- 'user_id' : id
128
- } ) ;
129
125
}
130
126
}
131
127
@@ -141,25 +137,28 @@ export async function setUserProperties(
141
137
properties : CustomParams ,
142
138
options ?: AnalyticsCallOptions
143
139
) : Promise < void > {
144
- if ( options && options . global ) {
145
- const flatProperties : { [ key : string ] : unknown } = { } ;
146
- for ( const key of Object . keys ( properties ) ) {
147
- // use dot notation for merge behavior in gtag.js
148
- flatProperties [ `user_properties.${ key } ` ] = properties [ key ] ;
140
+ try {
141
+ if ( options && options . global ) {
142
+ const flatProperties : { [ key : string ] : unknown } = { } ;
143
+ for ( const key of Object . keys ( properties ) ) {
144
+ // use dot notation for merge behavior in gtag.js
145
+ flatProperties [ `user_properties.${ key } ` ] = properties [ key ] ;
146
+ }
147
+ gtagFunction ( GtagCommand . SET , flatProperties ) ;
148
+ return Promise . resolve ( ) ;
149
+ } else {
150
+ const measurementId = await initializationPromise ;
151
+ gtagFunction ( GtagCommand . CONFIG , measurementId , {
152
+ update : true ,
153
+ 'user_properties' : properties
154
+ } ) ;
149
155
}
150
- gtagFunction ( GtagCommand . SET , flatProperties ) ;
151
- return Promise . resolve ( ) ;
152
- } else {
153
- const measurementId = await handleInitializationPromiseErrors (
154
- initializationPromise
155
- ) ;
156
- if ( measurementId == null ) {
157
- return ;
156
+ } catch ( e ) {
157
+ if ( e . message . includes ( AnalyticsError . INVALID_ANALYTICS_CONTEXT ) ) {
158
+ logger . warn ( e . message ) ;
159
+ } else {
160
+ throw e ;
158
161
}
159
- gtagFunction ( GtagCommand . CONFIG , measurementId , {
160
- update : true ,
161
- 'user_properties' : properties
162
- } ) ;
163
162
}
164
163
}
165
164
@@ -172,11 +171,14 @@ export async function setAnalyticsCollectionEnabled(
172
171
initializationPromise : Promise < string > ,
173
172
enabled : boolean
174
173
) : Promise < void > {
175
- const measurementId = await handleInitializationPromiseErrors (
176
- initializationPromise
177
- ) ;
178
- if ( measurementId == null ) {
179
- return ;
174
+ try {
175
+ const measurementId = await initializationPromise ;
176
+ window [ `ga-disable-${ measurementId } ` ] = ! enabled ;
177
+ } catch ( e ) {
178
+ if ( e . message . includes ( AnalyticsError . INVALID_ANALYTICS_CONTEXT ) ) {
179
+ logger . warn ( e . message ) ;
180
+ } else {
181
+ throw e ;
182
+ }
180
183
}
181
- window [ `ga-disable-${ measurementId } ` ] = ! enabled ;
182
184
}
0 commit comments