2017-05-27 21:47:09 +02: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 16:33:07 +01:00
|
|
|
import { getCurrentLanguage, _t, _td } from './languageHandler';
|
2017-05-29 20:04:37 +02:00
|
|
|
import PlatformPeg from './PlatformPeg';
|
2018-04-26 15:30:27 +02:00
|
|
|
import SdkConfig from './SdkConfig';
|
2017-11-19 16:33:07 +01:00
|
|
|
import Modal from './Modal';
|
|
|
|
import sdk from './index';
|
|
|
|
|
2018-04-25 16:06:45 +02:00
|
|
|
const hashRegex = /#\/(groups?|room|user|settings|register|login|forgot_password|home|directory)/;
|
2018-04-26 13:22:56 +02:00
|
|
|
const hashVarRegex = /#\/(group|room|user)\/.*$/;
|
2018-04-25 15:23:31 +02:00
|
|
|
|
|
|
|
// Remove all but the first item in the hash path. Redact unexpected hashes.
|
|
|
|
function getRedactedHash(hash) {
|
2018-04-26 14:05:59 +02:00
|
|
|
// Don't leak URLs we aren't expecting - they could contain tokens/PII
|
2018-04-25 15:23:31 +02:00
|
|
|
const match = hashRegex.exec(hash);
|
|
|
|
if (!match) {
|
|
|
|
console.warn(`Unexpected hash location "${hash}"`);
|
|
|
|
return '#/<unexpected hash location>';
|
|
|
|
}
|
|
|
|
|
2018-04-26 13:22:56 +02:00
|
|
|
if (hashVarRegex.test(hash)) {
|
|
|
|
return hash.replace(hashVarRegex, "#/$1/<redacted>");
|
|
|
|
}
|
|
|
|
|
2018-04-25 15:23:31 +02:00
|
|
|
return hash.replace(hashRegex, "#/$1");
|
2017-11-19 16:33:07 +01:00
|
|
|
}
|
2017-05-27 21:47:09 +02:00
|
|
|
|
2018-06-26 18:06:12 +02:00
|
|
|
// Return the current origin, path and hash separated with a `/`. This does
|
|
|
|
// not include query parameters.
|
2017-06-03 22:26:22 +02:00
|
|
|
function getRedactedUrl() {
|
2018-06-27 11:31:51 +02:00
|
|
|
const { origin, hash } = window.location;
|
|
|
|
let { pathname } = window.location;
|
2018-06-26 18:06:12 +02:00
|
|
|
|
|
|
|
// Redact paths which could contain unexpected PII
|
|
|
|
if (origin.startsWith('file://')) {
|
|
|
|
pathname = "/<redacted>/";
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:21:30 +02:00
|
|
|
return origin + pathname + getRedactedHash(hash);
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 20:04:37 +02:00
|
|
|
const customVariables = {
|
2017-11-19 16:33:07 +01:00
|
|
|
'App Platform': {
|
|
|
|
id: 1,
|
|
|
|
expl: _td('The platform you\'re on'),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: 'Electron Platform',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
|
|
|
'App Version': {
|
|
|
|
id: 2,
|
|
|
|
expl: _td('The version of Riot.im'),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: '15.0.0',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
|
|
|
'User Type': {
|
|
|
|
id: 3,
|
|
|
|
expl: _td('Whether or not you\'re logged in (we don\'t record your user name)'),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: 'Logged In',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
|
|
|
'Chosen Language': {
|
|
|
|
id: 4,
|
|
|
|
expl: _td('Your language of choice'),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: 'en',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
|
|
|
'Instance': {
|
|
|
|
id: 5,
|
|
|
|
expl: _td('Which officially provided instance you are using, if any'),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: 'app',
|
2017-11-19 16:33:07 +01: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 13:46:06 +02:00
|
|
|
example: 'off',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
|
|
|
'Homeserver URL': {
|
|
|
|
id: 7,
|
|
|
|
expl: _td('Your homeserver\'s URL'),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: 'https://matrix.org',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
|
|
|
'Identity Server URL': {
|
|
|
|
id: 8,
|
|
|
|
expl: _td('Your identity server\'s URL'),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: 'https://vector.im',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
2017-05-29 20:04:37 +02:00
|
|
|
};
|
|
|
|
|
2017-07-27 18:19:18 +02:00
|
|
|
function whitelistRedact(whitelist, str) {
|
|
|
|
if (whitelist.includes(str)) return str;
|
|
|
|
return '<redacted>';
|
|
|
|
}
|
|
|
|
|
2017-05-27 21:47:09 +02:00
|
|
|
class Analytics {
|
|
|
|
constructor() {
|
2017-05-29 18:26:48 +02:00
|
|
|
this._paq = null;
|
2017-05-29 15:26:29 +02:00
|
|
|
this.disabled = true;
|
2017-05-29 18:26:48 +02:00
|
|
|
this.firstPage = true;
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 15:26:29 +02:00
|
|
|
/**
|
|
|
|
* Enable Analytics if initialized but disabled
|
|
|
|
* otherwise try and initalize, no-op if piwik config missing
|
|
|
|
*/
|
|
|
|
enable() {
|
2017-05-29 18:26:48 +02:00
|
|
|
if (this._paq || this._init()) {
|
|
|
|
this.disabled = false;
|
|
|
|
}
|
2017-05-29 15:26:29 +02: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 12:26:52 +02:00
|
|
|
this.trackEvent('Analytics', 'opt-out');
|
2018-02-28 02:03:58 +01: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-22 17:15:50 +01:00
|
|
|
this._paq.push(['disableHeartBeatTimer']);
|
2017-05-29 15:26:29 +02: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-27 21:47:09 +02:00
|
|
|
|
2017-05-29 18:26:48 +02:00
|
|
|
window._paq = this._paq = window._paq || [];
|
|
|
|
|
|
|
|
this._paq.push(['setTrackerUrl', url+'piwik.php']);
|
|
|
|
this._paq.push(['setSiteId', siteId]);
|
2017-05-29 20:04:37 +02:00
|
|
|
|
2017-05-29 18:26:48 +02:00
|
|
|
this._paq.push(['trackAllContentImpressions']);
|
|
|
|
this._paq.push(['discardHashTag', false]);
|
|
|
|
this._paq.push(['enableHeartBeatTimer']);
|
2017-07-27 18:19:18 +02:00
|
|
|
// this._paq.push(['enableLinkTracking', true]);
|
2017-05-29 18:26:48 +02:00
|
|
|
|
2017-05-29 20:04:37 +02:00
|
|
|
const platform = PlatformPeg.get();
|
2017-05-29 20:52:00 +02:00
|
|
|
this._setVisitVariable('App Platform', platform.getHumanReadableName());
|
2017-05-29 20:04:37 +02:00
|
|
|
platform.getAppVersion().then((version) => {
|
|
|
|
this._setVisitVariable('App Version', version);
|
|
|
|
}).catch(() => {
|
|
|
|
this._setVisitVariable('App Version', 'unknown');
|
|
|
|
});
|
|
|
|
|
|
|
|
this._setVisitVariable('Chosen Language', getCurrentLanguage());
|
|
|
|
|
2017-06-07 12:25:27 +02:00
|
|
|
if (window.location.hostname === 'riot.im') {
|
|
|
|
this._setVisitVariable('Instance', window.location.pathname);
|
|
|
|
}
|
|
|
|
|
2017-05-29 15:26:29 +02: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-29 18:26:48 +02:00
|
|
|
self._paq = window._paq;
|
2017-05-29 15:26:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
s.parentNode.insertBefore(g, s);
|
|
|
|
})();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-28 11:25:28 +02:00
|
|
|
trackPageChange(generationTimeMs) {
|
2017-05-29 15:26:29 +02:00
|
|
|
if (this.disabled) return;
|
2017-05-29 18:26:48 +02:00
|
|
|
if (this.firstPage) {
|
|
|
|
// De-duplicate first page
|
|
|
|
// router seems to hit the fn twice
|
|
|
|
this.firstPage = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-05-03 19:25:00 +02: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-03 22:26:22 +02:00
|
|
|
this._paq.push(['setCustomUrl', getRedactedUrl()]);
|
2017-05-29 18:26:48 +02:00
|
|
|
this._paq.push(['trackPageView']);
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 16:03:47 +02:00
|
|
|
trackEvent(category, action, name, value) {
|
2017-05-29 15:26:29 +02:00
|
|
|
if (this.disabled) return;
|
2018-06-28 16:03:47 +02:00
|
|
|
this._paq.push(['trackEvent', category, action, name, value]);
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 18:26:48 +02:00
|
|
|
logout() {
|
2017-05-29 15:26:29 +02:00
|
|
|
if (this.disabled) return;
|
2017-05-29 18:26:48 +02:00
|
|
|
this._paq.push(['deleteCookies']);
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 20:04:37 +02:00
|
|
|
_setVisitVariable(key, value) {
|
2018-04-26 16:18:29 +02:00
|
|
|
if (this.disabled) return;
|
2017-11-19 16:33:07 +01:00
|
|
|
this._paq.push(['setCustomVariable', customVariables[key].id, key, value, 'visit']);
|
2017-05-29 20:04:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 18:19:18 +02:00
|
|
|
setLoggedIn(isGuest, homeserverUrl, identityServerUrl) {
|
2017-05-29 20:04:37 +02:00
|
|
|
if (this.disabled) return;
|
2017-11-19 16:33:07 +01:00
|
|
|
|
|
|
|
const config = SdkConfig.get();
|
2018-04-26 14:41:43 +02:00
|
|
|
if (!config.piwik) return;
|
|
|
|
|
2018-04-26 16:18:29 +02:00
|
|
|
const whitelistedHSUrls = config.piwik.whitelistedHSUrls || [];
|
|
|
|
const whitelistedISUrls = config.piwik.whitelistedISUrls || [];
|
2017-11-19 16:33:07 +01:00
|
|
|
|
2017-07-27 18:19:18 +02: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-29 20:04:37 +02:00
|
|
|
}
|
2017-08-09 19:39:06 +02:00
|
|
|
|
|
|
|
setRichtextMode(state) {
|
|
|
|
if (this.disabled) return;
|
|
|
|
this._setVisitVariable('RTE: Uses Richtext Mode', state ? 'on' : 'off');
|
|
|
|
}
|
2017-11-19 16:33:07 +01:00
|
|
|
|
|
|
|
showDetailsModal() {
|
2018-05-24 13:46:06 +02: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 16:33:07 +01:00
|
|
|
|
|
|
|
const resolution = `${window.screen.width}x${window.screen.height}`;
|
2018-04-26 11:59:43 +02: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 16:33:07 +01:00
|
|
|
|
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
|
|
|
Modal.createTrackedDialog('Analytics Details', '', ErrorDialog, {
|
|
|
|
title: _t('Analytics'),
|
2018-04-26 11:59:43 +02:00
|
|
|
description: <div className="mx_UserSettings_analyticsModal">
|
2017-11-19 16:33:07 +01:00
|
|
|
<div>
|
2018-01-08 14:29:27 +01:00
|
|
|
{ _t('The information being sent to us to help make Riot.im better includes:') }
|
2017-11-19 16:33:07 +01:00
|
|
|
</div>
|
|
|
|
<table>
|
|
|
|
{ rows.map((row) => <tr key={row[0]}>
|
|
|
|
<td>{ _t(customVariables[row[0]].expl) }</td>
|
2018-05-24 13:46:06 +02:00
|
|
|
{ row[1] !== undefined && <td><code>{ row[1] }</code></td> }
|
2017-11-19 16:33:07 +01:00
|
|
|
</tr>) }
|
2018-04-26 11:59:43 +02:00
|
|
|
{ otherVariables.map((item, index) =>
|
|
|
|
<tr key={index}>
|
|
|
|
<td>{ _t(item.expl) }</td>
|
|
|
|
<td><code>{ item.value }</code></td>
|
|
|
|
</tr>,
|
|
|
|
) }
|
2017-11-19 16:33:07 +01: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-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!global.mxAnalytics) {
|
|
|
|
global.mxAnalytics = new Analytics();
|
|
|
|
}
|
|
|
|
module.exports = global.mxAnalytics;
|