From e77a051144663ea08571e0bfb7aef6f463463f78 Mon Sep 17 00:00:00 2001
From: siaikin <abc1310054026@outlook.com>
Date: Tue, 09 Sep 2025 03:06:28 +0000
Subject: [PATCH] feat(analytics): add support for Vercel Web Analytics provider (#2119)

---
 quartz/plugins/emitters/componentResources.ts |   13 +++++++++++++
 docs/configuration.md                         |    1 +
 quartz/cfg.ts                                 |    3 +++
 3 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/docs/configuration.md b/docs/configuration.md
index 6a8068a..c12a8a5 100644
--- a/docs/configuration.md
+++ b/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`.
diff --git a/quartz/cfg.ts b/quartz/cfg.ts
index 016bce6..57dff5c 100644
--- a/quartz/cfg.ts
+++ b/quartz/cfg.ts
@@ -47,6 +47,9 @@
       host: string
       siteId: string
     }
+  | {
+      provider: "vercel"
+    }
 
 export interface GlobalConfiguration {
   pageTitle: string
diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts
index 857f4d3..f97fdde 100644
--- a/quartz/plugins/emitters/componentResources.ts
+++ b/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) {

--
Gitblit v1.10.0