|
@@ -0,0 +1,163 @@
|
|
1
|
+/* global _paq */
|
|
2
|
+
|
|
3
|
+import { getJitsiMeetGlobalNS } from '../../base/util';
|
|
4
|
+
|
|
5
|
+import AbstractHandler from './AbstractHandler';
|
|
6
|
+
|
|
7
|
+/**
|
|
8
|
+ * Analytics handler for Matomo.
|
|
9
|
+ */
|
|
10
|
+class MatomoHandler extends AbstractHandler {
|
|
11
|
+
|
|
12
|
+ /**
|
|
13
|
+ * Creates new instance of the Matomo handler.
|
|
14
|
+ *
|
|
15
|
+ * @param {Object} options -
|
|
16
|
+ * @param {string} options.matomoEndpoint - The Matomo endpoint.
|
|
17
|
+ * @param {string} options.matomoSiteID - The site ID.
|
|
18
|
+ */
|
|
19
|
+ constructor(options) {
|
|
20
|
+ super(options);
|
|
21
|
+ this._userProperties = {};
|
|
22
|
+
|
|
23
|
+ if (!options.matomoEndpoint) {
|
|
24
|
+ throw new Error(
|
|
25
|
+ 'Failed to initialize Matomo handler: no endpoint defined.'
|
|
26
|
+ );
|
|
27
|
+ }
|
|
28
|
+ if (!options.matomoSiteID) {
|
|
29
|
+ throw new Error(
|
|
30
|
+ 'Failed to initialize Matomo handler: no site ID defined.'
|
|
31
|
+ );
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ this._enabled = true;
|
|
35
|
+ this._initMatomo(options);
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ /**
|
|
39
|
+ * Initializes the _paq object.
|
|
40
|
+ *
|
|
41
|
+ * @param {Object} options -
|
|
42
|
+ * @param {string} options.matomoEndpoint - The Matomo endpoint.
|
|
43
|
+ * @param {string} options.matomoSiteID - The site ID.
|
|
44
|
+ * @returns {void}
|
|
45
|
+ */
|
|
46
|
+ _initMatomo(options) {
|
|
47
|
+ const _paq = window._paq || [];
|
|
48
|
+
|
|
49
|
+ window._paq = _paq;
|
|
50
|
+
|
|
51
|
+ _paq.push([ 'trackPageView' ]);
|
|
52
|
+ _paq.push([ 'enableLinkTracking' ]);
|
|
53
|
+
|
|
54
|
+ (function() {
|
|
55
|
+ // add trailing slash if needed
|
|
56
|
+ const u = options.matomoEndpoint.endsWith('/')
|
|
57
|
+ ? options.matomoEndpoint
|
|
58
|
+ : `${options.matomoEndpoint}/`;
|
|
59
|
+
|
|
60
|
+ // configure the tracker
|
|
61
|
+ _paq.push([ 'setTrackerUrl', `${u}matomo.php` ]);
|
|
62
|
+ _paq.push([ 'setSiteId', options.matomoSiteID ]);
|
|
63
|
+
|
|
64
|
+ // insert the matomo script
|
|
65
|
+ const d = document,
|
|
66
|
+ g = d.createElement('script'),
|
|
67
|
+ s = d.getElementsByTagName('script')[0];
|
|
68
|
+
|
|
69
|
+ g.type = 'text/javascript';
|
|
70
|
+ g.async = true;
|
|
71
|
+ g.defer = true;
|
|
72
|
+ g.src = `${u}matomo.js`;
|
|
73
|
+ s.parentNode.insertBefore(g, s);
|
|
74
|
+ })();
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ /**
|
|
78
|
+ * Extracts the integer to use for a Matomo event's value field
|
|
79
|
+ * from a lib-jitsi-meet analytics event.
|
|
80
|
+ *
|
|
81
|
+ * @param {Object} event - The lib-jitsi-meet analytics event.
|
|
82
|
+ * @returns {number} - The integer to use for the 'value' of a Matomo
|
|
83
|
+ * event, or NaN if the lib-jitsi-meet event doesn't contain a
|
|
84
|
+ * suitable value.
|
|
85
|
+ * @private
|
|
86
|
+ */
|
|
87
|
+ _extractValue(event) {
|
|
88
|
+ const value = event && event.attributes && event.attributes.value;
|
|
89
|
+
|
|
90
|
+ // Try to extract an integer from the 'value' attribute.
|
|
91
|
+ return Math.round(parseFloat(value));
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ /**
|
|
95
|
+ * Sets the permanent properties for the current session.
|
|
96
|
+ *
|
|
97
|
+ * @param {Object} userProps - The permanent properties.
|
|
98
|
+ * @returns {void}
|
|
99
|
+ */
|
|
100
|
+ setUserProperties(userProps = {}) {
|
|
101
|
+ if (!this._enabled) {
|
|
102
|
+ return;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ const visitScope = [ 'user_agent', 'callstats_name', 'browser_name' ];
|
|
106
|
+
|
|
107
|
+ // add variables in the 'page' scope
|
|
108
|
+ Object.keys(userProps)
|
|
109
|
+ .filter(key => visitScope.indexOf(key) === -1)
|
|
110
|
+ .forEach((key, index) => {
|
|
111
|
+ _paq.push([
|
|
112
|
+ 'setCustomVariable',
|
|
113
|
+ 1 + index,
|
|
114
|
+ key,
|
|
115
|
+ userProps[key],
|
|
116
|
+ 'page'
|
|
117
|
+ ]);
|
|
118
|
+ });
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+ // add variables in the 'visit' scope
|
|
122
|
+ Object.keys(userProps)
|
|
123
|
+ .filter(key => visitScope.indexOf(key) !== -1)
|
|
124
|
+ .forEach((key, index) => {
|
|
125
|
+ _paq.push([
|
|
126
|
+ 'setCustomVariable',
|
|
127
|
+ 1 + index,
|
|
128
|
+ key,
|
|
129
|
+ userProps[key],
|
|
130
|
+ 'visit'
|
|
131
|
+ ]);
|
|
132
|
+ });
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ /**
|
|
136
|
+ * This is the entry point of the API. The function sends an event to
|
|
137
|
+ * the Matomo endpoint. The format of the event is described in
|
|
138
|
+ * analyticsAdapter in lib-jitsi-meet.
|
|
139
|
+ *
|
|
140
|
+ * @param {Object} event - The event in the format specified by
|
|
141
|
+ * lib-jitsi-meet.
|
|
142
|
+ * @returns {void}
|
|
143
|
+ */
|
|
144
|
+ sendEvent(event) {
|
|
145
|
+ if (this._shouldIgnore(event)) {
|
|
146
|
+ return;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ const value = this._extractValue(event);
|
|
150
|
+ const matomoEvent = [ 'trackEvent', 'jitsi-meet', this._extractName(event) ];
|
|
151
|
+
|
|
152
|
+ if (!isNaN(value)) {
|
|
153
|
+ matomoEvent.push(value);
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ _paq.push(matomoEvent);
|
|
157
|
+ }
|
|
158
|
+}
|
|
159
|
+
|
|
160
|
+const globalNS = getJitsiMeetGlobalNS();
|
|
161
|
+
|
|
162
|
+globalNS.analyticsHandlers = globalNS.analyticsHandlers || [];
|
|
163
|
+globalNS.analyticsHandlers.push(MatomoHandler);
|