1
0
Fork 0

Add option to send usage stats

master
Gordon Williams 2022-10-19 15:33:41 +01:00
parent 638400e99c
commit 13fd18ff8c
3 changed files with 51 additions and 3 deletions

2
core

@ -1 +1 @@
Subproject commit 76419750083a88ee7a569db3975ae1bdd6dc155a
Subproject commit 80de03d8e665c210dc3443d6869176c848ab103f

View File

@ -151,6 +151,10 @@
<input type="checkbox" id="settings-ble-compat">
<i class="form-icon"></i> Bluetooth Compatibility mode (limit to 20 byte writes)
</label>
<label class="form-switch">
<input type="checkbox" id="settings-usage-stats">
<i class="form-icon"></i> Send app analytics to banglejs.com (apps installed, favourites, firmware version)
</label>
<div class="form-group">
<select class="form-select form-inline" id="settings-lang" style="width: 10em">
<option value="">None (English)</option>

View File

@ -48,8 +48,6 @@ function onFoundDeviceInfo(deviceId, deviceVersion) {
} else if (versionLess(deviceVersion, RECOMMENDED_VERSION)) {
showToast(`You're using an old Bangle.js firmware (${deviceVersion}) and ${RECOMMENDED_VERSION} is available (<a href="http://www.espruino.com/ChangeLog" target="_blank">see changes</a>). You can update ${fwExtraText}<a href="${fwURL}" target="_blank">with the instructions here</a>` ,"warning", 20000);
}
// check against features shown?
filterAppsForDevice(deviceId);
/* if we'd saved a device ID but this device is different, ensure
@ -59,6 +57,43 @@ function onFoundDeviceInfo(deviceId, deviceVersion) {
setSavedDeviceId(undefined);
}
// Called when we refresh the list of installed apps
function onRefreshMyApps() {
/* if we're allowed to, send usage stats. We'll only
actually send if the data has changed */
sendUsageStats();
}
var submittedUsageInfo = "";
/* Send usage stats to servers if it has changed */
function sendUsageStats() {
if (!SETTINGS.sendUsageStats) return; // not allowed!
if (device.uid === undefined) return; // no data yet!
/* Work out what we'll send:
* A suitably garbled UID so we can avoid too many duplicates
* firmware version
* apps installed
* apps favourited
*/
var usageInfo = `uid=${encodeURIComponent(device.uid)}&fw=${encodeURIComponent(device.version)}&apps=${encodeURIComponent(device.appsInstalled.map(a=>a.id).join(","))}&favs=${encodeURIComponent(SETTINGS.favourites.join(","))}`;
// Do a quick check for unchanged data to reduce server load
if (usageInfo != submittedUsageInfo) {
console.log("sendUsageStats: Submitting usage stats...");
var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
xmlhttp.open("POST", "https://banglejs.com/submit_app_stats.php", true /*async*/);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.onload = (e) => {
if (xmlhttp.readyState === 4)
console.log(`sendUsageStats (${xmlhttp.status}): ${xmlhttp.responseText}`);
};
xmlhttp.onerror = (e) => {
console.error("sendUsageStats ERROR: "+xmlhttp.statusText);
};
xmlhttp.send(usageInfo);
submittedUsageInfo = usageInfo;
}
}
var originalAppJSON = undefined;
function filterAppsForDevice(deviceId) {
if (originalAppJSON===undefined && appJSON.length)
@ -205,6 +240,15 @@ window.addEventListener('load', (event) => {
saveSettings();
});
// Sending usage stats
var selectUsageStats = document.getElementById("settings-usage-stats");
selectUsageStats.checked = !!SETTINGS.sendUsageStats;
selectUsageStats.addEventListener("change",event=>{
console.log("Send Usage Stats "+(event.target.checked?"on":"off"));
SETTINGS.sendUsageStats = event.target.checked;
saveSettings();
});
// Load language list
httpGet("lang/index.json").then(languagesJSON=>{
var languages;