|
@@ -0,0 +1,128 @@
|
|
1
|
+/* global JitsiMeetJS, config, APP */
|
|
2
|
+/**
|
|
3
|
+ * Load the integration of a third-party analytics API such as Google
|
|
4
|
+ * Analytics. Since we cannot guarantee the quality of the third-party service
|
|
5
|
+ * (e.g. their server may take noticeably long time to respond), it is in our
|
|
6
|
+ * best interest (in the sense that the intergration of the analytics API is
|
|
7
|
+ * important to us but not enough to allow it to prevent people from joining
|
|
8
|
+ * a conference) to download the API asynchronously. Additionally, Google
|
|
9
|
+ * Analytics will download its implementation asynchronously anyway so it makes
|
|
10
|
+ * sense to append the loading on our side rather than prepend it.
|
|
11
|
+ * @param {string} url the url to be loaded
|
|
12
|
+ * @returns {Promise} resolved with no arguments when the script is loaded and
|
|
13
|
+ * rejected with the error from JitsiMeetJS.ScriptUtil.loadScript method
|
|
14
|
+ */
|
|
15
|
+function loadScript(url) {
|
|
16
|
+ return new Promise((resolve, reject) =>
|
|
17
|
+ JitsiMeetJS.util.ScriptUtil.loadScript(
|
|
18
|
+ url,
|
|
19
|
+ /* async */ true,
|
|
20
|
+ /* prepend */ false,
|
|
21
|
+ /* relativeURL */ false,
|
|
22
|
+ /* loadCallback */ () => resolve(),
|
|
23
|
+ /* errorCallback */ error => reject(error)));
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+/**
|
|
27
|
+ * Handles the initialization of analytics.
|
|
28
|
+ */
|
|
29
|
+class Analytics {
|
|
30
|
+ constructor() {
|
|
31
|
+ this._scriptURLs = Array.isArray(config.analyticsScriptUrls)
|
|
32
|
+ ? config.analyticsScriptUrls : [];
|
|
33
|
+ this._enabled = !!this._scriptURLs.length
|
|
34
|
+ && !config.disableThirdPartyRequests;
|
|
35
|
+ window.analyticsHandlers = [];
|
|
36
|
+ const machineId = JitsiMeetJS.getMachineId();
|
|
37
|
+ this._handlerConstructorOptions = {
|
|
38
|
+ product: "lib-jitsi-meet",
|
|
39
|
+ version: JitsiMeetJS.version,
|
|
40
|
+ session: machineId,
|
|
41
|
+ user: "uid-" + machineId
|
|
42
|
+ };
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ /**
|
|
46
|
+ * Returns whether analytics is enabled or not.
|
|
47
|
+ * @returns {boolean} whether analytics is enabled or not.
|
|
48
|
+ */
|
|
49
|
+ isEnabled() {
|
|
50
|
+ return this._enabled;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ /**
|
|
54
|
+ * Tries to load the scripts for the analytics handlers.
|
|
55
|
+ * @returns {Promise} resolves with the handlers that have been
|
|
56
|
+ * successfully loaded and rejects if there are no handlers loaded or the
|
|
57
|
+ * analytics is disabled.
|
|
58
|
+ */
|
|
59
|
+ _loadHandlers() {
|
|
60
|
+ if(!this.isEnabled()) {
|
|
61
|
+ return Promise.reject(new Error("Analytics is disabled!"));
|
|
62
|
+ }
|
|
63
|
+ let handlersPromises = [];
|
|
64
|
+ this._scriptURLs.forEach(url =>
|
|
65
|
+ handlersPromises.push(
|
|
66
|
+ loadScript(url).then(
|
|
67
|
+ () => {
|
|
68
|
+ return {type: "success"};
|
|
69
|
+ },
|
|
70
|
+ error => {
|
|
71
|
+ return {type: "error", error, url};
|
|
72
|
+ }))
|
|
73
|
+ );
|
|
74
|
+ return new Promise((resolve, reject) =>
|
|
75
|
+ {
|
|
76
|
+ Promise.all(handlersPromises).then(values => {
|
|
77
|
+ values.forEach(el => {
|
|
78
|
+ if(el.type === "error") {
|
|
79
|
+ console.log("Fialed to load " + el.url);
|
|
80
|
+ console.error(el.error);
|
|
81
|
+ }
|
|
82
|
+ });
|
|
83
|
+
|
|
84
|
+ if(window.analyticsHandlers.length === 0) {
|
|
85
|
+ reject(new Error("No analytics handlers available"));
|
|
86
|
+ } else {
|
|
87
|
+ let handlerInstances = [];
|
|
88
|
+ window.analyticsHandlers.forEach(
|
|
89
|
+ Handler => handlerInstances.push(
|
|
90
|
+ new Handler(this._handlerConstructorOptions)));
|
|
91
|
+ resolve(handlerInstances);
|
|
92
|
+ }
|
|
93
|
+ });
|
|
94
|
+ });
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ /**
|
|
98
|
+ * Loads the analytics scripts and inits JitsiMeetJS.analytics by setting
|
|
99
|
+ * permanent properties and setting the handlers from the loaded scripts.
|
|
100
|
+ * NOTE: Has to be used after JitsiMeetJS.init. Otherwise analytics will be
|
|
101
|
+ * null.
|
|
102
|
+ */
|
|
103
|
+ init() {
|
|
104
|
+ let analytics = JitsiMeetJS.analytics;
|
|
105
|
+ if(!this.isEnabled() || !analytics)
|
|
106
|
+ return;
|
|
107
|
+
|
|
108
|
+ this._loadHandlers()
|
|
109
|
+ .then(handlers => {
|
|
110
|
+ let permanentProperties = {
|
|
111
|
+ userAgent: navigator.userAgent,
|
|
112
|
+ roomName: APP.conference.roomName
|
|
113
|
+ };
|
|
114
|
+ let {server, group} = APP.tokenData;
|
|
115
|
+ if(server) {
|
|
116
|
+ permanentProperties.server = server;
|
|
117
|
+ }
|
|
118
|
+ if(group) {
|
|
119
|
+ permanentProperties.group = group;
|
|
120
|
+ }
|
|
121
|
+ analytics.addPermanentProperties(permanentProperties);
|
|
122
|
+ analytics.setAnalyticsHandlers(handlers);
|
|
123
|
+ }, error => analytics.dispose() && console.error(error));
|
|
124
|
+
|
|
125
|
+ }
|
|
126
|
+}
|
|
127
|
+
|
|
128
|
+export default new Analytics();
|