Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(matomo): support custom tracker urls #236

Merged
merged 8 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion docs/content/scripts/analytics/matomo-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ const matomoAnalytics = useScriptMatomoAnalytics({
})
```

### Using Matomo Whitelabel

For Matomo Whitelabel, set trackerUrl and scriptInput.src to customize tracking.

```ts
const matomoAnalytics = useScriptMatomoAnalytics({
trackerUrl: 'https://c.staging.cookie3.co/lake',
scriptInput: {
src: 'https://cdn.cookie3.co/scripts/analytics/latest/cookie3.analytics.min.js',
},
})
```

Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage.

### MatomoAnalyticsApi
Expand All @@ -100,10 +113,12 @@ You must provide the options when setting up the script for the first time.
```ts
// matomoUrl and site are required
export const MatomoAnalyticsOptions = object({
matomoUrl: string(),
matomoUrl: string(),
siteId: string(),
trackerUrl: optional(string()),
trackPageView: optional(boolean()),
enableLinkTracking: optional(boolean()),
disableCookies: optional(boolean()),
})
```

Expand Down
15 changes: 12 additions & 3 deletions src/runtime/registry/matomo-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { boolean, object, optional, string } from '#nuxt-scripts-validator'
import type { RegistryScriptInput } from '#nuxt-scripts'

export const MatomoAnalyticsOptions = object({
matomoUrl: string(), // site is required
matomoUrl: optional(string()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm maybe we can deprecate this in favour if trackerUrl

Copy link
Contributor Author

@reslear reslear Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably, because in the official documentation this name is used as an environment variable - apparently as syntax sugar

var u="//{$MATOMO_URL}/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);

https://developer.matomo.org/guides/tracking-javascript-guide

I just don't know your policy on major releases so I left it for backwards compatibility. If it's not important, I'd remove it in favor of proper names and add full url's to doc.

siteId: string(),
trackerUrl: optional(string()),
trackPageView: optional(boolean()),
enableLinkTracking: optional(boolean()),
disableCookies: optional(boolean()),
})

export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false, false, false>
Expand All @@ -23,7 +25,7 @@ declare global {
export function useScriptMatomoAnalytics<T extends MatomoAnalyticsApi>(_options?: MatomoAnalyticsInput) {
return useRegistryScript<T, typeof MatomoAnalyticsOptions>('matomoAnalytics', options => ({
scriptInput: {
src: withBase(`/matomo.js`, withHttps(options?.matomoUrl)),
src: withBase(`/matomo.js`, withHttps(options?.matomoUrl || '')),
crossorigin: false,
},
schema: import.meta.dev ? MatomoAnalyticsOptions : undefined,
Expand All @@ -48,7 +50,14 @@ export function useScriptMatomoAnalytics<T extends MatomoAnalyticsApi>(_options?
if (options?.enableLinkTracking) {
_paq.push(['enableLinkTracking'])
}
_paq.push(['setTrackerUrl', withBase(`/matomo.php`, withHttps(options?.matomoUrl))])

if (options?.disableCookies) {
_paq.push(['disableCookies'])
}

if (options?.trackerUrl || options?.matomoUrl) {
_paq.push(['setTrackerUrl', options?.trackerUrl ? withHttps(options.trackerUrl) : withBase(`/matomo.php`, withHttps(options?.matomoUrl || ''))])
}
_paq.push(['setSiteId', options?.siteId || '1'])
},
}), _options)
Expand Down