Skip to content

Commit ed87c47

Browse files
committed
feat: add new GA track module
1 parent 7d5ba42 commit ed87c47

17 files changed

Lines changed: 345 additions & 124 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
run: npm run build
5353
env:
5454
GA_TC: ${{ secrets.GA_TC }}
55+
GA_MP: ${{ secrets.GA_MP }}
5556
- name: Build for ${{ matrix.platform }}-${{ matrix.arch }}
5657
run: npm run pack
5758
env:

package-lock.json

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@
190190
"prettier": "^3.6.2",
191191
"react": "~16.8.6",
192192
"react-dom": "~16.8.6",
193-
"react-ga4": "^2.1.0",
194193
"react-monaco-editor": "^0.59.0",
195194
"redux-logger": "^3.0.6",
196195
"run-electron": "^2.0.0",

src/main/main.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { getPath } from './utils/path';
1919
import { PathKey } from 'common/configs/paths';
2020
import { ElectronDownloadManager } from 'electron-dl-manager';
2121
import { IPCDownloadItemStatus } from 'common/typings/ipc';
22-
import { appConf } from './utils/store';
23-
import { v4 as uuidv4 } from 'uuid';
22+
import track from './utils/track';
23+
import { setupGracefulExit } from './utils/graceful-exit';
2424

2525
logMain.info('[app.start]', process.env.NODE_ENV);
2626
logMain.info('[app.info]', app.getVersion(), currentPlatformArch, {
@@ -224,10 +224,31 @@ const menuTemplate: MenuItem[] = [
224224
{
225225
label: '打开日志目录',
226226
click: (_item, focusedWindow) => {
227-
// track.event('app', 'openLogDir');
227+
track.event('app', 'openLogDir');
228228
openLogDir();
229229
},
230230
},
231+
{
232+
label: '切换开发者工具',
233+
accelerator: (() => {
234+
if (process.platform === 'darwin') {
235+
return 'Alt+Command+I';
236+
} else {
237+
return 'Ctrl+Shift+I';
238+
}
239+
})(),
240+
click: (_item, _focusedWindow) => {
241+
if (mainWindow) {
242+
if (mainWindow.webContents.isDevToolsOpened()) {
243+
mainWindow.webContents.closeDevTools();
244+
} else {
245+
mainWindow.webContents.openDevTools({
246+
mode: 'detach',
247+
});
248+
}
249+
}
250+
},
251+
},
231252
],
232253
},
233254
isMac
@@ -258,21 +279,21 @@ const menuTemplate: MenuItem[] = [
258279
{
259280
label: 'Algo Bootstrap 官方网站',
260281
click: () => {
261-
// track.event('app', 'openSite');
282+
track.event('app', 'openSite');
262283
shell.openExternal(constants.site);
263284
},
264285
},
265286
{
266287
label: '使用文档',
267288
click: () => {
268-
// track.event('app', 'openDocs');
289+
track.event('app', 'openDocs');
269290
shell.openExternal(constants.docs);
270291
},
271292
},
272293
{
273294
label: '加入 QQ 群聊',
274295
click: (_item, focusedWindow) => {
275-
// track.event('app', 'openQQGroup');
296+
track.event('app', 'openQQGroup');
276297
const options = {
277298
type: 'info' as const,
278299
message: '加入 QQ 群聊',
@@ -284,7 +305,7 @@ const menuTemplate: MenuItem[] = [
284305
{
285306
label: '探索 algoUX 产品家族',
286307
click: () => {
287-
// track.event('app', 'openAlgoUX');
308+
track.event('app', 'openAlgoUX');
288309
shell.openExternal(constants.algoUXHomePage);
289310
},
290311
},
@@ -302,7 +323,7 @@ function addUpdateMenuItems(items, position) {
302323
label: '检查更新',
303324
key: 'checkForUpdate',
304325
click: () => {
305-
// track.event('app', 'checkUpdate');
326+
track.event('app', 'checkUpdate');
306327
checkUpdate();
307328
},
308329
},
@@ -398,19 +419,15 @@ if (!gotTheLock) {
398419
copyright: 'Copyright © 2019-present algoUX',
399420
});
400421

401-
let uid = appConf.get('uid');
402-
if (!uid) {
403-
uid = appConf.get('uid') || uuidv4();
404-
appConf.set('uid', uid);
405-
}
406-
407422
try {
408423
const ua = session.defaultSession.getUserAgent() + ` AlgoBootstrap/${app.getVersion()}`;
409424
session.defaultSession.setUserAgent(ua);
410425
} catch (e) {
411426
logMain.error('[setUserAgent] error:', e);
412427
}
413428

429+
track.init();
430+
414431
// 初始化 remote 模块
415432
initialize();
416433

@@ -466,6 +483,12 @@ app.on('activate', () => {
466483
}
467484
});
468485

486+
async function cleanup() {
487+
await track.beforeExit();
488+
}
489+
490+
setupGracefulExit(cleanup);
491+
469492
app.on('quit', () => {
470493
logMain.info('[app.quit]');
471494
});
@@ -742,4 +765,8 @@ async function openLogDir() {
742765
}
743766
}
744767

745-
// track.event('app', 'start', app.getVersion(), 1);
768+
track.event('app', 'start', app.getVersion(), 1);
769+
770+
process.on('uncaughtException', (err) => {
771+
logMain.error('[uncaughtException]', err);
772+
});

src/main/modules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as envChecker from './services/env-checker';
55
import * as envInstaller from './services/env-installer';
66
import * as vsc from './services/vsc';
77
import * as resources from './services/resources';
8+
import track from './utils/track';
89
import { getEnvComponents } from 'common/configs/env';
910
import { appConf } from './utils/store';
1011
import * as appService from './services/app';
@@ -20,6 +21,7 @@ const _modules = {
2021
platform,
2122
vsc,
2223
appService,
24+
track,
2325
resources,
2426
appConf,
2527
};

src/main/utils/graceful-exit.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { app } from 'electron';
2+
import { logMain } from './logger';
3+
4+
export function setupGracefulExit(asyncCleanup: Function) {
5+
if (typeof asyncCleanup !== 'function') {
6+
throw new TypeError('setupGracefulExit requires an async function');
7+
}
8+
9+
let quitting = false;
10+
11+
async function cleanupAndQuit(reason: string) {
12+
if (quitting) return;
13+
quitting = true;
14+
15+
logMain.info(`[graceful-exit] preparing to quit (${reason})...`);
16+
try {
17+
await Promise.resolve(asyncCleanup(reason));
18+
} catch (err) {
19+
logMain.error('[graceful-exit] cleanup failed:', err);
20+
} finally {
21+
logMain.info('[graceful-exit] cleanup complete, quitting now');
22+
app.quit();
23+
}
24+
}
25+
26+
app.on('before-quit', (e) => {
27+
if (!quitting) {
28+
e.preventDefault();
29+
cleanupAndQuit('before-quit');
30+
}
31+
});
32+
33+
process.on('SIGINT', () => cleanupAndQuit('SIGINT'));
34+
process.on('SIGTERM', () => cleanupAndQuit('SIGTERM'));
35+
}

0 commit comments

Comments
 (0)