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 1 commit
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
21 changes: 20 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,23 @@ const matomoAnalytics = useScriptMatomoAnalytics({
})
```

or custom matomo whitelabel for example [Cookie3](https://docs.cookie3.co/cookie3-docs/integrations/website-integration/cookie3-script/nuxt):
reslear marked this conversation as resolved.
Show resolved Hide resolved

```ts
const cookie3Options = {
reslear marked this conversation as resolved.
Show resolved Hide resolved
siteId: YOUR_SITE_ID,
additionalTracking: true,
cookielessEnabled: true,
}

const matomoAnalytics = useScriptMatomoAnalytics({
setTrackerUrl: 'https://c.staging.cookie3.co/lake',
reslear marked this conversation as resolved.
Show resolved Hide resolved
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 +117,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
20 changes: 16 additions & 4 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
siteId: string(),
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: optional(string()),
trackerUrl: optional(string()),
trackPageView: optional(boolean()),
enableLinkTracking: optional(boolean()),
disableCookies: optional(boolean()),
})

export type MatomoAnalyticsInput = RegistryScriptInput<typeof MatomoAnalyticsOptions, false, false, false>
Expand Down Expand Up @@ -48,8 +50,18 @@ export function useScriptMatomoAnalytics<T extends MatomoAnalyticsApi>(_options?
if (options?.enableLinkTracking) {
_paq.push(['enableLinkTracking'])
}
_paq.push(['setTrackerUrl', withBase(`/matomo.php`, withHttps(options?.matomoUrl))])
_paq.push(['setSiteId', options?.siteId || '1'])

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

if(options?.trackerUrl || options?.matomoUrl) {
_paq.push(['setTrackerUrl', withHttps(options.trackerUrl) ?? withBase(`/matomo.php`, withHttps(options.matomoUrl))])
}

if(options?.siteId) {
reslear marked this conversation as resolved.
Show resolved Hide resolved
_paq.push(['setSiteId', options?.siteId])
}
},
}), _options)
}