Skip to content

Commit

Permalink
new tracking metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Hybes committed Nov 8, 2023
1 parent fcc5f7e commit 0146824
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 61 deletions.
87 changes: 28 additions & 59 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const util = require('util');
const fetch = require('node-fetch')
const exec = util.promisify(require('child_process').exec);
const Sentry = require('@sentry/electron');
const { send } = require('process');

const configPath = path.join(app.getPath('userData'), 'config.json');
const defaultBlitzPath = path.join(app.getPath('home'), 'AppData', 'Local', 'Programs', 'Blitz', 'Blitz.exe');
Expand Down Expand Up @@ -46,11 +47,10 @@ if (fs.existsSync(configPath)) {
const loadedConfig = JSON.parse(fileData);
config = { ...config, ...loadedConfig };
} catch (error) {
console.error('Error reading configuration:', error);
Sentry.captureException(error);
}
}
else {
// It's the first time the app is being started. Set it to start on boot by default.
app.setLoginItemSettings({
openAtLogin: true
});
Expand Down Expand Up @@ -92,15 +92,17 @@ autoUpdater.setFeedURL({
owner: 'Hybes'
});
autoUpdater.on('checking-for-update', () => {
sendTracking('/auto-update', 'Auto Update');
openUpdateWindow();
console.log('Checking for update...');
});
autoUpdater.on('update-available', (info) => {
sendTracking('/update-available', 'Update Available');
if (updateWindow) {
updateWindow.webContents.send('update-status', 'Update available. Downloading...');
}
});
autoUpdater.on('update-downloaded', (info) => {
sendTracking('/update-downloaded', 'Update Downloaded');
if (updateWindow) {
updateWindow.webContents.send('update-status', 'Update downloaded. Installing...');
}
Expand All @@ -109,6 +111,7 @@ autoUpdater.on('update-downloaded', (info) => {
}, 500);
});
autoUpdater.on('update-not-available', () => {
sendTracking('/update-not-available', 'Update Not Available');
if (updateWindow) {
updateWindow.webContents.send('update-status', 'You have the latest version.');
setTimeout(() => {
Expand All @@ -117,6 +120,7 @@ autoUpdater.on('update-not-available', () => {
}
});
autoUpdater.on('error', (err) => {
sendTracking('/update-error', 'Update Error');
if (updateWindow) {
Sentry.captureException(error);
updateWindow.webContents.send('update-status', 'Error: ' + err.message);
Expand All @@ -131,7 +135,7 @@ autoUpdater.on('download-progress', (progressObj) => {

async function isTaskRunning(taskName) {
if (!taskName) {
console.error('Error: taskName is not provided or is undefined');
Sentry.captureException('Config is malformed, taskName is missing');
return false;
}

Expand All @@ -142,7 +146,7 @@ async function isTaskRunning(taskName) {
if (!error.stdout || !error.stderr) {
return false;
}
console.error(`Error checking task ${taskName}:`, error);
Sentry.captureException(error);
return false;
}
}
Expand All @@ -151,7 +155,7 @@ async function ensureBlitzIsRunning() {
if (!isBlitzRunning) {
exec(`"${config.blitzPath}"`, (error) => {
if (error) {
console.error("Error starting Blitz:", error);
Sentry.captureException(error);
}
});
}
Expand All @@ -169,77 +173,40 @@ async function startMonitoring() {
} else if (!isLeagueClientRunning && !isLeagueOfLegendsRunning && isBlitzRunning) {
exec('taskkill /im Blitz.exe /f', (errorKill) => {
if (errorKill) {
console.error("Error killing Blitz:", errorKill);
Sentry.captureException(errorKill);
}
});
}
} catch (error) {
console.error("Error during monitoring:", error);
Sentry.captureException(error);
}
}
}, 3000);
}
async function sendWebhook() {
async function sendTracking(data, name) {
if (config.anonymousUsage) {
try {
const response = await fetch('https://api.brth.uk/v2/bflo-collector', {
const response = await fetch('https://views.cnnct.uk/api/event', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ event: 'app-started' })
});
Sentry.captureMessage('Webhook Sent for Stats', {
level: 'info',
extra: {
endpoint: 'https://api.brth.uk/v2/bflo-collector',
method: 'POST',
},
body: JSON.stringify({"name":"pageview","url":data,"domain":"bflo.com"})
});

if (!response.ok) {
console.error('Webhook error:', response.statusText);
Sentry.captureException(response.statusText);
}
} catch (error) {
Sentry.captureException(error);
console.error('Webhook error:', error);
}
}
if (config.appInstalled) {
try {
const response = await fetch('https://api.brth.uk/v2/bflo-install-collector', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ event: 'app-installed' })
});
Sentry.captureMessage('Webhook Sent for Install', {
level: 'info',
extra: {
endpoint: 'https://api.brth.uk/v2/bflo-install-collector',
method: 'POST',
},
});
config.appInstalled = false;
if (!response.ok) {
console.error('Webhook error:', response.statusText);
}
} catch (error) {
Sentry.captureException(error);
console.error('Webhook error:', error);
}
}
}

async function killLeagueProcesses() {
try {
await exec('taskkill /im "League of Legends.exe" /f');
await exec('taskkill /im "LeagueClient.exe" /f');
Sentry.captureMessage('User killed League manually', {
level: 'info',
extra: {
feature: 'killLeagueProcess',
},
});
console.log("Successfully killed League processes.");
} catch (error) {
console.error("Error killing League processes:", error);
Sentry.captureException(error);
}
}
function setupTray() {
Expand All @@ -255,6 +222,7 @@ function updateTrayMenu() {
type: 'checkbox',
checked: monitoring,
click: () => {
sendTracking('/toggle-monitoring', 'Monitoring Toggle');
monitoring = !monitoring;
if (monitoring) {
startMonitoring();
Expand All @@ -267,26 +235,25 @@ function updateTrayMenu() {
type: 'checkbox',
checked: app.getLoginItemSettings().openAtLogin,
click: () => {
sendTracking('/toggle-start-on-boot', 'Start on Boot Toggle');
const startOnBoot = !app.getLoginItemSettings().openAtLogin;
app.setLoginItemSettings({
openAtLogin: startOnBoot
});
updateTrayMenu();
Sentry.captureMessage('User accessed feature X', {
level: 'info',
extra: {
feature: 'toggleStartOnBoot',
},
});
}
},
{
label: 'Force Kill League',
click: killLeagueProcesses
click: () => {
sendTracking('/force-kill-league', 'Force Kill League');
killLeagueProcesses();
},
},
{
label: 'About',
click: () => {
sendTracking('/about', 'About');
openMainWindow();
}
},
Expand All @@ -297,6 +264,7 @@ function updateTrayMenu() {
{
label: 'Check for Updates',
click: () => {
sendTracking('/check-for-updates', 'Check for Updates');
if (process.env.NODE_ENV === 'development') {
// Mock response for development
setTimeout(() => {
Expand All @@ -310,6 +278,7 @@ function updateTrayMenu() {
{
label: 'Quit',
click: () => {
sendTracking('/quit', 'Quit');
app.quit();
}
}
Expand Down Expand Up @@ -351,10 +320,10 @@ function openMainWindow() {

app.whenReady().then(() => {
setupTray();
sendTracking('/app-open', 'App Open');
if (monitoring) {
startMonitoring();
}
sendWebhook();
});
app.on('browser-window-created', function (e, window) {
window.webContents.on('devtools-opened', function (e) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "blitz-for-league-only",
"productName": "Blitz for League Only",
"version": "1.6.10",
"version": "1.7.0",
"description": "Open and Close Blitz based on the status of League Client",
"main": "main.js",
"scripts": {
Expand Down Expand Up @@ -41,7 +41,7 @@
]
},
"keywords": [],
"author": "",
"author": "Ben Hybert",
"license": "ISC",
"dependencies": {
"@sentry/electron": "^4.10.0",
Expand Down

0 comments on commit 0146824

Please sign in to comment.