Skip to content

fix(astro): expose types for @vercel/analytics/astro export (#169) - #219

Open
VG31OP wants to merge 1 commit into
vercel:mainfrom
VG31OP:fix/astro-export-types
Open

VG31OP wants to merge 1 commit into
vercel:mainfrom
VG31OP:fix/astro-export-types

Conversation

@VG31OP

@VG31OP VG31OP commented Oct 15, 2026

Copy link
Copy Markdown

Summary

Fixes #169.

When importing @vercel/analytics/astro, TypeScript fails to resolve module declarations under TypeScript 4.7+ / 5.x resolution modes (moduleResolution: "bundler", "node16", or "nodenext"):

Cannot find module '@vercel/analytics/astro' or its corresponding type declarations.

Root Cause

  1. In packages/web/package.json#exports["./astro"], only "import": "./dist/astro/component.ts" was provided, omitting the "types" export condition. TypeScript requires the "types" condition to be listed first for subpath imports.
  2. In packages/web/package.json#typesVersions["*"], entries existed for next, nuxt, react, remix, server, sveltekit, and vue, but "astro" was missing.
  3. Other subpath export conditions (./next, ./react, ./nuxt, ./remix, ./vue) were also missing explicit "types" conditions in exports, relying solely on fallback typesVersions.

Changes

  • Added "types": "./dist/astro/component.ts" to exports["./astro"].
  • Added "astro": ["dist/astro/component.ts"] to typesVersions["*"].
  • Added explicit "types" conditions across all subpath export targets in packages/web/package.json.

Verification

  • Tested with pnpm --filter astro build and astro check in apps/astro: @vercel/analytics/astro imports and resolves cleanly without type errors.
  • Verified all existing vitest tests in packages/web pass (10/10 test files, 107/107 tests passing).
  • Verified Biome checks pass with zero errors.

@vercel

vercel Bot commented Oct 15, 2026

Copy link
Copy Markdown

@VG31OP is attempting to deploy a commit to the Curated Tests - Permanent E2E Team on Vercel.

A member of the Team first needs to authorize it.

Khanos pushed a commit to Khanos/khanos.frontend that referenced this pull request Oct 29, 2026
# Vercel Web Analytics Installation Report

## Summary
Successfully installed and configured Vercel Web Analytics for this Astro project following the latest official documentation.

## Changes Made

### 1. Installed @vercel/analytics package
- Added `@vercel/analytics@^2.0.1` to dependencies
- Updated `pnpm-lock.yaml` to reflect the new dependency

### 2. Configured Vercel adapter (astro.config.mjs)
- Enabled Web Analytics in the Vercel adapter configuration
- Added `webAnalytics: { enabled: true }` to the adapter options
- This configuration helps with proper deployment and integration with Vercel's platform

### 3. Added Analytics component (src/layouts/Layout.astro)
- Imported Analytics component from `@vercel/analytics/astro`
- Added `<Analytics />` component to the head section of the main layout
- The component is placed after the ClientRouter to ensure proper initialization
- Analytics will now track page views across all pages that use this layout

### 4. Added TypeScript declarations (src/env.d.ts)
- Added module declarations for `@vercel/analytics/astro` to resolve TypeScript errors
- This allows the TypeScript compiler to recognize the Astro component import
- Workaround for missing type exports in the current version of @vercel/analytics

## Implementation Details

The implementation follows the official Vercel Analytics documentation for Astro:
- For @vercel/analytics v2.0.1 (installed), manual component integration is required
- The adapter's `webAnalytics.enabled` configuration provides additional integration with Vercel's platform
- The Analytics component is imported and rendered in the base layout to track all pages

## Testing

- ✅ Build successful: `pnpm build` completes without errors
- ✅ TypeScript check passed: No type errors in Astro files
- ✅ All tests passing: 86 tests across 7 test files
- ✅ No linting errors introduced

## Deployment Notes

Once deployed to Vercel:
1. Enable Web Analytics in your Vercel dashboard under the project's Analytics tab
2. Analytics will automatically start tracking page views
3. Check browser's Network tab for `/<unique-path>/view` requests to confirm tracking is active

## Files Modified

- `astro.config.mjs` - Added webAnalytics configuration to Vercel adapter
- `package.json` - Added @vercel/analytics dependency
- `pnpm-lock.yaml` - Updated with new dependency
- `src/layouts/Layout.astro` - Imported and added Analytics component
- `src/env.d.ts` - Added TypeScript module declarations

## Notes

The TypeScript module declarations in `env.d.ts` are a temporary workaround for the current version of @vercel/analytics which doesn't properly expose types for the Astro subpath export. This is a known issue (vercel/analytics#219) and should be resolved in a future version of the package.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type error import Analytics from '@vercel/analytics/astro';

1 participant