Skip to content

Commit 216c69c

Browse files
committed
feat: Desktop add google measurement protocol
1 parent 74e3888 commit 216c69c

6 files changed

Lines changed: 130 additions & 3 deletions

File tree

chat2db-client/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010

1111

1212
/release
13-
/static
13+
/static
14+
/versions

chat2db-client/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
"copy-to-clipboard": "^3.3.3",
3232
"echarts": "^5.4.2",
3333
"echarts-for-react": "^3.0.2",
34+
"electron-log": "^4.4.8",
3435
"event-source-polyfill": "^1.0.31",
3536
"lodash": "^4.17.21",
3637
"markdown-it-link-attributes": "^4.0.1",
3738
"monaco-editor": "^0.34.0",
3839
"monaco-editor-esm-webpack-plugin": "^2.1.0",
3940
"monaco-editor-webpack-plugin": "^7.0.1",
41+
"node-machine-id": "^1.1.12",
4042
"react-sortablejs": "^6.1.4",
4143
"sql-formatter": "^12.2.1",
4244
"styled-components": "^6.0.1",
@@ -79,11 +81,13 @@
7981
"asar": false,
8082
"files": [
8183
"dist/**/*",
82-
"!node_modules",
8384
"static/",
8485
"src/main",
8586
"versions/**/*",
86-
"package.json"
87+
"package.json",
88+
"!node_modules/**/*",
89+
"node_modules/uuid",
90+
"node_modules/node-machine-id"
8791
],
8892
"nsis": {
8993
"oneClick": false,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const os = require('os');
2+
const { readVersion } = require('./utils');
3+
const Analytics4 = require('./ga4');
4+
const log = require('electron-log');
5+
6+
function registerAnalytics() {
7+
const analytics = new Analytics4('G-V8M4E5SF61', 'LShbzC_vRka5Sw5AWco7Tw');
8+
const customParams = {
9+
platform: 'DESKTOP',
10+
version: readVersion(),
11+
os: os.platform(),
12+
};
13+
analytics.setParams(customParams).event('first_enter');
14+
}
15+
16+
module.exports = registerAnalytics;

chat2db-client/src/main/ga4.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const { net } = require('electron');
2+
const { v4: uuidv4 } = require('uuid');
3+
const { machineIdSync } = require('node-machine-id');
4+
const log = require('electron-log');
5+
6+
class Analytics4 {
7+
constructor(trackingID, secretKey, clientID = machineIdSync(), sessionID = uuidv4()) {
8+
this.trackingID = trackingID;
9+
this.secretKey = secretKey;
10+
this.clientID = clientID;
11+
this.sessionID = sessionID;
12+
this.customParams = {};
13+
this.userProperties = null;
14+
this.baseURL = 'https://google-analytics.com/mp';
15+
this.collectURL = '/collect';
16+
}
17+
18+
set(key, value) {
19+
if (value !== null) {
20+
this.customParams[key] = value;
21+
} else {
22+
delete this.customParams[key];
23+
}
24+
return this;
25+
}
26+
27+
setParams(params) {
28+
if (typeof params === 'object' && Object.keys(params).length > 0) {
29+
Object.assign(this.customParams, params);
30+
} else {
31+
this.customParams = {};
32+
}
33+
return this;
34+
}
35+
36+
setUserProperties(upValue) {
37+
if (typeof upValue === 'object' && Object.keys(upValue).length > 0) {
38+
this.userProperties = upValue;
39+
} else {
40+
this.userProperties = null;
41+
}
42+
return this;
43+
}
44+
45+
event(eventName) {
46+
const payload = {
47+
client_id: this.clientID,
48+
events: [
49+
{
50+
name: eventName,
51+
params: {
52+
session_id: this.sessionID,
53+
...this.customParams,
54+
},
55+
},
56+
],
57+
};
58+
59+
if (this.userProperties) {
60+
Object.assign(payload, { user_properties: this.userProperties });
61+
}
62+
63+
const url = `${this.baseURL}${this.collectURL}?measurement_id=${this.trackingID}&api_secret=${this.secretKey}`;
64+
const request = net.request({
65+
method: 'POST',
66+
url,
67+
});
68+
69+
request.on('response', (response) => {
70+
let responseData = '';
71+
response.on('data', (chunk) => {
72+
responseData += chunk;
73+
});
74+
75+
response.on('end', () => {
76+
if (response.statusCode >= 200 && response.statusCode < 300) {
77+
log.info('success', responseData);
78+
} else {
79+
log.error('response error', response.statusCode);
80+
}
81+
});
82+
});
83+
84+
request.on('error', (error) => {
85+
log.error('Error posting data:', error);
86+
});
87+
88+
request.write(JSON.stringify(payload));
89+
90+
request.end();
91+
}
92+
}
93+
94+
module.exports = Analytics4;

chat2db-client/src/main/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const os = require('os');
55
const fs = require('fs');
66
const registerAppMenu = require('./menu');
7+
const registerAnalysis = require('./analysis');
78
const i18n = require('./i18n');
89
const { loadMainResource } = require('./utils');
910

@@ -52,6 +53,7 @@ app.commandLine.appendSwitch('--disable-gpu-sandbox');
5253
app.on('ready', () => {
5354
createWindow();
5455
registerAppMenu();
56+
registerAnalysis();
5557

5658
app.on('activate', function () {
5759
if (mainWindow === null) {

chat2db-client/yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4695,6 +4695,11 @@ electron-localshortcut@^3.1.0:
46954695
keyboardevent-from-electron-accelerator "^2.0.0"
46964696
keyboardevents-areequal "^0.2.1"
46974697

4698+
electron-log@^4.4.8:
4699+
version "4.4.8"
4700+
resolved "https://registry.npmmirror.com/electron-log/-/electron-log-4.4.8.tgz#fcb9f714dbcaefb6ac7984c4683912c74730248a"
4701+
integrity sha512-QQ4GvrXO+HkgqqEOYbi+DHL7hj5JM+nHi/j+qrN9zeeXVKy8ZABgbu4CnG+BBqDZ2+tbeq9tUC4DZfIWFU5AZA==
4702+
46984703
electron-osx-sign@^0.6.0:
46994704
version "0.6.0"
47004705
resolved "https://registry.npmmirror.com/electron-osx-sign/-/electron-osx-sign-0.6.0.tgz#9b69c191d471d9458ef5b1e4fdd52baa059f1bb8"
@@ -6944,6 +6949,11 @@ node-libs-browser@2.2.1:
69446949
util "^0.11.0"
69456950
vm-browserify "^1.0.1"
69466951

6952+
node-machine-id@^1.1.12:
6953+
version "1.1.12"
6954+
resolved "https://registry.npmmirror.com/node-machine-id/-/node-machine-id-1.1.12.tgz#37904eee1e59b320bb9c5d6c0a59f3b469cb6267"
6955+
integrity sha512-QNABxbrPa3qEIfrE6GOJ7BYIuignnJw7iQ2YPbc3Nla1HzRJjXzZOiikfF8m7eAMfichLt3M4VgLOetqgDmgGQ==
6956+
69476957
node-releases@^2.0.12:
69486958
version "2.0.13"
69496959
resolved "https://registry.npmmirror.com/node-releases/-/node-releases-2.0.13.tgz#d5ed1627c23e3461e819b02e57b75e4899b1c81d"

0 commit comments

Comments
 (0)