feat(analytics): add support for Vercel Web Analytics provider (#2119)
Some checks failed
Build and Test / build-and-test (ubuntu-latest) (push) Has been skipped
Build and Test / publish-tag (push) Has been skipped
Build and Test / build-and-test (macos-latest) (push) Has been cancelled
Build and Test / build-and-test (windows-latest) (push) Has been cancelled

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

* fix: correct indentation for Vercel analytics provider condition
This commit is contained in:
siaikin 2025-09-09 11:06:28 +08:00 committed by GitHub
parent ecb3d92941
commit e77a051144
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 0 deletions

View File

@ -35,6 +35,7 @@ This part of the configuration concerns anything that can affect the whole site.
- `{ 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`.

View File

@ -47,6 +47,9 @@ export type Analytics =
host: string
siteId: string
}
| {
provider: "vercel"
}
export interface GlobalConfiguration {
pageTitle: string

View File

@ -228,6 +228,19 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso
\`
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) {