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

This commit is contained in:
John Barker 2025-07-18 22:37:32 +01:00
parent 059848f8b0
commit 1446d5d2cf
3 changed files with 29 additions and 1 deletions

View File

@ -33,7 +33,8 @@ This part of the configuration concerns anything that can affect the whole site.
- `{ provider: 'posthog', apiKey: '<your-posthog-project-apiKey>', host: '<your-posthog-host>' }`: use [Posthog](https://posthog.com/);
- `{ 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: '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,28 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso
})(window, document, "clarity", "script", "${cfg.analytics.projectId}");\`
document.head.appendChild(clarityScript)
`)
} else if (cfg.analytics?.provider === "matomo") {
const siteId = cfg.analytics.siteId
const matomoHost = cfg.analytics.host
componentResources.afterDOMLoaded.push(`
const matomoScript = document.createElement("script");
matomoScript.innerHTML = \`
var _paq = window._paq = window._paq || [];
var currentUrl = location.href;
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//${matomoHost}/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', ${siteId}]);
var 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) {