From 3faf2ff6f57cd17289f777e2a823ca79c00cb43f Mon Sep 17 00:00:00 2001 From: John Bowdre <61015723+jbowdre@users.noreply.github.com> Date: Tue, 18 Jun 2024 15:38:45 -0500 Subject: [PATCH] feat(analytics): Cabin analytics support (#1221) * add cabin analytics * fix formatting --- docs/configuration.md | 1 + quartz/cfg.ts | 4 ++++ quartz/plugins/emitters/componentResources.ts | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 6c4c928..e97d8df 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -31,6 +31,7 @@ This part of the configuration concerns anything that can affect the whole site. - `{ 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); - `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 13fb516..574ceb1 100644 --- a/quartz/cfg.ts +++ b/quartz/cfg.ts @@ -34,6 +34,10 @@ export type Analytics = provider: "tinylytics" siteId: string } + | { + provider: "cabin" + host?: string + } export interface GlobalConfiguration { pageTitle: string diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts index e6725d9..d1d8c85 100644 --- a/quartz/plugins/emitters/componentResources.ts +++ b/quartz/plugins/emitters/componentResources.ts @@ -144,6 +144,14 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso tinylyticsScript.defer = true document.head.appendChild(tinylyticsScript) `) + } else if (cfg.analytics?.provider === "cabin") { + componentResources.afterDOMLoaded.push(` + const cabinScript = document.createElement("script") + cabinScript.src = "${cfg.analytics.host ?? "https://scripts.cabin.dev"}/cabin.js" + cabinScript.defer = true + cabinScript.async = true + document.head.appendChild(cabinScript) + `) } if (cfg.enableSPA) {