From 3051597de1ca41845675a5d5c017cf01fae061da Mon Sep 17 00:00:00 2001 From: Vercel Date: Wed, 11 Feb 2026 05:48:53 +0000 Subject: [PATCH] Add Vercel Web Analytics to SvelteKit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Vercel Web Analytics Installation and Configuration ### Summary Successfully installed and configured Vercel Web Analytics for the SvelteKit project. ### Changes Made #### 1. Package Installation - Installed `@vercel/analytics` package using npm - Updated `package.json` to include the new dependency - Updated `package-lock.json` with resolved dependencies #### 2. Analytics Configuration **Created:** `src/routes/+layout.ts` - Imported `dev` from `$app/environment` to detect development mode - Imported `injectAnalytics` from `@vercel/analytics/sveltekit` - Called `injectAnalytics({ mode: dev ? 'development' : 'production' })` to initialize analytics with environment-aware mode ### Implementation Details The analytics injection was added to the root layout file (`+layout.ts`) rather than the Svelte component (`+layout.svelte`) to follow SvelteKit best practices. The `+layout.ts` file runs before the component is mounted, ensuring analytics is initialized early in the application lifecycle. The configuration uses the `dev` environment variable to automatically switch between development and production modes, ensuring proper tracking behavior in different environments. ### Verification - ✅ Build completed successfully (`npm run build`) - ✅ Type checking passed with no errors or warnings (`npm run check`) - ✅ Lock file properly updated to include all dependencies - ✅ No build artifacts staged (only source code changes) ### Files Modified/Created 1. `package.json` - Added @vercel/analytics dependency 2. `package-lock.json` - Updated with resolved dependencies 3. `src/routes/+layout.ts` - Created new file with analytics configuration ### Notes The implementation preserves all existing code structure. The root layout Svelte component (`+layout.svelte`) remains unchanged, with analytics configuration cleanly separated in the TypeScript layout file. Co-authored-by: Vercel --- package-lock.json | 40 +++++++++++++++++++++++++++++++++++++++- package.json | 2 +- src/routes/+layout.ts | 4 ++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/routes/+layout.ts diff --git a/package-lock.json b/package-lock.json index 4c6c54c..fadfea0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@huggingface/transformers": "^3.8.1", "@pixiv/three-vrm": "^3.4.5", "@sveltejs/adapter-vercel": "^6.3.1", + "@vercel/analytics": "^1.6.1", "ai": "^6.0.77", "fish-audio": "^0.0.3", "kokoro-js": "^1.2.1", @@ -2596,6 +2597,44 @@ "dev": true, "license": "MIT" }, + "node_modules/@vercel/analytics": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@vercel/analytics/-/analytics-1.6.1.tgz", + "integrity": "sha512-oH9He/bEM+6oKlv3chWuOOcp8Y6fo6/PSro8hEkgCW3pu9/OiCXiUpRUogDh3Fs3LH2sosDrx8CxeOLBEE+afg==", + "license": "MPL-2.0", + "peerDependencies": { + "@remix-run/react": "^2", + "@sveltejs/kit": "^1 || ^2", + "next": ">= 13", + "react": "^18 || ^19 || ^19.0.0-rc", + "svelte": ">= 4", + "vue": "^3", + "vue-router": "^4" + }, + "peerDependenciesMeta": { + "@remix-run/react": { + "optional": true + }, + "@sveltejs/kit": { + "optional": true + }, + "next": { + "optional": true + }, + "react": { + "optional": true + }, + "svelte": { + "optional": true + }, + "vue": { + "optional": true + }, + "vue-router": { + "optional": true + } + } + }, "node_modules/@vercel/nft": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/@vercel/nft/-/nft-1.3.0.tgz", @@ -4421,4 +4460,3 @@ } } } - diff --git a/package.json b/package.json index 3ff7ed8..4f02637 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@huggingface/transformers": "^3.8.1", "@pixiv/three-vrm": "^3.4.5", "@sveltejs/adapter-vercel": "^6.3.1", + "@vercel/analytics": "^1.6.1", "ai": "^6.0.77", "fish-audio": "^0.0.3", "kokoro-js": "^1.2.1", @@ -36,4 +37,3 @@ "three": "^0.182.0" } } - diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts new file mode 100644 index 0000000..ffad00f --- /dev/null +++ b/src/routes/+layout.ts @@ -0,0 +1,4 @@ +import { dev } from '$app/environment'; +import { injectAnalytics } from '@vercel/analytics/sveltekit'; + +injectAnalytics({ mode: dev ? 'development' : 'production' });