siaikin
2025-09-09 e77a051144663ea08571e0bfb7aef6f463463f78
feat(analytics): add support for Vercel Web Analytics provider (#2119)

* feat(analytics): add support for Vercel Web Analytics provider

* fix: correct indentation for Vercel analytics provider condition
3 files modified
17 ■■■■■ changed files
docs/configuration.md 1 ●●●● patch | view | raw | blame | history
quartz/cfg.ts 3 ●●●●● patch | view | raw | blame | history
quartz/plugins/emitters/componentResources.ts 13 ●●●●● patch | view | raw | blame | history
docs/configuration.md
@@ -35,6 +35,7 @@
  - `{ 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: 'matomo', siteId: '<your-matomo-id-code', host: 'matomo.example.com' }`: use [Matomo](https://matomo.org/), without protocol.
  - `{ provider: 'vercel' }`: use [Vercel Web Analytics](https://vercel.com/docs/concepts/analytics).
- `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
@@ -47,6 +47,9 @@
      host: string
      siteId: string
    }
  | {
      provider: "vercel"
    }
export interface GlobalConfiguration {
  pageTitle: string
quartz/plugins/emitters/componentResources.ts
@@ -228,6 +228,19 @@
      \`
      document.head.appendChild(matomoScript);
    `)
  } else if (cfg.analytics?.provider === "vercel") {
    /**
     * script from {@link https://vercel.com/docs/analytics/quickstart?framework=html#add-the-script-tag-to-your-site|Vercel Docs}
     */
    componentResources.beforeDOMLoaded.push(`
      window.va = window.va || function () { (window.vaq = window.vaq || []).push(arguments); };
    `)
    componentResources.afterDOMLoaded.push(`
      const vercelInsightsScript = document.createElement("script")
      vercelInsightsScript.src = "/_vercel/insights/script.js"
      vercelInsightsScript.defer = true
      document.head.appendChild(vercelInsightsScript)
    `)
  }
  if (cfg.enableSPA) {