Browse Source

fix(analytics) make handler loading more resilient

- Don't initialize handler's is their API key is not set
- Don't swallow exceptions when creating handlers
- Don't remove all handlers if an external one fails
- Dispose the analytics subsystem if no handlers are registered
j8
Saúl Ibarra Corretgé 4 years ago
parent
commit
b153bf2fb8
1 changed files with 42 additions and 35 deletions
  1. 42
    35
      react/features/analytics/functions.js

+ 42
- 35
react/features/analytics/functions.js View File

@@ -57,12 +57,15 @@ export function resetAnalytics() {
57 57
  * @param {Store} store - The redux store in which the specified {@code action} is being dispatched.
58 58
  * @returns {Promise} Resolves with the handlers that have been successfully loaded.
59 59
  */
60
-export function createHandlers({ getState }: { getState: Function }) {
60
+export async function createHandlers({ getState }: { getState: Function }) {
61 61
     getJitsiMeetGlobalNS().analyticsHandlers = [];
62 62
     window.analyticsHandlers = []; // Legacy support.
63 63
 
64 64
     if (!isAnalyticsEnabled(getState)) {
65
-        return Promise.resolve([]);
65
+        // Avoid all analytics processing if there are no handlers, since no event would be sent.
66
+        analytics.dispose();
67
+
68
+        return [];
66 69
     }
67 70
 
68 71
     const state = getState();
@@ -100,43 +103,47 @@ export function createHandlers({ getState }: { getState: Function }) {
100 103
     };
101 104
     const handlers = [];
102 105
 
103
-    try {
104
-        const amplitude = new AmplitudeHandler(handlerConstructorOptions);
106
+    if (amplitudeAPPKey) {
107
+        try {
108
+            const amplitude = new AmplitudeHandler(handlerConstructorOptions);
105 109
 
106
-        analytics.amplitudeIdentityProps = amplitude.getIdentityProps();
110
+            analytics.amplitudeIdentityProps = amplitude.getIdentityProps();
111
+
112
+            handlers.push(amplitude);
113
+        } catch (e) {
114
+            logger.error('Failed to initialize Amplitude handler', e);
115
+        }
116
+    }
107 117
 
108
-        handlers.push(amplitude);
109
-    // eslint-disable-next-line no-empty
110
-    } catch (e) {}
118
+    if (matomoEndpoint && matomoSiteID) {
119
+        try {
120
+            const matomo = new MatomoHandler(handlerConstructorOptions);
111 121
 
112
-    try {
113
-        const matomo = new MatomoHandler(handlerConstructorOptions);
114
-
115
-        handlers.push(matomo);
116
-    // eslint-disable-next-line no-empty
117
-    } catch (e) {}
118
-
119
-    return (
120
-        _loadHandlers(scriptURLs, handlerConstructorOptions)
121
-            .then(externalHandlers => {
122
-                handlers.push(...externalHandlers);
123
-                if (handlers.length === 0) {
124
-                    // Throwing an error in order to dispose the analytics in the catch clause due to the lack of any
125
-                    // analytics handlers.
126
-                    throw new Error('No analytics handlers created!');
127
-                }
128
-
129
-                return handlers;
130
-            })
131
-            .catch(e => {
132
-                analytics.dispose();
133
-                if (handlers.length !== 0) {
134
-                    logger.error(e);
135
-                }
136
-
137
-                return [];
138
-            }));
122
+            handlers.push(matomo);
123
+        } catch (e) {
124
+            logger.error('Failed to initialize Matomo handler', e);
125
+        }
126
+    }
127
+
128
+    if (Array.isArray(scriptURLs) && scriptURLs.length > 0) {
129
+        let externalHandlers;
130
+
131
+        try {
132
+            externalHandlers = await _loadHandlers(scriptURLs, handlerConstructorOptions);
133
+            handlers.push(...externalHandlers);
134
+        } catch (e) {
135
+            logger.error('Failed to initialize external analytics handlers', e);
136
+        }
137
+    }
138
+
139
+    // Avoid all analytics processing if there are no handlers, since no event would be sent.
140
+    if (handlers.length === 0) {
141
+        analytics.dispose();
142
+    }
143
+
144
+    logger.info(`Initialized ${handlers.length} analytics handlers`);
139 145
 
146
+    return handlers;
140 147
 }
141 148
 
142 149
 /**

Loading…
Cancel
Save