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