feat: locally store stats so users can see them (no ui yet)

This commit is contained in:
David Ralph
2021-06-30 11:27:54 +01:00
parent 1d44b2792e
commit 2dcaa5270d
28 changed files with 78 additions and 45 deletions

View File

@@ -234,5 +234,13 @@
{
"name": "backgroundFilterAmount",
"value": 0
},
{
"name": "stats",
"value": false
},
{
"name": "statsData",
"value": "{}"
}
]

View File

@@ -27,7 +27,7 @@ export default class SettingsFunctions {
settings[key] = localStorage.getItem(key);
});
saveFile(settings, 'mue-settings.json');
window.analytics.postEvent('tab', 'Settings exported');
window.stats.postEvent('tab', 'Settings exported');
}
static setItem(key, value) {

View File

@@ -1,10 +1,12 @@
export default class Analytics {
export default class Stats {
constructor(id) {
this.id = id;
this.domain = window.constants.UMAMI_DOMAIN;
}
async postEvent(type, name) {
const value = name.toLowerCase().replaceAll(' ', '-');
await fetch(this.domain + '/api/collect', {
method: 'POST',
headers: {
@@ -17,13 +19,29 @@ export default class Analytics {
website: this.id,
url: '/',
event_type: type,
event_value: name.toLowerCase().replaceAll(' ', '-'),
event_value: value,
hostname: 'localhost',
language: localStorage.getItem('language').replace('_', '-'),
screen: `${window.screen.width}x${window.screen.height}`
}
})
});
let 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;
}
} else {
data[type][value] = data[type][value] + 1;
}
localStorage.setItem('statsData', JSON.stringify(data));
}
async tabLoad() {
@@ -45,5 +63,9 @@ export default class Analytics {
}
})
});
let data = JSON.parse(localStorage.getItem('statsData'));
data['tabs-opened'] = data['tabs-opened'] + 1 || 1;
localStorage.setItem('statsData', JSON.stringify(data));
}
}