feat: add support for matomo analytics (#2051)
Some checks failed
Build and Test / build-and-test (ubuntu-latest) (push) Has been skipped
Build and Test / publish-tag (push) Has been skipped
Build and Test / build-and-test (macos-latest) (push) Has been cancelled
Build and Test / build-and-test (windows-latest) (push) Has been cancelled

* feat: Support Matomo as analytics provider (self-hosted)

* SPA support

* Don't use var

* inline

* Revert fixing issues outside of this change
This commit is contained in:
John Barker 2025-08-13 04:56:04 +01:00 committed by GitHub
parent 45c130b3dd
commit e918f11652
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 0 deletions

View File

@ -34,6 +34,7 @@ This part of the configuration concerns anything that can affect the whole site.
- `{ provider: 'tinylytics', siteId: '<your-site-id>' }`: use [Tinylytics](https://tinylytics.app/);
- `{ provider: 'cabin' }` or `{ provider: 'cabin', host: 'https://cabin.example.com' }` (custom domain): use [Cabin](https://withcabin.com);
- `{provider: 'clarity', projectId: '<your-clarity-id-code' }`: use [Microsoft clarity](https://clarity.microsoft.com/). The project id can be found on top of the overview page.
- `{ provider: 'matomo', siteId: '<your-matomo-id-code', host: 'matomo.example.com' }`: use [Matomo](https://matomo.org/), without protocol.
- `locale`: used for [[i18n]] and date formatting
- `baseUrl`: this is used for sitemaps and RSS feeds that require an absolute URL to know where the canonical 'home' of your site lives. This is normally the deployed URL of your site (e.g. `quartz.jzhao.xyz` for this site). Do not include the protocol (i.e. `https://`) or any leading or trailing slashes.
- This should also include the subpath if you are [[hosting]] on GitHub pages without a custom domain. For example, if my repository is `jackyzha0/quartz`, GitHub pages would deploy to `https://jackyzha0.github.io/quartz` and the `baseUrl` would be `jackyzha0.github.io/quartz`.

View File

@ -42,6 +42,11 @@ export type Analytics =
provider: "clarity"
projectId?: string
}
| {
provider: "matomo"
host: string
siteId: string
}
export interface GlobalConfiguration {
pageTitle: string

View File

@ -201,6 +201,33 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso
})(window, document, "clarity", "script", "${cfg.analytics.projectId}");\`
document.head.appendChild(clarityScript)
`)
} else if (cfg.analytics?.provider === "matomo") {
componentResources.afterDOMLoaded.push(`
const matomoScript = document.createElement("script");
matomoScript.innerHTML = \`
let _paq = window._paq = window._paq || [];
// Track SPA navigation
// https://developer.matomo.org/guides/spa-tracking
document.addEventListener("nav", () => {
_paq.push(['setCustomUrl', location.pathname]);
_paq.push(['setDocumentTitle', document.title]);
_paq.push(['trackPageView']);
});
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
const u="//${cfg.analytics.host}/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', ${cfg.analytics.siteId}]);
const d=document, g=d.createElement('script'), s=d.getElementsByTagName
('script')[0];
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
\`
document.head.appendChild(matomoScript);
`)
}
if (cfg.enableSPA) {