加入追踪
This commit is contained in:
1 parent
859c78173a
commit
ca719ba1d4
17 files changed
+77
-47
No files matched your search
@@ -0,0 +1,23 @@
|
||||
type MatomoWindow = Window & { _paq?: unknown[][] }
|
||||
|
||||
export function trackMatomoEvent(eventName: string) {
|
||||
if (!import.meta.env.PROD) return
|
||||
const separator = eventName.indexOf('.')
|
||||
if (separator <= 0 || separator === eventName.length - 1) return
|
||||
const target = window as MatomoWindow
|
||||
;(target._paq ||= []).push([
|
||||
'trackEvent',
|
||||
eventName.slice(0, separator),
|
||||
eventName.slice(separator + 1),
|
||||
])
|
||||
}
|
||||
|
||||
export function installMatomoEventTracking() {
|
||||
if (!import.meta.env.PROD) return
|
||||
document.addEventListener('click', (event) => {
|
||||
const target = event.target
|
||||
if (!(target instanceof Element)) return
|
||||
const tracked = target.closest<HTMLElement>('[data-matomo-event]')
|
||||
if (tracked?.dataset.matomoEvent) trackMatomoEvent(tracked.dataset.matomoEvent)
|
||||
}, true)
|
||||
}
|
||||
Reference in new issue
Block a user