From f5d195369bfaefc5baefc2fc9184675f620e92d5 Mon Sep 17 00:00:00 2001 From: Master Date: Sat, 31 Aug 2024 23:28:06 +0200 Subject: [PATCH] feat: Matomo analytics support --- docs/configuration.md | 3 ++- quartz/cfg.ts | 5 ++++ quartz/plugins/emitters/componentResources.ts | 23 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index 1dc114816..4d6f8b2c3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -28,10 +28,11 @@ This part of the configuration concerns anything that can affect the whole site. - `{ provider: 'google', tagId: '' }`: use Google Analytics; - `{ provider: 'plausible' }` (managed) or `{ provider: 'plausible', host: '' }` (self-hosted): use [Plausible](https://plausible.io/); - `{ provider: 'umami', host: '', websiteId: '' }`: use [Umami](https://umami.is/); - - `{ provider: 'goatcounter', websiteId: 'my-goatcounter-id' }` (managed) or `{ provider: 'goatcounter', websiteId: 'my-goatcounter-id', host: 'my-goatcounter-domain.com', scriptSrc: 'https://my-url.to/counter.js' }` (self-hosted) use [GoatCounter](https://goatcounter.com); + - `{ provider: 'goatcounter', websiteId: 'my-goatcounter-id' }` (managed) or `{ provider: 'goatcounter', websiteId: 'my-goatcounter-id', host: 'my-goatcounter-domain.com', scriptSrc: 'https://my-url.to/counter.js' }`: (self-hosted) use [GoatCounter](https://goatcounter.com); - `{ provider: 'posthog', apiKey: '', host: '' }`: use [Posthog](https://posthog.com/); - `{ provider: 'tinylytics', siteId: '' }`: use [Tinylytics](https://tinylytics.app/); - `{ provider: 'cabin' }` or `{ provider: 'cabin', host: 'https://cabin.example.com' }` (custom domain): use [Cabin](https://withcabin.com); + - `{ provider: "matomo", host: "", siteId: "1" }`: (self-hosted) use [Matomo](https://matomo.org/) - `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`. diff --git a/quartz/cfg.ts b/quartz/cfg.ts index 0c344d33a..7bd582866 100644 --- a/quartz/cfg.ts +++ b/quartz/cfg.ts @@ -38,6 +38,11 @@ export type Analytics = provider: "cabin" host?: string } + | { + provider: "matomo" + host: string + siteId: string + } export interface GlobalConfiguration { pageTitle: string diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts index d1d8c8597..eee1e3442 100644 --- a/quartz/plugins/emitters/componentResources.ts +++ b/quartz/plugins/emitters/componentResources.ts @@ -152,6 +152,29 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso cabinScript.async = true document.head.appendChild(cabinScript) `) + } else if (cfg.analytics?.provider === "matomo") { + const { host, siteId } = cfg.analytics + componentResources.afterDOMLoaded.push(` + var _paq = window._paq = window._paq || []; + (function() { + var u="${host}/"; + _paq.push(['setTrackerUrl', u+'matomo.php']); + _paq.push(['setSiteId', '${siteId}']); + var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; + g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); + })(); + + // Track initial page view + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + + // Track SPA navigation + document.addEventListener("nav", () => { + _paq.push(['setCustomUrl', window.location.pathname]); + _paq.push(['setDocumentTitle', document.title]); + _paq.push(['trackPageView']); + }); + `) } if (cfg.enableSPA) {