Skip to content

Commit e918f11

Browse files
authored
feat: add support for matomo analytics (#2051)
* feat: Support Matomo as analytics provider (self-hosted) * SPA support * Don't use var * inline * Revert fixing issues outside of this change
1 parent 45c130b commit e918f11

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This part of the configuration concerns anything that can affect the whole site.
3434
- `{ provider: 'tinylytics', siteId: '<your-site-id>' }`: use [Tinylytics](https://tinylytics.app/);
3535
- `{ provider: 'cabin' }` or `{ provider: 'cabin', host: 'https://cabin.example.com' }` (custom domain): use [Cabin](https://withcabin.com);
3636
- `{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.
37+
- `{ provider: 'matomo', siteId: '<your-matomo-id-code', host: 'matomo.example.com' }`: use [Matomo](https://matomo.org/), without protocol.
3738
- `locale`: used for [[i18n]] and date formatting
3839
- `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.
3940
- 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`.

quartz/cfg.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export type Analytics =
4242
provider: "clarity"
4343
projectId?: string
4444
}
45+
| {
46+
provider: "matomo"
47+
host: string
48+
siteId: string
49+
}
4550

4651
export interface GlobalConfiguration {
4752
pageTitle: string

quartz/plugins/emitters/componentResources.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,33 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso
201201
})(window, document, "clarity", "script", "${cfg.analytics.projectId}");\`
202202
document.head.appendChild(clarityScript)
203203
`)
204+
} else if (cfg.analytics?.provider === "matomo") {
205+
componentResources.afterDOMLoaded.push(`
206+
const matomoScript = document.createElement("script");
207+
matomoScript.innerHTML = \`
208+
let _paq = window._paq = window._paq || [];
209+
210+
// Track SPA navigation
211+
// https://developer.matomo.org/guides/spa-tracking
212+
document.addEventListener("nav", () => {
213+
_paq.push(['setCustomUrl', location.pathname]);
214+
_paq.push(['setDocumentTitle', document.title]);
215+
_paq.push(['trackPageView']);
216+
});
217+
218+
_paq.push(['trackPageView']);
219+
_paq.push(['enableLinkTracking']);
220+
(function() {
221+
const u="//${cfg.analytics.host}/";
222+
_paq.push(['setTrackerUrl', u+'matomo.php']);
223+
_paq.push(['setSiteId', ${cfg.analytics.siteId}]);
224+
const d=document, g=d.createElement('script'), s=d.getElementsByTagName
225+
('script')[0];
226+
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
227+
})();
228+
\`
229+
document.head.appendChild(matomoScript);
230+
`)
204231
}
205232

206233
if (cfg.enableSPA) {

0 commit comments

Comments
 (0)