网页优化

This commit is contained in:
tianmo committed 2026-10-31 15:26:31 +08:00
1 parent ab32146e8d
commit 753e3b1e21
47 files changed
+187 -56

No files matched your search

+19 -1
View File
@@ -1,7 +1,7 @@
import { i18n, SUPPORTED_LOCALES } from '../i18n'
type MatomoWindow = Window & { _paq?: unknown[][] }
type EventCategory = '时间轴' | '系统设置' | '属性面板' | '使用说明'
type EventCategory = '欢迎页' | '页面导航' | '时间轴' | '系统设置' | '属性面板' | '使用说明'
type EventAction = '点击按钮' | '点击开关' | '调整数值' | '点击下载'
type MessageTree = Record<string, unknown>
@@ -36,6 +36,7 @@ function localizedToChinese(value: string | null | undefined) {
function categoryFor(element: Element): EventCategory {
const section = element.closest<HTMLElement>('[data-matomo-section]')?.dataset.matomoSection
if (section === 'welcome') return '欢迎页'
if (section === 'timeline') return '时间轴'
if (section === 'settings') return '系统设置'
return '属性面板'
@@ -78,6 +79,16 @@ export function trackMatomoEvent(category: EventCategory, action: EventAction, n
;(target._paq ||= []).push(['trackEvent', category, action, name])
}
export function trackMatomoPageView() {
if (!import.meta.env.PROD) return
const target = window as MatomoWindow
;(target._paq ||= []).push(
['setCustomUrl', window.location.href],
['setDocumentTitle', document.title],
['trackPageView'],
)
}
export function installMatomoEventTracking() {
if (!import.meta.env.PROD) return
document.addEventListener('click', (event) => {
@@ -92,6 +103,13 @@ export function installMatomoEventTracking() {
)
return
}
const tracked = target.closest<HTMLElement>('[data-matomo-event]')
if (tracked) {
const category = tracked.dataset.matomoCategory as EventCategory | undefined
const action = tracked.dataset.matomoAction as EventAction | undefined
trackMatomoEvent(category || categoryFor(tracked), action || '点击按钮', tracked.dataset.matomoEvent || '')
return
}
const button = target.closest<HTMLButtonElement>('button')
if (!button) return
trackMatomoEvent(categoryFor(button), '点击按钮', buttonName(button))