Skip to content

Commit d7e18c4

Browse files
committed
fix(matomo): easier cloud config using cloudId
Fixes #279
1 parent 870e020 commit d7e18c4

File tree

4 files changed

+119
-48
lines changed

4 files changed

+119
-48
lines changed

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

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use the [useScriptMatomoAnalytics](#useScriptMatomoAnalytics) composable.
1717

1818
## Loading Globally
1919

20-
If you don't plan to send custom events you can use the [Environment overrides](https://nuxt.com/docs/getting-started/configuration#environment-overrides) to
21-
disable the script in development.
20+
The following config assumes you're using Matomo Cloud with the default `siteId` of `1`.
21+
22+
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`.
2223

2324
::code-group
2425

@@ -27,7 +28,7 @@ export default defineNuxtConfig({
2728
scripts: {
2829
registry: {
2930
matomoAnalytics: {
30-
siteId: 'YOUR_SITE_ID'
31+
cloudId: 'YOUR_CLOUD_ID', // e.g. nuxt.matomo.cloud
3132
}
3233
}
3334
}
@@ -40,7 +41,7 @@ export default defineNuxtConfig({
4041
scripts: {
4142
registry: {
4243
matomoAnalytics: {
43-
siteId: 'YOUR_SITE_ID',
44+
cloudId: 'YOUR_CLOUD_ID', // e.g. nuxt.matomo.cloud
4445
}
4546
}
4647
}
@@ -61,8 +62,8 @@ export default defineNuxtConfig({
6162
scripts: {
6263
matomoAnalytics: {
6364
// .env
64-
// NUXT_PUBLIC_SCRIPTS_MATOMO_ANALYTICS_SITE_ID=<your-id>
65-
siteId: '', // NUXT_PUBLIC_SCRIPTS_MATOMO_ANALYTICS_SITE_ID
65+
// NUXT_PUBLIC_SCRIPTS_MATOMO_ANALYTICS_CLOUD_ID=<your-id>
66+
cloudId: '', // NUXT_PUBLIC_SCRIPTS_MATOMO_ANALYTICS_CLOUD_ID
6667
},
6768
},
6869
},
@@ -76,16 +77,52 @@ export default defineNuxtConfig({
7677

7778
The `useScriptMatomoAnalytics` composable lets you have fine-grain control over when and how Matomo Analytics is loaded on your site.
7879

80+
81+
```ts
82+
const matomoAnalytics = useScriptMatomoAnalytics({
83+
cloudId: 'YOUR_CLOUD_ID', // e.g. nuxt.matomo.cloud
84+
})
85+
```
86+
87+
By default, a `siteId` of `1` is used and the page is not tracked. You can enable tracking by setting `trackPageView` to `true`.
88+
89+
```ts
90+
const matomoAnalytics = useScriptMatomoAnalytics({
91+
cloudId: 'YOUR_CLOUD_ID', // e.g. nuxt.matomo.cloud
92+
trackPageView: true,
93+
siteId: 2,
94+
})
95+
```
96+
97+
If you'd like more control over the tracking, for example to set a custom dimension, you can send events using the `proxy` object.
98+
99+
```ts
100+
const { proxy } = useScriptMatomoAnalytics({
101+
cloudId: 'YOUR_CLOUD_ID', // e.g. nuxt.matomo.cloud
102+
})
103+
104+
// set custom dimension
105+
proxy._paq.push(['setCustomDimension', 1, 'value'])
106+
// send page event
107+
proxy._paq.push(['trackPageView'])
108+
```
109+
110+
Please see the [Config Schema](#config-schema) for all available options.
111+
112+
### Using Matomo Self-Hosted
113+
114+
For self-hosted Matomo, set `matomoUrl` to customize tracking, you may need to set the `trackerUrl` if you've customized this.
115+
79116
```ts
80117
const matomoAnalytics = useScriptMatomoAnalytics({
81-
matomoUrl: 'YOUR_MATOMO_URL',
82-
siteId: 'YOUR_SITE_ID'
118+
// e.g. https://your-url.com/tracker.js & https://your-url.com//matomo.php both exists
119+
matomoUrl: 'https://your-url.com',
83120
})
84121
```
85122

86123
### Using Matomo Whitelabel
87124

88-
For Matomo Whitelabel, set trackerUrl and scriptInput.src to customize tracking.
125+
For Matomo Whitelabel, set `trackerUrl` and `scriptInput.src` to customize tracking.
89126

90127
```ts
91128
const matomoAnalytics = useScriptMatomoAnalytics({
@@ -113,8 +150,8 @@ You must provide the options when setting up the script for the first time.
113150
```ts
114151
// matomoUrl and site are required
115152
export const MatomoAnalyticsOptions = object({
116-
matomoUrl: string(),
117-
siteId: string(),
153+
matomoUrl: optional(string()),
154+
siteId: optional(string()),
118155
trackerUrl: optional(string()),
119156
trackPageView: optional(boolean()),
120157
enableLinkTracking: optional(boolean()),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts" setup>
2+
import { useHead } from '#imports'
3+
4+
useHead({
5+
title: 'Matomo Analytics',
6+
script: [
7+
{
8+
innerHTML: `var _paq = window._paq = window._paq || [];
9+
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
10+
_paq.push(['trackPageView']);
11+
_paq.push(['enableLinkTracking']);
12+
(function() {
13+
var u="https://nuxt.matomo.cloud/";
14+
_paq.push(['setTrackerUrl', u+'matomo.php']);
15+
_paq.push(['setSiteId', '1']);
16+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
17+
g.async=true; g.src='https://cdn.matomo.cloud/nuxt.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s);
18+
})();`,
19+
},
20+
],
21+
})
22+
</script>
23+
24+
<template>
25+
<div>
26+
matomo default
27+
</div>
28+
</template>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ useHead({
77
88
// composables return the underlying api as a proxy object and the script state
99
const { status, proxy } = useScriptMatomoAnalytics({
10-
matomoUrl: 'https://nuxt-scripts-demo.matomo.cloud',
10+
cloudId: 'nuxt.matomo.cloud',
1111
siteId: '1',
1212
})
1313
Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { withBase, withHttps } from 'ufo'
1+
import { withBase, withHttps, withoutProtocol, withoutTrailingSlash } from 'ufo'
22
import { useRegistryScript } from '../utils'
33
import { boolean, object, optional, string } from '#nuxt-scripts-validator'
44
import type { RegistryScriptInput } from '#nuxt-scripts'
55

66
export const MatomoAnalyticsOptions = object({
77
matomoUrl: optional(string()),
88
siteId: string(),
9+
cloudId: optional(string()),
910
trackerUrl: optional(string()),
1011
trackPageView: optional(boolean()),
1112
enableLinkTracking: optional(boolean()),
@@ -23,42 +24,47 @@ declare global {
2324
}
2425

2526
export function useScriptMatomoAnalytics<T extends MatomoAnalyticsApi>(_options?: MatomoAnalyticsInput) {
26-
return useRegistryScript<T, typeof MatomoAnalyticsOptions>('matomoAnalytics', options => ({
27-
scriptInput: {
28-
src: withBase(`/matomo.js`, withHttps(options?.matomoUrl || '')),
29-
crossorigin: false,
30-
},
31-
schema: import.meta.dev ? MatomoAnalyticsOptions : undefined,
32-
scriptOptions: {
33-
use() {
34-
return { _paq: window._paq }
27+
return useRegistryScript<T, typeof MatomoAnalyticsOptions>('matomoAnalytics', (options) => {
28+
const normalizedCloudId = options?.cloudId ? withoutTrailingSlash(withoutProtocol(options.cloudId)) : undefined
29+
const origin = options?.matomoUrl ? options.matomoUrl : `https://cdn.matomo.cloud/${normalizedCloudId}/`
30+
const _paq = import.meta.client ? (window._paq = window._paq || []) : []
31+
return {
32+
scriptInput: {
33+
src: withBase(`/matomo.js`, origin),
34+
crossorigin: false,
3535
},
36-
// allow _paq to be accessed on the server
37-
stub: import.meta.client
36+
schema: import.meta.dev ? MatomoAnalyticsOptions : undefined,
37+
scriptOptions: {
38+
use() {
39+
return { _paq: window._paq }
40+
},
41+
// allow _paq to be accessed on the server
42+
stub: import.meta.client
43+
? undefined
44+
: ({ fn }) => {
45+
return fn === '_paq' ? [] : undefined
46+
},
47+
},
48+
clientInit: import.meta.server
3849
? undefined
39-
: ({ fn }) => {
40-
return fn === '_paq' ? [] : undefined
50+
: () => {
51+
if (options?.enableLinkTracking) {
52+
_paq.push(['enableLinkTracking'])
53+
}
54+
if (options?.disableCookies) {
55+
_paq.push(['disableCookies'])
56+
}
57+
if (options?.trackerUrl || options?.matomoUrl) {
58+
_paq.push(['setTrackerUrl', options?.trackerUrl ? withHttps(options.trackerUrl) : withBase(`/matomo.php`, withHttps(options?.matomoUrl || ''))])
59+
}
60+
else if (normalizedCloudId) {
61+
_paq.push(['setTrackerUrl', withBase(`/matomo.php`, withHttps(normalizedCloudId))])
62+
}
63+
_paq.push(['setSiteId', String(options?.siteId) || '1'])
64+
if (options?.trackPageView) {
65+
_paq.push(['trackPageView'])
66+
}
4167
},
42-
},
43-
clientInit: import.meta.server
44-
? undefined
45-
: () => {
46-
const _paq = window._paq = window._paq || []
47-
if (options?.trackPageView) {
48-
_paq.push(['trackPageView'])
49-
}
50-
if (options?.enableLinkTracking) {
51-
_paq.push(['enableLinkTracking'])
52-
}
53-
54-
if (options?.disableCookies) {
55-
_paq.push(['disableCookies'])
56-
}
57-
58-
if (options?.trackerUrl || options?.matomoUrl) {
59-
_paq.push(['setTrackerUrl', options?.trackerUrl ? withHttps(options.trackerUrl) : withBase(`/matomo.php`, withHttps(options?.matomoUrl || ''))])
60-
}
61-
_paq.push(['setSiteId', options?.siteId || '1'])
62-
},
63-
}), _options)
68+
}
69+
}, _options)
6470
}

0 commit comments

Comments
 (0)