Skip to content

Commit 712a869

Browse files
committed
fix(matomo): broken event tracking
Fixes #322
1 parent 5a54cfd commit 712a869

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

playground/pages/third-parties/matomo-analytics/nuxt-scripts.vue

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,55 @@ useHead({
77
88
// composables return the underlying api as a proxy object and the script state
99
const { status, proxy } = useScriptMatomoAnalytics({
10-
cloudId: 'nuxt.matomo.cloud',
10+
matomoUrl: 'https://nuxt.matomo.cloud/',
1111
siteId: '1',
1212
scriptOptions: {
1313
trigger: 'onNuxtReady',
1414
},
1515
})
1616
17-
// Use proxy to track page view
17+
// Test the issue: Use proxy to track events and goals
18+
// Note: setTrackerUrl and setSiteId are automatically set by the registry based on matomoUrl and siteId options
1819
proxy._paq.push(['trackPageView'])
20+
21+
// Test custom events and goals that were not working in the original issue
22+
function trackCustomEvent() {
23+
proxy._paq.push(['trackEvent', 'Test', 'Button Click', 'Proxy Test'])
24+
}
25+
26+
function trackGoal() {
27+
proxy._paq.push(['trackGoal', 1])
28+
}
29+
30+
function testDirectPaq() {
31+
// Test direct window._paq usage for comparison
32+
if (window._paq) {
33+
window._paq.push(['trackEvent', 'Test', 'Button Click', 'Direct Test'])
34+
}
35+
}
1936
</script>
2037

2138
<template>
2239
<div>
40+
<h1>Matomo Analytics Test</h1>
2341
<ClientOnly>
2442
<div>
25-
status: {{ status }}
43+
<p>Script status: {{ status }}</p>
44+
45+
<div style="margin: 20px 0;">
46+
<UButton @click="trackCustomEvent" style="margin-right: 10px; padding: 10px;">
47+
Track Custom Event (proxy._paq)
48+
</UButton>
49+
<UButton @click="trackGoal" style="padding: 10px;">
50+
Track Goal (proxy._paq)
51+
</UButton>
52+
</div>
53+
54+
<div style="margin: 20px 0;">
55+
<UButton @click="testDirectPaq" style="margin-right: 10px; padding: 10px;">
56+
Track Event (direct window._paq)
57+
</UButton>
58+
</div>
2659
</div>
2760
</ClientOnly>
2861
</div>

src/runtime/registry/matomo-analytics.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@ export function useScriptMatomoAnalytics<T extends MatomoAnalyticsApi>(_options?
3636
schema: import.meta.dev ? MatomoAnalyticsOptions : undefined,
3737
scriptOptions: {
3838
use() {
39-
return { _paq: window._paq }
39+
// Ensure _paq is always available as a queue, even before script loads
40+
const _paqProxy = {
41+
push: (...args: any[]) => {
42+
// If _paq exists and has push method, use it directly
43+
if (window._paq && typeof window._paq.push === 'function') {
44+
return window._paq.push(...args)
45+
}
46+
// Otherwise, ensure _paq is initialized as an array and push to it
47+
window._paq = window._paq || []
48+
return window._paq.push(...args)
49+
},
50+
}
51+
return { _paq: _paqProxy }
4052
},
4153
},
4254
clientInit: import.meta.server

0 commit comments

Comments
 (0)