2017-05-28 03:47:09 +08:00
|
|
|
/*
|
|
|
|
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 23:33:07 +08:00
|
|
|
import { getCurrentLanguage, _t, _td } from './languageHandler';
|
2017-05-30 02:04:37 +08:00
|
|
|
import PlatformPeg from './PlatformPeg';
|
2018-04-26 21:30:27 +08:00
|
|
|
import SdkConfig from './SdkConfig';
|
2017-11-19 23:33:07 +08:00
|
|
|
import Modal from './Modal';
|
|
|
|
import sdk from './index';
|
|
|
|
|
2018-04-25 22:06:45 +08:00
|
|
|
const hashRegex = /#\/(groups?|room|user|settings|register|login|forgot_password|home|directory)/;
|
2018-04-26 19:22:56 +08:00
|
|
|
const hashVarRegex = /#\/(group|room|user)\/.*$/;
|
2018-04-25 21:23:31 +08:00
|
|
|
|
|
|
|
// Remove all but the first item in the hash path. Redact unexpected hashes.
|
|
|
|
function getRedactedHash(hash) {
|
2018-04-26 20:05:59 +08:00
|
|
|
// Don't leak URLs we aren't expecting - they could contain tokens/PII
|
2018-04-25 21:23:31 +08:00
|
|
|
const match = hashRegex.exec(hash);
|
|
|
|
if (!match) {
|
|
|
|
console.warn(`Unexpected hash location "${hash}"`);
|
|
|
|
return '#/<unexpected hash location>';
|
|
|
|
}
|
|
|
|
|
2018-04-26 19:22:56 +08:00
|
|
|
if (hashVarRegex.test(hash)) {
|
|
|
|
return hash.replace(hashVarRegex, "#/$1/<redacted>");
|
|
|
|
}
|
|
|
|
|
2018-04-25 21:23:31 +08:00
|
|
|
return hash.replace(hashRegex, "#/$1");
|
2017-11-19 23:33:07 +08:00
|
|
|
}
|
2017-05-28 03:47:09 +08:00
|
|
|
|
2018-06-27 00:06:12 +08:00
|
|
|
// Return the current origin, path and hash separated with a `/`. This does
|
|
|
|
// not include query parameters.
|
2017-06-04 04:26:22 +08:00
|
|
|
function getRedactedUrl() {
|
2018-06-27 17:31:51 +08:00
|
|
|
const { origin, hash } = window.location;
|
|
|
|
let { pathname } = window.location;
|
2018-06-27 00:06:12 +08:00
|
|
|
|
|
|
|
// Redact paths which could contain unexpected PII
|
|
|
|
if (origin.startsWith('file://')) {
|
|
|
|
pathname = "/<redacted>/";
|
|
|
|
}
|
|
|
|
|
2018-04-25 23:21:30 +08:00
|
|
|
return origin + pathname + getRedactedHash(hash);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 02:04:37 +08:00
|
|
|
const customVariables = {
|
2017-11-19 23:33:07 +08:00
|
|
|
'App Platform': {
|
|
|
|
id: 1,
|
|
|
|
expl: _td('The platform you\'re on'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: 'Electron Platform',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
|
|
|
'App Version': {
|
|
|
|
id: 2,
|
|
|
|
expl: _td('The version of Riot.im'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: '15.0.0',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
|
|
|
'User Type': {
|
|
|
|
id: 3,
|
2019-01-26 04:25:29 +08:00
|
|
|
expl: _td('Whether or not you\'re logged in (we don\'t record your username)'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: 'Logged In',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
|
|
|
'Chosen Language': {
|
|
|
|
id: 4,
|
|
|
|
expl: _td('Your language of choice'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: 'en',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
|
|
|
'Instance': {
|
|
|
|
id: 5,
|
|
|
|
expl: _td('Which officially provided instance you are using, if any'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: 'app',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
|
|
|
'RTE: Uses Richtext Mode': {
|
|
|
|
id: 6,
|
|
|
|
expl: _td('Whether or not you\'re using the Richtext mode of the Rich Text Editor'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: 'off',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
|
|
|
'Homeserver URL': {
|
|
|
|
id: 7,
|
|
|
|
expl: _td('Your homeserver\'s URL'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: 'https://matrix.org',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
|
|
|
'Identity Server URL': {
|
|
|
|
id: 8,
|
|
|
|
expl: _td('Your identity server\'s URL'),
|
2018-05-24 19:46:06 +08:00
|
|
|
example: 'https://vector.im',
|
2017-11-19 23:33:07 +08:00
|
|
|
},
|
2017-05-30 02:04:37 +08:00
|
|
|
};
|
|
|
|
|
2017-07-28 00:19:18 +08:00
|
|
|
function whitelistRedact(whitelist, str) {
|
|
|
|
if (whitelist.includes(str)) return str;
|
|
|
|
return '<redacted>';
|
|
|
|
}
|
|
|
|
|
2017-05-28 03:47:09 +08:00
|
|
|
class Analytics {
|
|
|
|
constructor() {
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq = null;
|
2017-05-29 21:26:29 +08:00
|
|
|
this.disabled = true;
|
2017-05-30 00:26:48 +08:00
|
|
|
this.firstPage = true;
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 21:26:29 +08:00
|
|
|
/**
|
|
|
|
* Enable Analytics if initialized but disabled
|
|
|
|
* otherwise try and initalize, no-op if piwik config missing
|
|
|
|
*/
|
|
|
|
enable() {
|
2017-05-30 00:26:48 +08:00
|
|
|
if (this._paq || this._init()) {
|
|
|
|
this.disabled = false;
|
|
|
|
}
|
2017-05-29 21:26:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable Analytics calls, will not fully unload Piwik until a refresh,
|
|
|
|
* but this is second best, Piwik should not pull anything implicitly.
|
|
|
|
*/
|
|
|
|
disable() {
|
2017-06-07 18:26:52 +08:00
|
|
|
this.trackEvent('Analytics', 'opt-out');
|
2018-02-28 09:03:58 +08:00
|
|
|
// disableHeartBeatTimer is undocumented but exists in the piwik code
|
|
|
|
// the _paq.push method will result in an error being printed in the console
|
|
|
|
// if an unknown method signature is passed
|
2018-02-23 00:15:50 +08:00
|
|
|
this._paq.push(['disableHeartBeatTimer']);
|
2017-05-29 21:26:29 +08:00
|
|
|
this.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_init() {
|
|
|
|
const config = SdkConfig.get();
|
|
|
|
if (!config || !config.piwik || !config.piwik.url || !config.piwik.siteId) return;
|
|
|
|
|
|
|
|
const url = config.piwik.url;
|
|
|
|
const siteId = config.piwik.siteId;
|
|
|
|
const self = this;
|
2017-05-28 03:47:09 +08:00
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
window._paq = this._paq = window._paq || [];
|
|
|
|
|
|
|
|
this._paq.push(['setTrackerUrl', url+'piwik.php']);
|
|
|
|
this._paq.push(['setSiteId', siteId]);
|
2017-05-30 02:04:37 +08:00
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq.push(['trackAllContentImpressions']);
|
|
|
|
this._paq.push(['discardHashTag', false]);
|
|
|
|
this._paq.push(['enableHeartBeatTimer']);
|
2017-07-28 00:19:18 +08:00
|
|
|
// this._paq.push(['enableLinkTracking', true]);
|
2017-05-30 00:26:48 +08:00
|
|
|
|
2017-05-30 02:04:37 +08:00
|
|
|
const platform = PlatformPeg.get();
|
2017-05-30 02:52:00 +08:00
|
|
|
this._setVisitVariable('App Platform', platform.getHumanReadableName());
|
2017-05-30 02:04:37 +08:00
|
|
|
platform.getAppVersion().then((version) => {
|
|
|
|
this._setVisitVariable('App Version', version);
|
|
|
|
}).catch(() => {
|
|
|
|
this._setVisitVariable('App Version', 'unknown');
|
|
|
|
});
|
|
|
|
|
|
|
|
this._setVisitVariable('Chosen Language', getCurrentLanguage());
|
|
|
|
|
2017-06-07 18:25:27 +08:00
|
|
|
if (window.location.hostname === 'riot.im') {
|
|
|
|
this._setVisitVariable('Instance', window.location.pathname);
|
|
|
|
}
|
|
|
|
|
2017-05-29 21:26:29 +08:00
|
|
|
(function() {
|
|
|
|
const g = document.createElement('script');
|
|
|
|
const s = document.getElementsByTagName('script')[0];
|
|
|
|
g.type='text/javascript'; g.async=true; g.defer=true; g.src=url+'piwik.js';
|
|
|
|
|
|
|
|
g.onload = function() {
|
|
|
|
console.log('Initialised anonymous analytics');
|
2017-05-30 00:26:48 +08:00
|
|
|
self._paq = window._paq;
|
2017-05-29 21:26:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
s.parentNode.insertBefore(g, s);
|
|
|
|
})();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-28 17:25:28 +08:00
|
|
|
trackPageChange(generationTimeMs) {
|
2017-05-29 21:26:29 +08:00
|
|
|
if (this.disabled) return;
|
2017-05-30 00:26:48 +08:00
|
|
|
if (this.firstPage) {
|
|
|
|
// De-duplicate first page
|
|
|
|
// router seems to hit the fn twice
|
|
|
|
this.firstPage = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-05-04 01:25:00 +08:00
|
|
|
|
|
|
|
if (typeof generationTimeMs === 'number') {
|
|
|
|
this._paq.push(['setGenerationTimeMs', generationTimeMs]);
|
|
|
|
} else {
|
|
|
|
console.warn('Analytics.trackPageChange: expected generationTimeMs to be a number');
|
|
|
|
// But continue anyway because we still want to track the change
|
|
|
|
}
|
|
|
|
|
2017-06-04 04:26:22 +08:00
|
|
|
this._paq.push(['setCustomUrl', getRedactedUrl()]);
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq.push(['trackPageView']);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2018-06-28 22:03:47 +08:00
|
|
|
trackEvent(category, action, name, value) {
|
2017-05-29 21:26:29 +08:00
|
|
|
if (this.disabled) return;
|
2018-06-28 22:03:47 +08:00
|
|
|
this._paq.push(['trackEvent', category, action, name, value]);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
logout() {
|
2017-05-29 21:26:29 +08:00
|
|
|
if (this.disabled) return;
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq.push(['deleteCookies']);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 02:04:37 +08:00
|
|
|
_setVisitVariable(key, value) {
|
2018-04-26 22:18:29 +08:00
|
|
|
if (this.disabled) return;
|
2017-11-19 23:33:07 +08:00
|
|
|
this._paq.push(['setCustomVariable', customVariables[key].id, key, value, 'visit']);
|
2017-05-30 02:04:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 00:19:18 +08:00
|
|
|
setLoggedIn(isGuest, homeserverUrl, identityServerUrl) {
|
2017-05-30 02:04:37 +08:00
|
|
|
if (this.disabled) return;
|
2017-11-19 23:33:07 +08:00
|
|
|
|
|
|
|
const config = SdkConfig.get();
|
2018-04-26 20:41:43 +08:00
|
|
|
if (!config.piwik) return;
|
|
|
|
|
2018-04-26 22:18:29 +08:00
|
|
|
const whitelistedHSUrls = config.piwik.whitelistedHSUrls || [];
|
|
|
|
const whitelistedISUrls = config.piwik.whitelistedISUrls || [];
|
2017-11-19 23:33:07 +08:00
|
|
|
|
2017-07-28 00:19:18 +08:00
|
|
|
this._setVisitVariable('User Type', isGuest ? 'Guest' : 'Logged In');
|
|
|
|
this._setVisitVariable('Homeserver URL', whitelistRedact(whitelistedHSUrls, homeserverUrl));
|
|
|
|
this._setVisitVariable('Identity Server URL', whitelistRedact(whitelistedISUrls, identityServerUrl));
|
2017-05-30 02:04:37 +08:00
|
|
|
}
|
2017-08-10 01:39:06 +08:00
|
|
|
|
|
|
|
setRichtextMode(state) {
|
|
|
|
if (this.disabled) return;
|
|
|
|
this._setVisitVariable('RTE: Uses Richtext Mode', state ? 'on' : 'off');
|
|
|
|
}
|
2017-11-19 23:33:07 +08:00
|
|
|
|
|
|
|
showDetailsModal() {
|
2018-05-24 19:46:06 +08:00
|
|
|
let rows = [];
|
|
|
|
if (window.Piwik) {
|
|
|
|
const Tracker = window.Piwik.getAsyncTracker();
|
|
|
|
rows = Object.values(customVariables).map((v) => Tracker.getCustomVariable(v.id)).filter(Boolean);
|
|
|
|
} else {
|
|
|
|
// Piwik may not have been enabled, so show example values
|
|
|
|
rows = Object.keys(customVariables).map(
|
|
|
|
(k) => [
|
|
|
|
k,
|
|
|
|
_t('e.g. %(exampleValue)s', { exampleValue: customVariables[k].example }),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2017-11-19 23:33:07 +08:00
|
|
|
|
|
|
|
const resolution = `${window.screen.width}x${window.screen.height}`;
|
2018-04-26 17:59:43 +08:00
|
|
|
const otherVariables = [
|
|
|
|
{
|
|
|
|
expl: _td('Every page you use in the app'),
|
|
|
|
value: _t(
|
|
|
|
'e.g. <CurrentPageURL>',
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
CurrentPageURL: getRedactedUrl(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{ expl: _td('Your User Agent'), value: navigator.userAgent },
|
|
|
|
{ expl: _td('Your device resolution'), value: resolution },
|
|
|
|
];
|
2017-11-19 23:33:07 +08:00
|
|
|
|
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
|
|
|
Modal.createTrackedDialog('Analytics Details', '', ErrorDialog, {
|
|
|
|
title: _t('Analytics'),
|
2019-02-05 04:25:26 +08:00
|
|
|
description: <div className="mx_AnalyticsModal">
|
2017-11-19 23:33:07 +08:00
|
|
|
<div>
|
2018-01-08 21:29:27 +08:00
|
|
|
{ _t('The information being sent to us to help make Riot.im better includes:') }
|
2017-11-19 23:33:07 +08:00
|
|
|
</div>
|
|
|
|
<table>
|
|
|
|
{ rows.map((row) => <tr key={row[0]}>
|
|
|
|
<td>{ _t(customVariables[row[0]].expl) }</td>
|
2018-05-24 19:46:06 +08:00
|
|
|
{ row[1] !== undefined && <td><code>{ row[1] }</code></td> }
|
2017-11-19 23:33:07 +08:00
|
|
|
</tr>) }
|
2018-04-26 17:59:43 +08:00
|
|
|
{ otherVariables.map((item, index) =>
|
|
|
|
<tr key={index}>
|
|
|
|
<td>{ _t(item.expl) }</td>
|
|
|
|
<td><code>{ item.value }</code></td>
|
|
|
|
</tr>,
|
|
|
|
) }
|
2017-11-19 23:33:07 +08:00
|
|
|
</table>
|
|
|
|
<div>
|
|
|
|
{ _t('Where this page includes identifiable information, such as a room, '
|
|
|
|
+ 'user or group ID, that data is removed before being sent to the server.') }
|
|
|
|
</div>
|
|
|
|
</div>,
|
|
|
|
});
|
|
|
|
}
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!global.mxAnalytics) {
|
|
|
|
global.mxAnalytics = new Analytics();
|
|
|
|
}
|
|
|
|
module.exports = global.mxAnalytics;
|