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';
|
2017-11-19 16:33:07 +01:00
|
|
|
import SdkConfig, { DEFAULTS } from './SdkConfig';
|
|
|
|
import Modal from './Modal';
|
|
|
|
import sdk from './index';
|
|
|
|
|
|
|
|
function getRedactedHash() {
|
|
|
|
return window.location.hash.replace(/#\/(group|room|user)\/(.+)/, "#/$1/<redacted>");
|
|
|
|
}
|
2017-05-27 21:47:09 +02:00
|
|
|
|
2017-06-03 22:26:22 +02:00
|
|
|
function getRedactedUrl() {
|
2017-06-07 12:17:57 +02:00
|
|
|
// hardcoded url to make piwik happy
|
2017-11-19 16:33:07 +01:00
|
|
|
return 'https://riot.im/app/' + getRedactedHash();
|
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'),
|
|
|
|
},
|
|
|
|
'App Version': {
|
|
|
|
id: 2,
|
|
|
|
expl: _td('The version of Riot.im'),
|
|
|
|
},
|
|
|
|
'User Type': {
|
|
|
|
id: 3,
|
|
|
|
expl: _td('Whether or not you\'re logged in (we don\'t record your user name)'),
|
|
|
|
},
|
|
|
|
'Chosen Language': {
|
|
|
|
id: 4,
|
|
|
|
expl: _td('Your language of choice'),
|
|
|
|
},
|
|
|
|
'Instance': {
|
|
|
|
id: 5,
|
|
|
|
expl: _td('Which officially provided instance you are using, if any'),
|
|
|
|
},
|
|
|
|
'RTE: Uses Richtext Mode': {
|
|
|
|
id: 6,
|
|
|
|
expl: _td('Whether or not you\'re using the Richtext mode of the Rich Text Editor'),
|
|
|
|
},
|
|
|
|
'Homeserver URL': {
|
|
|
|
id: 7,
|
|
|
|
expl: _td('Your homeserver\'s URL'),
|
|
|
|
},
|
|
|
|
'Identity Server URL': {
|
|
|
|
id: 8,
|
|
|
|
expl: _td('Your identity server\'s URL'),
|
|
|
|
},
|
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;
|
2018-03-27 12:17:49 +02:00
|
|
|
this.generationTimeMs = null;
|
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-27 12:17:49 +02:00
|
|
|
startPageChangeTimer() {
|
|
|
|
performance.clearMarks('riot_page_change_start');
|
|
|
|
performance.mark('riot_page_change_start');
|
|
|
|
}
|
|
|
|
|
|
|
|
stopPageChangeTimer() {
|
|
|
|
performance.mark('riot_page_change_stop');
|
|
|
|
performance.measure(
|
|
|
|
'riot_page_change_delta',
|
|
|
|
'riot_page_change_start',
|
|
|
|
'riot_page_change_stop',
|
|
|
|
);
|
|
|
|
|
|
|
|
const measurement = performance.getEntriesByName('riot_page_change_delta').pop();
|
|
|
|
this.generationTimeMs = measurement.duration;
|
|
|
|
}
|
|
|
|
|
2017-05-29 18:26:48 +02:00
|
|
|
trackPageChange() {
|
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;
|
|
|
|
}
|
2017-06-03 22:26:22 +02:00
|
|
|
this._paq.push(['setCustomUrl', getRedactedUrl()]);
|
2018-03-27 12:17:49 +02:00
|
|
|
if (typeof this.generationTimeMs === 'number') {
|
|
|
|
this._paq.push(['setGenerationTimeMs', this.generationTimeMs]);
|
|
|
|
}
|
2017-05-29 18:26:48 +02:00
|
|
|
this._paq.push(['trackPageView']);
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 18:26:48 +02:00
|
|
|
trackEvent(category, action, name) {
|
2017-05-29 15:26:29 +02:00
|
|
|
if (this.disabled) return;
|
2017-05-29 18:26:48 +02:00
|
|
|
this._paq.push(['trackEvent', category, action, name]);
|
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) {
|
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();
|
|
|
|
const whitelistedHSUrls = config.piwik.whitelistedHSUrls || DEFAULTS.piwik.whitelistedHSUrls;
|
|
|
|
const whitelistedISUrls = config.piwik.whitelistedISUrls || DEFAULTS.piwik.whitelistedISUrls;
|
|
|
|
|
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() {
|
|
|
|
const Tracker = window.Piwik.getAsyncTracker();
|
|
|
|
const rows = Object.values(customVariables).map((v) => Tracker.getCustomVariable(v.id)).filter(Boolean);
|
|
|
|
|
|
|
|
const resolution = `${window.screen.width}x${window.screen.height}`;
|
|
|
|
|
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
|
|
|
Modal.createTrackedDialog('Analytics Details', '', ErrorDialog, {
|
|
|
|
title: _t('Analytics'),
|
|
|
|
description: <div>
|
|
|
|
<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>
|
|
|
|
<td><code>{ row[1] }</code></td>
|
|
|
|
</tr>) }
|
|
|
|
</table>
|
|
|
|
<br />
|
|
|
|
<div>
|
|
|
|
{ _t('We also record each page you use in the app (currently <CurrentPageHash>), your User Agent'
|
|
|
|
+ ' (<CurrentUserAgent>) and your device resolution (<CurrentDeviceResolution>).',
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
CurrentPageHash: <code>{ getRedactedHash() }</code>,
|
|
|
|
CurrentUserAgent: <code>{ navigator.userAgent }</code>,
|
|
|
|
CurrentDeviceResolution: <code>{ resolution }</code>,
|
|
|
|
},
|
|
|
|
) }
|
|
|
|
|
|
|
|
{ _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;
|