2017-05-27 21:47:09 +02:00
|
|
|
/*
|
2020-02-13 01:39:28 +01:00
|
|
|
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2017-05-27 21:47:09 +02:00
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
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
|
2017-05-27 21:47:09 +02:00
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2017-05-27 21:47:09 +02:00
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2021-10-23 00:23:32 +02:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2017-05-27 21:47:09 +02:00
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { getCurrentLanguage, _t, _td, IVariables } 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';
|
2021-07-02 18:00:07 +02:00
|
|
|
import * as sdk from './index';
|
2017-11-19 16:33:07 +01:00
|
|
|
|
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.
|
2020-10-13 18:36:50 +02:00
|
|
|
function getRedactedHash(hash: string): string {
|
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) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn(`Unexpected hash location "${hash}"`);
|
2018-04-25 15:23:31 +02:00
|
|
|
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.
|
2020-10-13 18:36:50 +02:00
|
|
|
function getRedactedUrl(): string {
|
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
|
|
|
}
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
interface IData {
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
gt_ms?: string;
|
|
|
|
e_c?: string;
|
|
|
|
e_a?: string;
|
|
|
|
e_n?: string;
|
|
|
|
e_v?: string;
|
|
|
|
ping?: string;
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IVariable {
|
|
|
|
id: number;
|
|
|
|
expl: string; // explanation
|
|
|
|
example: string; // example value
|
|
|
|
getTextVariables?(): IVariables; // object to pass as 2nd argument to `_t`
|
|
|
|
}
|
|
|
|
|
|
|
|
const customVariables: Record<string, IVariable> = {
|
2020-02-14 18:36:14 +01:00
|
|
|
// The Matomo installation at https://matomo.riot.im is currently configured
|
|
|
|
// with a limit of 10 custom variables.
|
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,
|
2020-07-10 20:07:11 +02:00
|
|
|
expl: _td('The version of %(brand)s'),
|
|
|
|
getTextVariables: () => ({
|
|
|
|
brand: SdkConfig.get().brand,
|
|
|
|
}),
|
2018-05-24 13:46:06 +02:00
|
|
|
example: '15.0.0',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
|
|
|
'User Type': {
|
|
|
|
id: 3,
|
2019-01-25 21:25:29 +01:00
|
|
|
expl: _td('Whether or not you\'re logged in (we don\'t record your username)'),
|
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
|
|
|
},
|
2020-02-14 18:36:14 +01:00
|
|
|
'Touch Input': {
|
2017-11-19 16:33:07 +01:00
|
|
|
id: 8,
|
2020-07-10 20:07:11 +02:00
|
|
|
expl: _td("Whether you're using %(brand)s on a device where touch is the primary input mechanism"),
|
|
|
|
getTextVariables: () => ({
|
|
|
|
brand: SdkConfig.get().brand,
|
|
|
|
}),
|
2020-02-14 18:36:14 +01:00
|
|
|
example: 'false',
|
2017-11-19 16:33:07 +01:00
|
|
|
},
|
2020-02-14 15:58:37 +01:00
|
|
|
'Breadcrumbs': {
|
|
|
|
id: 9,
|
|
|
|
expl: _td("Whether or not you're using the 'breadcrumbs' feature (avatars above the room list)"),
|
|
|
|
example: 'disabled',
|
|
|
|
},
|
|
|
|
'Installed PWA': {
|
|
|
|
id: 10,
|
2020-07-10 20:07:11 +02:00
|
|
|
expl: _td("Whether you're using %(brand)s as an installed Progressive Web App"),
|
|
|
|
getTextVariables: () => ({
|
|
|
|
brand: SdkConfig.get().brand,
|
|
|
|
}),
|
2020-02-14 15:58:37 +01:00
|
|
|
example: 'false',
|
|
|
|
},
|
2017-05-29 20:04:37 +02:00
|
|
|
};
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
function whitelistRedact(whitelist: string[], str: string): string {
|
2017-07-27 18:19:18 +02:00
|
|
|
if (whitelist.includes(str)) return str;
|
|
|
|
return '<redacted>';
|
|
|
|
}
|
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
const UID_KEY = "mx_Riot_Analytics_uid";
|
|
|
|
const CREATION_TS_KEY = "mx_Riot_Analytics_cts";
|
|
|
|
const VISIT_COUNT_KEY = "mx_Riot_Analytics_vc";
|
|
|
|
const LAST_VISIT_TS_KEY = "mx_Riot_Analytics_lvts";
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
function getUid(): string {
|
2020-02-13 01:39:28 +01:00
|
|
|
try {
|
2020-04-08 22:42:58 +02:00
|
|
|
let data = localStorage && localStorage.getItem(UID_KEY);
|
|
|
|
if (!data && localStorage) {
|
2020-02-13 01:39:28 +01:00
|
|
|
localStorage.setItem(UID_KEY, data = [...Array(16)].map(() => Math.random().toString(16)[2]).join(''));
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
} catch (e) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("Analytics error: ", e);
|
2020-02-13 01:39:28 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const HEARTBEAT_INTERVAL = 30 * 1000; // seconds
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
export class Analytics {
|
|
|
|
private baseUrl: URL = null;
|
|
|
|
private siteId: string = null;
|
|
|
|
private visitVariables: Record<number, [string, string]> = {}; // {[id: number]: [name: string, value: string]}
|
|
|
|
private firstPage = true;
|
|
|
|
private heartbeatIntervalID: number = null;
|
2020-02-13 01:39:28 +01:00
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
private readonly creationTs: string;
|
|
|
|
private readonly lastVisitTs: string;
|
|
|
|
private readonly visitCount: string;
|
2017-05-27 21:47:09 +02:00
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
constructor() {
|
2020-04-08 22:42:58 +02:00
|
|
|
this.creationTs = localStorage && localStorage.getItem(CREATION_TS_KEY);
|
|
|
|
if (!this.creationTs && localStorage) {
|
2020-10-13 18:36:50 +02:00
|
|
|
localStorage.setItem(CREATION_TS_KEY, this.creationTs = String(new Date().getTime()));
|
2017-05-29 18:26:48 +02:00
|
|
|
}
|
2020-02-13 01:39:28 +01:00
|
|
|
|
2020-04-08 22:42:58 +02:00
|
|
|
this.lastVisitTs = localStorage && localStorage.getItem(LAST_VISIT_TS_KEY);
|
2020-10-13 18:36:50 +02:00
|
|
|
this.visitCount = localStorage && localStorage.getItem(VISIT_COUNT_KEY) || "0";
|
|
|
|
this.visitCount = String(parseInt(this.visitCount, 10) + 1); // increment
|
2020-04-08 22:42:58 +02:00
|
|
|
if (localStorage) {
|
2020-10-13 18:36:50 +02:00
|
|
|
localStorage.setItem(VISIT_COUNT_KEY, this.visitCount);
|
2020-04-08 22:42:58 +02:00
|
|
|
}
|
2020-02-13 01:39:28 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
public get disabled() {
|
2020-02-13 01:39:28 +01:00
|
|
|
return !this.baseUrl;
|
2017-05-29 15:26:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
public canEnable() {
|
2020-09-16 11:29:21 +02:00
|
|
|
const config = SdkConfig.get();
|
|
|
|
return navigator.doNotTrack !== "1" && config && config.piwik && config.piwik.url && config.piwik.siteId;
|
|
|
|
}
|
|
|
|
|
2017-05-29 15:26:29 +02:00
|
|
|
/**
|
2020-02-13 01:39:28 +01:00
|
|
|
* Enable Analytics if initialized but disabled
|
|
|
|
* otherwise try and initalize, no-op if piwik config missing
|
2017-05-29 15:26:29 +02:00
|
|
|
*/
|
2020-10-13 18:36:50 +02:00
|
|
|
public async enable() {
|
2020-02-13 01:39:28 +01:00
|
|
|
if (!this.disabled) return;
|
2020-09-16 11:29:21 +02:00
|
|
|
if (!this.canEnable()) return;
|
2017-05-29 15:26:29 +02:00
|
|
|
const config = SdkConfig.get();
|
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
this.baseUrl = new URL("piwik.php", config.piwik.url);
|
|
|
|
// set constants
|
2020-10-13 18:36:50 +02:00
|
|
|
this.baseUrl.searchParams.set("rec", "1"); // rec is required for tracking
|
2020-02-13 01:39:28 +01:00
|
|
|
this.baseUrl.searchParams.set("idsite", config.piwik.siteId); // rec is required for tracking
|
2020-10-13 18:36:50 +02:00
|
|
|
this.baseUrl.searchParams.set("apiv", "1"); // API version to use
|
|
|
|
this.baseUrl.searchParams.set("send_image", "0"); // we want a 204, not a tiny GIF
|
2020-02-13 01:39:28 +01:00
|
|
|
// set user parameters
|
|
|
|
this.baseUrl.searchParams.set("_id", getUid()); // uuid
|
|
|
|
this.baseUrl.searchParams.set("_idts", this.creationTs); // first ts
|
2020-10-13 18:36:50 +02:00
|
|
|
this.baseUrl.searchParams.set("_idvc", this.visitCount); // visit count
|
2020-02-13 01:39:28 +01:00
|
|
|
if (this.lastVisitTs) {
|
|
|
|
this.baseUrl.searchParams.set("_viewts", this.lastVisitTs); // last visit ts
|
|
|
|
}
|
2017-05-29 18:26:48 +02:00
|
|
|
|
2017-05-29 20:04:37 +02:00
|
|
|
const platform = PlatformPeg.get();
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('App Platform', platform.getHumanReadableName());
|
2020-02-13 01:39:28 +01:00
|
|
|
try {
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('App Version', await platform.getAppVersion());
|
2020-02-13 01:39:28 +01:00
|
|
|
} catch (e) {
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('App Version', 'unknown');
|
2020-02-13 01:39:28 +01:00
|
|
|
}
|
2017-05-29 20:04:37 +02:00
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('Chosen Language', getCurrentLanguage());
|
2017-05-29 20:04:37 +02:00
|
|
|
|
2020-07-13 18:40:41 +02:00
|
|
|
const hostname = window.location.hostname;
|
|
|
|
if (hostname === 'riot.im') {
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('Instance', window.location.pathname);
|
2020-07-13 18:40:41 +02:00
|
|
|
} else if (hostname.endsWith('.element.io')) {
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('Instance', hostname.replace('.element.io', ''));
|
2017-06-07 12:25:27 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 15:58:37 +01:00
|
|
|
let installedPWA = "unknown";
|
|
|
|
try {
|
|
|
|
// Known to work at least for desktop Chrome
|
2020-10-13 18:36:50 +02:00
|
|
|
installedPWA = String(window.matchMedia('(display-mode: standalone)').matches);
|
2020-02-14 15:58:37 +01:00
|
|
|
} catch (e) { }
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('Installed PWA', installedPWA);
|
2020-02-14 15:58:37 +01:00
|
|
|
|
2020-02-14 18:36:14 +01:00
|
|
|
let touchInput = "unknown";
|
|
|
|
try {
|
|
|
|
// MDN claims broad support across browsers
|
2020-10-13 18:36:50 +02:00
|
|
|
touchInput = String(window.matchMedia('(pointer: coarse)').matches);
|
2020-02-14 18:36:14 +01:00
|
|
|
} catch (e) { }
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('Touch Input', touchInput);
|
2020-02-14 18:36:14 +01:00
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
// start heartbeat
|
2020-10-13 18:36:50 +02:00
|
|
|
this.heartbeatIntervalID = window.setInterval(this.ping.bind(this), HEARTBEAT_INTERVAL);
|
2020-02-13 01:39:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-13 11:47:50 +01:00
|
|
|
* Disable Analytics, stop the heartbeat and clear identifiers from localStorage
|
2020-02-13 01:39:28 +01:00
|
|
|
*/
|
2020-10-13 18:36:50 +02:00
|
|
|
public disable() {
|
2020-02-13 01:39:28 +01:00
|
|
|
if (this.disabled) return;
|
|
|
|
this.trackEvent('Analytics', 'opt-out');
|
2020-10-13 18:36:50 +02:00
|
|
|
window.clearInterval(this.heartbeatIntervalID);
|
2020-02-13 01:39:28 +01:00
|
|
|
this.baseUrl = null;
|
|
|
|
this.visitVariables = {};
|
2020-02-13 11:47:50 +01:00
|
|
|
localStorage.removeItem(UID_KEY);
|
|
|
|
localStorage.removeItem(CREATION_TS_KEY);
|
|
|
|
localStorage.removeItem(VISIT_COUNT_KEY);
|
|
|
|
localStorage.removeItem(LAST_VISIT_TS_KEY);
|
2020-02-13 01:39:28 +01:00
|
|
|
}
|
2017-05-29 15:26:29 +02:00
|
|
|
|
2021-07-19 23:43:11 +02:00
|
|
|
private async track(data: IData) {
|
2020-02-13 01:39:28 +01:00
|
|
|
if (this.disabled) return;
|
2017-05-29 15:26:29 +02:00
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
const now = new Date();
|
|
|
|
const params = {
|
|
|
|
...data,
|
|
|
|
url: getRedactedUrl(),
|
|
|
|
|
2020-02-13 12:50:40 +01:00
|
|
|
_cvar: JSON.stringify(this.visitVariables), // user custom vars
|
2020-02-13 01:39:28 +01:00
|
|
|
res: `${window.screen.width}x${window.screen.height}`, // resolution as WWWWxHHHH
|
|
|
|
rand: String(Math.random()).slice(2, 8), // random nonce to cache-bust
|
|
|
|
h: now.getHours(),
|
|
|
|
m: now.getMinutes(),
|
|
|
|
s: now.getSeconds(),
|
|
|
|
};
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
const url = new URL(this.baseUrl.toString()); // copy
|
2020-02-13 01:39:28 +01:00
|
|
|
for (const key in params) {
|
|
|
|
url.searchParams.set(key, params[key]);
|
|
|
|
}
|
2017-05-29 15:26:29 +02:00
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
try {
|
2020-10-13 18:36:50 +02:00
|
|
|
await window.fetch(url.toString(), {
|
2020-02-13 01:39:28 +01:00
|
|
|
method: "GET",
|
|
|
|
mode: "no-cors",
|
|
|
|
cache: "no-cache",
|
|
|
|
redirect: "follow",
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("Analytics error: ", e);
|
2020-02-13 01:39:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
public ping() {
|
2021-07-19 23:43:11 +02:00
|
|
|
this.track({
|
2020-10-13 18:36:50 +02:00
|
|
|
ping: "1",
|
2020-02-13 01:39:28 +01:00
|
|
|
});
|
2020-10-13 18:36:50 +02:00
|
|
|
localStorage.setItem(LAST_VISIT_TS_KEY, String(new Date().getTime())); // update last visit ts
|
2017-05-29 15:26:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
public trackPageChange(generationTimeMs?: number) {
|
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
|
|
|
|
2020-02-13 01:39:28 +01:00
|
|
|
if (typeof generationTimeMs !== 'number') {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn('Analytics.trackPageChange: expected generationTimeMs to be a number');
|
2018-05-03 19:25:00 +02:00
|
|
|
// But continue anyway because we still want to track the change
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:43:11 +02:00
|
|
|
this.track({
|
2020-10-13 18:36:50 +02:00
|
|
|
gt_ms: String(generationTimeMs),
|
2020-02-13 01:39:28 +01:00
|
|
|
});
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
public trackEvent(category: string, action: string, name?: string, value?: string) {
|
2017-05-29 15:26:29 +02:00
|
|
|
if (this.disabled) return;
|
2021-07-19 23:43:11 +02:00
|
|
|
this.track({
|
2020-02-13 01:39:28 +01:00
|
|
|
e_c: category,
|
|
|
|
e_a: action,
|
|
|
|
e_n: name,
|
|
|
|
e_v: value,
|
|
|
|
});
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
private setVisitVariable(key: keyof typeof customVariables, value: string) {
|
2018-04-26 16:18:29 +02:00
|
|
|
if (this.disabled) return;
|
2020-02-13 01:39:28 +01:00
|
|
|
this.visitVariables[customVariables[key].id] = [key, value];
|
2017-05-29 20:04:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 23:58:21 +02:00
|
|
|
public setLoggedIn(isGuest: boolean, homeserverUrl: string) {
|
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 || [];
|
2017-11-19 16:33:07 +01:00
|
|
|
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('User Type', isGuest ? 'Guest' : 'Logged In');
|
|
|
|
this.setVisitVariable('Homeserver URL', whitelistRedact(whitelistedHSUrls, homeserverUrl));
|
2017-05-29 20:04:37 +02:00
|
|
|
}
|
2017-08-09 19:39:06 +02:00
|
|
|
|
2020-10-13 23:58:21 +02:00
|
|
|
public setBreadcrumbs(state: boolean) {
|
2019-04-04 22:17:15 +02:00
|
|
|
if (this.disabled) return;
|
2020-10-13 18:36:50 +02:00
|
|
|
this.setVisitVariable('Breadcrumbs', state ? 'enabled' : 'disabled');
|
2019-04-04 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 23:58:21 +02:00
|
|
|
public showDetailsModal = () => {
|
2018-05-24 13:46:06 +02:00
|
|
|
let rows = [];
|
2020-02-13 01:39:28 +01:00
|
|
|
if (!this.disabled) {
|
|
|
|
rows = Object.values(this.visitVariables);
|
2018-05-24 13:46:06 +02:00
|
|
|
} else {
|
|
|
|
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>',
|
|
|
|
{},
|
|
|
|
{
|
2020-10-13 18:36:50 +02:00
|
|
|
CurrentPageURL: getRedactedUrl,
|
2018-04-26 11:59:43 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
2020-02-14 15:58:37 +01:00
|
|
|
{ expl: _td('Your user agent'), value: navigator.userAgent },
|
2018-04-26 11:59:43 +02:00
|
|
|
{ expl: _td('Your device resolution'), value: resolution },
|
|
|
|
];
|
2017-11-19 16:33:07 +01:00
|
|
|
|
2021-07-02 18:00:07 +02:00
|
|
|
// FIXME: Using an import will result in test failures
|
2021-12-05 23:39:33 +01:00
|
|
|
const cookiePolicyUrl = SdkConfig.get().piwik?.policyUrl;
|
2021-07-02 18:00:07 +02:00
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
2021-12-05 23:39:33 +01:00
|
|
|
const cookiePolicyLink = _t(
|
|
|
|
"Our complete cookie policy can be found <CookiePolicyLink>here</CookiePolicyLink>.",
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
"CookiePolicyLink": (sub) => {
|
|
|
|
return <a href={cookiePolicyUrl} target="_blank" rel="noreferrer noopener">{ sub }</a>;
|
|
|
|
},
|
|
|
|
});
|
2017-11-19 16:33:07 +01:00
|
|
|
Modal.createTrackedDialog('Analytics Details', '', ErrorDialog, {
|
|
|
|
title: _t('Analytics'),
|
2019-02-04 21:25:26 +01:00
|
|
|
description: <div className="mx_AnalyticsModal">
|
2021-12-05 23:39:33 +01:00
|
|
|
{ cookiePolicyUrl && <p>{ cookiePolicyLink }</p> }
|
|
|
|
<div>{ _t('Some examples of the information being sent to us to help make %(brand)s better includes:', {
|
2020-07-10 20:07:11 +02:00
|
|
|
brand: SdkConfig.get().brand,
|
2021-07-19 23:43:11 +02:00
|
|
|
}) }</div>
|
2017-11-19 16:33:07 +01:00
|
|
|
<table>
|
|
|
|
{ rows.map((row) => <tr key={row[0]}>
|
2021-12-05 23:39:33 +01:00
|
|
|
<td className="mx_AnalyticsModal_label">{ _t(
|
2020-07-10 20:07:11 +02:00
|
|
|
customVariables[row[0]].expl,
|
|
|
|
customVariables[row[0]].getTextVariables ?
|
|
|
|
customVariables[row[0]].getTextVariables() :
|
|
|
|
null,
|
2021-07-19 23:43:11 +02:00
|
|
|
) }</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>,
|
|
|
|
});
|
2020-02-13 01:39:28 +01:00
|
|
|
};
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:48:02 +02:00
|
|
|
if (!window.mxAnalytics) {
|
|
|
|
window.mxAnalytics = new Analytics();
|
2017-05-27 21:47:09 +02:00
|
|
|
}
|
2020-10-13 19:48:02 +02:00
|
|
|
export default window.mxAnalytics;
|