Skip to content

Commit d38b2a4

Browse files
committed
✨ Inject Googlge analytics
1 parent 8442c7c commit d38b2a4

File tree

10 files changed

+41
-20
lines changed

10 files changed

+41
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ docker run -d -p 8888:8888 -v "$(pwd)/data:/app/data" nicejade/wealth-tracker:la
119119
- `ALLOW_PASSWORD`: 设置为 `true` 启用密码保护功能;
120120
- `PEPPER_SECRET`: 设置它为用户密码提供更强大的保护;
121121
- `CAN_BE_RESET`: 设置为 `true` 允许重置数据库功能;
122+
- `GOOGLE_ANALYTICS_KEY`: Google Analytics 衡量 ID (如 `G-XXXXX`),配置后将开启页面访问与关键行为埋点(需在构建客户端时注入,新建 根据 `.env.example` 新建 `.env`,填入 ID)。
122123

123124
```bash
124125
docker run -d -p 8888:8888 \

client/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Google Analytics Measurement ID (e.g., G-XXXXXXXXXX)
2+
# If configured, the app will enable Google Analytics tracking
3+
GOOGLE_ANALYTICS_KEY='G-XXXXXXXXXX'

client/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@
108108
"url": "https://www.jeffjade.com/"
109109
},
110110
"datePublished": "2024-01-01",
111-
"dateModified": "2024-12-19"
111+
"dateModified": "2026-01-29"
112112
}
113113
</script>
114114
<!-- Google Analytics (Conditional) -->
115115
<script>
116116
(function () {
117-
const GA_KEY = '%VITE_GOOGLE_ANALYTICS_KEY%'
118-
if (GA_KEY && GA_KEY !== '%VITE_GOOGLE_ANALYTICS_KEY%') {
117+
const GA_KEY = '%GOOGLE_ANALYTICS_KEY%'
118+
if (GA_KEY) {
119119
const script = document.createElement('script')
120120
script.async = true
121121
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + GA_KEY

client/src/components/CustomCurrencyManager.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@
9090
9191
if (editingId) {
9292
response = await updateCustomCurrency(editingId, editingCurrency)
93-
trackEvent('custom_currency_update', { code: editingCurrency.code })
93+
trackEvent('custom-currency-update', { code: editingCurrency.code })
9494
} else {
9595
response = await createCustomCurrency(editingCurrency)
96-
trackEvent('custom_currency_create', { code: editingCurrency.code })
96+
trackEvent('custom-currency-create', { code: editingCurrency.code })
9797
}
9898
9999
if (response.success) {
@@ -129,7 +129,7 @@
129129
loading = true
130130
const response: any = await deleteCustomCurrency(deleteId)
131131
if (response.success) {
132-
trackEvent('custom_currency_delete')
132+
trackEvent('custom-currency-delete')
133133
await loadCurrencies()
134134
// 重新获取汇率
135135
await fetchExchangeRates()

client/src/components/Header.svelte

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { locale } from 'svelte-i18n'
55
import SvgIcon from './SvgIcon.svelte'
66
import { language, theme } from '../stores'
7+
import { trackEvent } from '../helper/analytics'
78
import { saveUserSettings } from './../helper/settings'
89
import { TITLE, DEFAULT_THEME, LANG_ARR } from './../helper/constant'
910
@@ -96,6 +97,7 @@
9697
const onToggleTheme = async () => {
9798
const newTheme = $theme === DEFAULT_THEME ? 'dark' : DEFAULT_THEME
9899
theme.set(newTheme)
100+
trackEvent('theme-toggle', { theme: newTheme })
99101
// 保存到服务器(响应式更新会自动触发,但这里显式调用以确保立即保存)
100102
await saveUserSettings({ theme: newTheme }).catch((err) => {
101103
console.error('Failed to save theme:', err)
@@ -106,6 +108,7 @@
106108
lang = item.value
107109
language.set(item.value)
108110
updateUrlLang(item.value)
111+
trackEvent('language-change', { language: item.value })
109112
110113
await saveUserSettings({ language: item.value }).catch((err) => {
111114
console.error('Failed to save language:', err)
@@ -115,7 +118,11 @@
115118

116119
<header class="flex h-20 items-center justify-between" style="z-index: 1000;">
117120
<h1 class="h-full leading-none">
118-
<a href="/" title={TITLE} class="flex h-full items-center space-x-2 focus-visible:outline-none">
121+
<a
122+
href="/"
123+
title={TITLE}
124+
class="flex h-full items-center space-x-2 focus-visible:outline-none"
125+
on:click={() => trackEvent('logo-click')}>
119126
<img src="/logo.png" alt="Sink" class="h-5 w-5" />
120127
<span class="title font-[cursive] text-xl font-semibold">{TITLE}</span>
121128
</a>
@@ -126,7 +133,8 @@
126133
rel="noopener"
127134
class="focus-visible-ring inline-flex rounded-md p-2 leading-5 outline-none hover:bg-gray-100"
128135
href="https://x.com/intent/user?screen_name=MarshalXuan"
129-
title="X | Twitter Follow MarshalXuan">
136+
title="X | Twitter Follow MarshalXuan"
137+
on:click={() => trackEvent('social-link-click', { platform: 'x' })}>
130138
<SvgIcon name="x" width={20} height={20} color="#212121" />
131139
</a>
132140
<button
@@ -169,7 +177,8 @@
169177
title="Github"
170178
class="inline-flex w-full items-center rounded-full bg-gray-900 px-6 py-3 text-sm
171179
leading-4 text-white hover:bg-gray-800 hover:text-white focus:outline-none focus:ring-0
172-
focus:ring-offset-2 md:hidden md:w-auto md:rounded-full md:px-3 md:focus:ring-2">
180+
focus:ring-offset-2 md:hidden md:w-auto md:rounded-full md:px-3 md:focus:ring-2"
181+
on:click={() => trackEvent('social-link-click', { platform: 'github' })}>
173182
<SvgIcon name="githubx" width={20} height={20} color="#ffffff" />
174183
<strong class="ml-2 font-semibold">GitHub</strong>
175184
</a>

client/src/components/Modal/Update.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,21 @@
133133
}
134134
if (action === ACTION_TYPES.create) {
135135
await createAssets(items)
136-
trackEvent('asset_create', {
136+
trackEvent('asset-create', {
137137
asset_type: items.type,
138138
currency: items.currency,
139139
})
140140
}
141141
if (action === ACTION_TYPES.update) {
142142
await updateAssets(items)
143-
trackEvent('asset_update', {
143+
trackEvent('asset-update', {
144144
asset_type: items.type,
145145
currency: items.currency,
146146
})
147147
}
148148
if (action === ACTION_TYPES.change) {
149149
await updateRecords(items)
150-
trackEvent('record_update', {
150+
trackEvent('record-update', {
151151
asset_type: items.type,
152152
currency: items.currency,
153153
})

client/src/components/Wysiwyg.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
exportMarkdown: () => {
254254
const markdown = editor?.storage.markdown.getMarkdown()
255255
if (markdown) {
256-
trackEvent('insight_export_markdown')
256+
trackEvent('insight-export-markdown')
257257
// Create a downloadable file
258258
const blob = new Blob([markdown], { type: 'text/markdown' })
259259
const url = URL.createObjectURL(blob)

client/src/routes/Advice.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
182182
const onGenAdviceClick = async () => {
183183
try {
184-
trackEvent('ai_advice_request', {
184+
trackEvent('ai-advice-request', {
185185
model: settings.model,
186186
})
187187
handleGptStream({ prompt, settings })

client/src/routes/Index.svelte

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,31 @@
8383
currentAssetItem.type = Date.now().toString()
8484
updateActionType = ACTION_TYPES.create
8585
isShowUpdateModal = true
86+
trackEvent('asset-add-click')
8687
}
8788
8889
const handleUpate = (event) => {
8990
currentAssetItem = event.detail
9091
updateActionType = ACTION_TYPES.update
9192
isShowUpdateModal = true
93+
trackEvent('asset-update-click', { asset_type: currentAssetItem.type })
9294
}
9395
9496
const handleDestroy = (event) => {
9597
const { type } = event.detail
9698
typeToBeDestroyed = type
9799
isShowComfirmModal = true
100+
trackEvent('asset-delete-click', { asset_type: type })
98101
}
99102
100103
const handleReset = () => {
101104
isShowResetModal = true
105+
trackEvent('database-reset-click')
102106
}
103107
104108
const handleUpdateConfirm = () => {
105109
fetchDatabase()
110+
trackEvent('asset-update-confirm', { action_type: updateActionType })
106111
}
107112
108113
const handleUpdateClose = () => {
@@ -112,7 +117,7 @@
112117
const handleComfirm = async () => {
113118
try {
114119
await destroyAssets({ type: typeToBeDestroyed })
115-
trackEvent('asset_delete', { asset_type: typeToBeDestroyed })
120+
trackEvent('asset-delete', { asset_type: typeToBeDestroyed })
116121
fetchDatabase()
117122
isShowComfirmModal = false
118123
} catch (error) {
@@ -127,7 +132,7 @@
127132
const handleResetComfirm = async () => {
128133
try {
129134
await resetDatabase()
130-
trackEvent('database_reset')
135+
trackEvent('database-reset')
131136
isShowResetModal = false
132137
window.location.reload()
133138
} catch (error) {
@@ -244,7 +249,10 @@
244249
<h2 class="gradient-text typing-text my-4 text-xl text-white">
245250
{$_('getAIAdviceDescription')}
246251
</h2>
247-
<a href="/advice" class="regular-btn hover:text-brand inline-block text-center text-base">
252+
<a
253+
href="/advice"
254+
class="regular-btn hover:text-brand inline-block text-center text-base"
255+
on:click={() => trackEvent('get-ai-advice-click')}>
248256
{$_('getAIAdvice')}
249257
</a>
250258
</div>

client/src/routes/Insights.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@
174174
175175
if (editingInsight) {
176176
await updateInsights({ ...data, id: editingInsight.id })
177-
trackEvent('insight_update')
177+
trackEvent('insight-update')
178178
notice.set($_('insights.updateSuccess'))
179179
} else {
180180
await createInsights(data)
181-
trackEvent('insight_create')
181+
trackEvent('insight-create')
182182
notice.set($_('insights.saveSuccess'))
183183
}
184184
@@ -210,7 +210,7 @@
210210
const confirmDelete = async () => {
211211
try {
212212
await destroyInsights({ id: deletingInsightId })
213-
trackEvent('insight_delete')
213+
trackEvent('insight-delete')
214214
notice.set($_('insights.deleteSuccess'))
215215
isShowDeleteModal = false
216216
deletingInsightId = null

0 commit comments

Comments
 (0)