Skip to content

Commit 317982d

Browse files
authored
feat(matomoAnalytics)!: watch mode (#514)
1 parent ecada33 commit 317982d

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

docs/content/scripts/analytics/matomo-analytics.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use the [useScriptMatomoAnalytics](#useScriptMatomoAnalytics) composable.
1717

1818
## Loading Globally
1919

20-
The following config assumes you're using Matomo Cloud with the default `siteId` of `1`.
20+
The following config assumes you're using Matomo Cloud with the default `siteId` of `1`. Page views are **automatically tracked** on navigation by default.
2121

2222
If you're self-hosting, you'll need to provide the `matomoUrl` instead. If you have other sites you want to track, you can add them using `siteId`.
2323

@@ -84,13 +84,13 @@ const matomoAnalytics = useScriptMatomoAnalytics({
8484
})
8585
```
8686

87-
By default, a `siteId` of `1` is used and the page is not tracked. You can enable tracking by setting `trackPageView` to `true`.
87+
By default, a `siteId` of `1` is used and page tracking is **automatically enabled** via the `watch` option.
8888

8989
```ts
9090
const matomoAnalytics = useScriptMatomoAnalytics({
9191
cloudId: 'YOUR_CLOUD_ID', // e.g. nuxt.matomo.cloud
92-
trackPageView: true,
9392
siteId: 2,
93+
// watch: true, // enabled by default - automatic page tracking!
9494
})
9595
```
9696

@@ -109,6 +109,35 @@ proxy._paq.push(['trackPageView'])
109109

110110
Please see the [Config Schema](#config-schema) for all available options.
111111

112+
## Custom Page Tracking
113+
114+
By default, all pages are tracked automatically, to disable the automatic tracking you can provide `watch: false`.
115+
116+
```ts
117+
import { useScriptEventPage } from '#nuxt-scripts'
118+
119+
const { proxy } = useScriptMatomoAnalytics({
120+
cloudId: 'YOUR_CLOUD_ID',
121+
watch: false, // disable automatic tracking
122+
})
123+
124+
// Custom page tracking with additional logic
125+
useScriptEventPage((payload) => {
126+
// Set custom dimensions based on route
127+
if (payload.path.startsWith('/products')) {
128+
proxy._paq.push(['setCustomDimension', 1, 'Product Page'])
129+
}
130+
131+
// Standard Matomo tracking calls (same as built-in watch behavior)
132+
proxy._paq.push(['setDocumentTitle', payload.title])
133+
proxy._paq.push(['setCustomUrl', payload.path])
134+
proxy._paq.push(['trackPageView'])
135+
136+
// Track additional custom events
137+
proxy._paq.push(['trackEvent', 'Navigation', 'PageView', payload.path])
138+
})
139+
```
140+
112141
### Using Matomo Self-Hosted
113142

114143
For self-hosted Matomo, set `matomoUrl` to customize tracking, you may need to set the `trackerUrl` if you've customized this.
@@ -151,11 +180,13 @@ You must provide the options when setting up the script for the first time.
151180
// matomoUrl and site are required
152181
export const MatomoAnalyticsOptions = object({
153182
matomoUrl: optional(string()),
154-
siteId: optional(string()),
183+
siteId: optional(union([string(), number()])),
184+
cloudId: optional(string()),
155185
trackerUrl: optional(string()),
156-
trackPageView: optional(boolean()),
186+
trackPageView: optional(boolean()), // deprecated - use watch instead
157187
enableLinkTracking: optional(boolean()),
158188
disableCookies: optional(boolean()),
189+
watch: optional(boolean()), // default: true
159190
})
160191
```
161192

src/runtime/registry/matomo-analytics.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { withBase, withHttps, withoutProtocol, withoutTrailingSlash } from 'ufo'
22
import { useRegistryScript } from '../utils'
3+
import { useScriptEventPage } from '../composables/useScriptEventPage'
34
import { boolean, object, optional, string, number, union } from '#nuxt-scripts-validator'
45
import type { RegistryScriptInput } from '#nuxt-scripts/types'
6+
import { logger } from '../logger'
57

68
export const MatomoAnalyticsOptions = object({
79
matomoUrl: optional(string()),
@@ -11,6 +13,7 @@ export const MatomoAnalyticsOptions = object({
1113
trackPageView: optional(boolean()),
1214
enableLinkTracking: optional(boolean()),
1315
disableCookies: optional(boolean()),
16+
watch: optional(boolean()),
1417
})
1518

1619
export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false, false, false>
@@ -48,6 +51,7 @@ export function useScriptMatomoAnalytics<T extends MatomoAnalyticsApi>(_options?
4851
return window._paq.push(...args)
4952
},
5053
}
54+
5155
return { _paq: _paqProxy }
5256
},
5357
},
@@ -67,8 +71,21 @@ export function useScriptMatomoAnalytics<T extends MatomoAnalyticsApi>(_options?
6771
_paq.push(['setTrackerUrl', withBase(`/matomo.php`, withHttps(normalizedCloudId))])
6872
}
6973
_paq.push(['setSiteId', String(options?.siteId) || '1'])
70-
if (options?.trackPageView) {
71-
_paq.push(['trackPageView'])
74+
// Deprecated: trackPageView option
75+
if (options?.trackPageView !== undefined) {
76+
if (import.meta.dev) {
77+
logger.warn('The `trackPageView` option is deprecated. Use `watch: true` (default) for automatic page view tracking, or remove this option entirely.')
78+
}
79+
if (options.trackPageView) {
80+
_paq.push(['trackPageView'])
81+
}
82+
}
83+
else if (options?.watch !== false) {
84+
useScriptEventPage((payload) => {
85+
window._paq.push(['setDocumentTitle', payload.title])
86+
window._paq.push(['setCustomUrl', payload.path])
87+
window._paq.push(['trackPageView'])
88+
})
7289
}
7390
},
7491
}

0 commit comments

Comments
 (0)