John Bowdre
2024-06-18 3faf2ff6f57cd17289f777e2a823ca79c00cb43f
feat(analytics): Cabin analytics support (#1221)

* add cabin analytics

* fix formatting
3 files modified
13 ■■■■■ changed files
docs/configuration.md 1 ●●●● patch | view | raw | blame | history
quartz/cfg.ts 4 ●●●● patch | view | raw | blame | history
quartz/plugins/emitters/componentResources.ts 8 ●●●●● patch | view | raw | blame | history
docs/configuration.md
@@ -31,6 +31,7 @@
  - `{ 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: '<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);
- `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`.
quartz/cfg.ts
@@ -34,6 +34,10 @@
      provider: "tinylytics"
      siteId: string
    }
  | {
      provider: "cabin"
      host?: string
    }
export interface GlobalConfiguration {
  pageTitle: string
quartz/plugins/emitters/componentResources.ts
@@ -144,6 +144,14 @@
      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) {