Skip to content

Commit

Permalink
feat: Include basic app usage analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed Jun 13, 2024
1 parent f890559 commit 6fbaab4
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 130 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GA4_ID=string;
GA4_SECRET=string;
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ jobs:
APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
WINDOWS_CERTIFICATE_PASSWORD: ${{ secrets.WINDOWS_CERTIFICATE_PASSWORD }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GA4_ID: ${{ secrets.GA4_ID }}
GA4_SECRET: ${{ secrets.GA4_SECRET }}

windows:
name: Publish windows app to release
Expand Down
367 changes: 238 additions & 129 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"crypto-browserify": "^3.12.0",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.5.0",
"electron-google-analytics4": "^1.2.0",
"electron-log": "^5.1.5",
"electron-squirrel-startup": "^1.0.1",
"electron-store": "^9.0.0",
Expand All @@ -100,6 +101,8 @@
"humanize-duration": "^3.32.1",
"immer": "^10.1.1",
"ky": "^1.3.0",
"md5": "^2.3.0",
"node-machine-id": "^1.1.12",
"os-browserify": "^0.3.0",
"os-name": "^6.0.0",
"path-browserify": "^1.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import logger from 'electron-log/main';
import ElectronSquirrelStartup from 'electron-squirrel-startup';
import * as Sentry from '@sentry/electron/main';

import './services/analytics.js';
import './services/errors.js';
import './services/config.js';
import './services/csv.js';
Expand Down
7 changes: 6 additions & 1 deletion src/pages/Data.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function Data() {
const [error, setError] = useState();
const [validation, setValidation] = useState('idle');

const onDrop = async (files) => {
const onDrop = async (files, _, event) => {
if (!files) {
geocodeDispatch({
type: 'RESET',
Expand All @@ -40,6 +40,11 @@ export default function Data() {
return;
}

window.ugrc.trackEvent({
category: 'file-selection-type',
label: event.type === 'drop' ? 'drag-and-drop' : 'file-dialog',
});

const file = files[0];
setError();
let stats;
Expand Down
4 changes: 4 additions & 0 deletions src/pages/Error.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import * as Sentry from '@sentry/electron/renderer';

export default function Error({ error, children }) {
Sentry.captureException(error);
window.ugrc.trackEvent({
category: 'error',
label: 'unhandled error',
});

return (
<article>
Expand Down
2 changes: 2 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ contextBridge.exposeInMainWorld('ugrc', {
relaunchApp: () => ipcRenderer.send('relaunchApp'),
openIssue: (content) => ipcRenderer.send('openIssue', content),
openEmail: (content) => ipcRenderer.send('openEmail', content),

trackEvent: (content) => ipcRenderer.send('trackEvent', content),
});
25 changes: 25 additions & 0 deletions src/services/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ipcMain } from 'electron';
import machineId from 'node-machine-id';
import Analytics from 'electron-google-analytics4';

const { machineIdSync } = machineId;
const id = machineIdSync();
const visitor = new Analytics(process.env.GA4_ID, process.env.GA4_SECRET, id);

visitor.event({ ec: 'application-open', ea: id, el: process.platform });
visitor.event({ ec: 'client-version', ea: id, el: process.version });

//* main thread functions
export const trackEvent = ({ category, action = id, label }) => {
visitor.event({
ec: category,
ea: action,
el: label,
});
};

//* renderer events
ipcMain.on('trackEvent', (_, content) => {
console.log(content);
trackEvent(content);
});
6 changes: 6 additions & 0 deletions src/services/geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import log from 'electron-log/main';
import { parse } from 'csv-parse';
import { stringify } from 'csv-stringify';
import ky from 'ky';
import { trackEvent } from './analytics';
// import '../../tests/mocks/server';

const __filename = url.fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -80,6 +81,7 @@ export const checkApiKey = async (apiKey) => {

const isValid = response.status === 200;

trackEvent({ category: 'api-key-check', label: isValid });
console.log({ category: 'api-key-check', label: isValid });

return isValid;
Expand Down Expand Up @@ -112,6 +114,9 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s
},
};

trackEvent({ category: 'geocode', label: `${totalRows}, ${md5(filePath)}` });
trackEvent({ category: 'wkid', label: wkid });

for await (const record of parser) {
if (cancelled) {
log.warn('Geocoding stopping');
Expand Down Expand Up @@ -215,6 +220,7 @@ export const geocode = async (event, { filePath, fields, apiKey, wkid = 26912, s

if (failures === fastFailLimit && fastFailLimit === rowsProcessed) {
cancelGeocode('fail-fast');
trackEvent({ category: 'geocoding-cancelled', label: 'fast-fail' });
}

await coolYourJets();
Expand Down

0 comments on commit 6fbaab4

Please sign in to comment.