feat: rewrite stats backend

This commit is contained in:
David Ralph
2024-09-03 14:06:13 +01:00
parent 742b762ae2
commit ed6f660099
22 changed files with 48 additions and 54 deletions

View File

@@ -28,7 +28,7 @@ const useAppSetup = () => {
EventBus.on('refresh', refreshHandler);
variables.stats.tabLoad();
variables.stats.postEvent('tabs-opened');
return () => {
EventBus.off('refresh', refreshHandler);

View File

@@ -18,7 +18,7 @@ const Tabs = (props) => {
const onClick = (tab, name) => {
if (name !== currentName) {
variables.stats.postEvent('tab', `Opened ${name}`);
variables.stats.postEvent('tab', ${name}, 'opened');
}
setCurrentTab(tab);
setCurrentName(name);

View File

@@ -6,7 +6,7 @@ import { Tooltip, Button } from 'components/Elements';
function ResetModal({ modalClose }) {
const reset = () => {
variables.stats.postEvent('setting', 'Reset');
variables.stats.postEvent('setting', 'reset');
setDefaultSettings('reset');
window.location.reload();
};

View File

@@ -21,7 +21,7 @@ const Checkbox = (props) => {
props.onChange(value);
}
variables.stats.postEvent('setting', `${props.name} ${value ? 'enabled' : 'disabled'}`);
variables.stats.postEvent('setting', props.name, value ? 'enabled' : 'disabled');
if (props.element) {
if (!document.querySelector(props.element)) {

View File

@@ -29,7 +29,11 @@ function Radio(props) {
props.onChange(value);
}
variables.stats.postEvent('setting', `${props.name} from ${value} to ${value}`);
if (props.name !== 'language') {
variables.stats.postEvent('setting', props.name, `${value}-${value}`);
} else {
variables.stats.postEvent('language', props.name, `${value}-${value}`);
}
if (props.element) {
if (!document.querySelector(props.element)) {

View File

@@ -22,7 +22,7 @@ function Header(props) {
variables.stats.postEvent(
'setting',
`${props.name} ${setting === true ? 'enabled' : 'disabled'}`,
props.setting, setting === true ? 'enabled' : 'disabled',
);
EventBus.emit('toggle', props.setting);

View File

@@ -31,7 +31,7 @@ const Controls = (props) => {
variables.stats.postEvent(
'setting',
`${props.name} ${setting === true ? 'enabled' : 'disabled'}`,
props.name, setting === true ? 'enabled' : 'disabled',
);
EventBus.emit('toggle', props.setting);

View File

@@ -23,7 +23,7 @@ function Favourite({ credit, offline, pun, tooltipText }) {
localStorage.removeItem('favourite');
setFavourited(buttons.unfavourited);
tooltipText(variables.getMessage('widgets.quote.favourite'));
variables.stats.postEvent('feature', 'Background favourite');
variables.stats.postEvent('feature', 'background', 'favourite');
} else {
const type = localStorage.getItem('backgroundType') || defaults.backgroundType;
@@ -87,7 +87,7 @@ function Favourite({ credit, offline, pun, tooltipText }) {
tooltipText(variables.getMessage('widgets.quote.unfavourite'));
variables.stats.postEvent('feature', 'Background unfavourite');
variables.stats.postEvent('feature', 'background', 'unfavourite');
}
}

View File

@@ -50,7 +50,7 @@ const downloadImage = async (info) => {
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
variables.stats.postEvent('feature', 'Background download');
variables.stats.postEvent('feature', 'background', 'download');
};
function PhotoInformation({ info, url, api }) {

View File

@@ -2,7 +2,7 @@ import { memo } from 'react';
import variables from 'config/variables';
function Lightbox({ modalClose, img }) {
variables.stats.postEvent('modal', 'Opened lightbox');
variables.stats.postEvent('modal', 'lightbox', 'opened');
return (
<>

View File

@@ -75,7 +75,7 @@ function Marketplace() {
setType('item');
variables.stats.postEvent('marketplace-item', `${item.display_name} viewed`);
variables.stats.postEvent('marketplace-item', item.display_name, 'viewed');
} else if (pageType === 'collection') {
setDone(false);
setItem({});

View File

@@ -17,7 +17,7 @@ function Modals() {
window.location.search !== '?nointro=true'
) {
setWelcomeVisible(true);
variables.stats.postEvent('modal', 'Opened welcome');
variables.stats.postEvent('modal', 'welcome', 'opened');
}
if (window.location.search === '?nointro=true') {
@@ -45,7 +45,7 @@ function Modals() {
}
if (action !== false) {
variables.stats.postEvent('modal', `Opened ${type.replace('Modal', '')}`);
variables.stats.postEvent('modal', type.replace('Modal', ''), 'opened');
}
};

View File

@@ -88,7 +88,7 @@ const Overview = () => {
useEffect(() => {
localStorage.setItem('order', JSON.stringify(items));
variables.stats.postEvent('setting', 'Widget order');
variables.stats.postEvent('setting', 'widget-order');
EventBus.emit('refresh', 'widgets');
}, [items]);

View File

@@ -49,14 +49,14 @@ function Maximise(props) {
if (hidden === false) {
setAttribute(0, 100);
variables.stats.postEvent('feature', 'Background maximise');
variables.stats.postEvent('feature', 'background-maximise');
} else {
setAttribute(
localStorage.getItem('blur') || defaults.blur,
localStorage.getItem('brightness') || defaults.brightness,
true,
);
variables.stats.postEvent('feature', 'Background unmaximise');
variables.stats.postEvent('feature', 'background-unmaximise');
}
};

View File

@@ -65,7 +65,7 @@ class Notes extends PureComponent {
}
pin() {
variables.stats.postEvent('feature', 'Notes pin');
variables.stats.postEvent('feature', 'notes', 'pin');
const notesPinned = localStorage.getItem('notesPinned') === 'true';
localStorage.setItem('notesPinned', !notesPinned);
this.setState({
@@ -74,7 +74,7 @@ class Notes extends PureComponent {
}
copy() {
variables.stats.postEvent('feature', 'Notes copied');
variables.stats.postEvent('feature', 'notes', 'copied');
navigator.clipboard.writeText(this.state.notes);
toast(variables.getMessage('toasts.notes'));
}
@@ -85,7 +85,7 @@ class Notes extends PureComponent {
return;
}
variables.stats.postEvent('feature', 'Notes download');
variables.stats.postEvent('feature', 'notes', 'download');
saveFile(this.state.notes, 'mue-notes.txt', 'text/plain');
}

View File

@@ -129,7 +129,7 @@ class Todo extends PureComponent {
}
pin() {
variables.stats.postEvent('feature', 'Todo pin');
variables.stats.postEvent('feature', 'todo', 'pin');
const todoPinned = localStorage.getItem('todoPinned') === 'true';
localStorage.setItem('todoPinned', !todoPinned);
this.setState({

View File

@@ -61,7 +61,7 @@ function AppsOptions({ appsEnabled }) {
iconError: '',
});
variables.stats.postEvent('feature', 'App link add');
variables.stats.postEvent('feature', 'app-link', 'add');
};
const startEditLink = (data) => {
@@ -102,7 +102,7 @@ function AppsOptions({ appsEnabled }) {
items: data,
}));
variables.stats.postEvent('feature', 'App link delete');
variables.stats.postEvent('feature', 'app-link', 'delete');
};
return (

View File

@@ -60,7 +60,7 @@ function NavbarOptions() {
variables.stats.postEvent(
'setting',
`${settingName} ${!isDisabled === true ? 'enabled' : 'disabled'}`,
settingName, !isDisabled === true ? 'enabled' : 'disabled',
);
EventBus.emit('refresh', 'navbar');

View File

@@ -38,7 +38,7 @@ class QuickLinksOptions extends PureComponent {
items: data,
});
variables.stats.postEvent('feature', 'Quicklink delete');
variables.stats.postEvent('feature', 'quicklink' ,'delete');
}
async addLink(name, url, icon) {
@@ -76,7 +76,7 @@ class QuickLinksOptions extends PureComponent {
iconError: '',
});
variables.stats.postEvent('feature', 'Quicklink add');
variables.stats.postEvent('feature', 'quicklink', 'add');
}
startEditLink(data) {

View File

@@ -318,7 +318,7 @@ class Quote extends PureComponent {
}
copyQuote() {
variables.stats.postEvent('feature', 'Quote copied');
variables.stats.postEvent('feature', 'quote', 'copied');
navigator.clipboard.writeText(`${this.state.quote} - ${this.state.author}`);
toast(variables.getMessage('toasts.quote'));
}
@@ -336,7 +336,7 @@ class Quote extends PureComponent {
});
}
variables.stats.postEvent('feature', 'Quote favourite');
variables.stats.postEvent('feature', 'quote', 'favourite');
}
init() {

View File

@@ -107,7 +107,7 @@ function Search() {
}
setTimeout(() => {
variables.stats.postEvent('feature', 'Voice search');
variables.stats.postEvent('feature', 'voice-search');
window.location.href = url + `?${query}=` + searchText.value;
}, 1000);
};
@@ -116,7 +116,7 @@ function Search() {
function searchButton(e) {
e.preventDefault();
const value = e.target.value || document.getElementById('searchtext').value || 'mue fast';
variables.stats.postEvent('feature', 'Search');
variables.stats.postEvent('feature', 'search');
window.location.href = url + `?${query}=` + value;
}

View File

@@ -20,39 +20,29 @@ export default class Stats {
}
/**
* It takes two arguments, a type and a name, and then it increments the value of the name in the type
* It takes three arguments, a type, a name, and an action, and then it increments the value of the name in the type
* object in localStorage
* @param type - The type of event you want to track. This can be anything you want, but I recommend
* using something like "click" or "hover"
* @param name - The name of the event.
* @param action - The action of the event.
*/
static async postEvent(type, name) {
const value = name.toLowerCase().replaceAll(' ', '-');
static async postEvent(type, name, action) {
const data = JSON.parse(localStorage.getItem('statsData')) || {};
// tl;dr this creates the objects if they don't exist
// this really needs a cleanup at some point
if (!data[type] || !data[type][value]) {
if (!data[type]) {
data[type] = {};
}
if (!data[type][value]) {
data[type][value] = 1;
}
if (!name) {
data[type] = data[type] + 1 || 1;
} else {
data[type][value] = data[type][value] + 1;
}
localStorage.setItem('statsData', JSON.stringify(data));
this.achievementTrigger(data);
}
data[type] = data[type] || {};
if (action) {
data[type][name] = data[type][name] || {};
data[type][name][action] = data[type][name][action] + 1 || 1;
} else {
data[type][name] = data[type][name] + 1 || 1;
}
}
/**
* It increments the value of the key 'tabs-opened' in the object stored in localStorage by 1.
*/
static async tabLoad() {
const data = JSON.parse(localStorage.getItem('statsData')) || {};
data['tabs-opened'] = data['tabs-opened'] + 1 || 1;
localStorage.setItem('statsData', JSON.stringify(data));
this.achievementTrigger(data);
}