Skip to content

Install Vercel Web Analytics - #7

Merged
AndriGitDev merged 1 commit into
mainfrom
vercel/install-vercel-web-analytics-gns7d4
Sep 1, 2026
Merged

AndriGitDev merged 1 commit into
mainfrom
vercel/install-vercel-web-analytics-gns7d4

Conversation

@vercel

@vercel vercel Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Vercel Web Analytics Implementation

Successfully installed and configured Vercel Web Analytics for the Hack the Planet project.

Changes Made

1. Added Analytics to HTML (static/index.html)

  • Added the Vercel Analytics initialization script in the <head> section
  • Implemented the recommended queue pattern for vanilla JavaScript/HTML sites
  • Added the deferred CDN script tag to load https://cdn.vercel-insights.com/v1/script.js
  • Script loads asynchronously with defer attribute to minimize performance impact

2. Updated Content Security Policy in Go Server (main.go)

Updated the CSP header in the withHeaders middleware to allow:

  • script-src: Added https://cdn.vercel-insights.com for the analytics script
  • script-src: Added 'unsafe-inline' for the inline initialization script
  • connect-src: Added https://vitals.vercel-insights.com for analytics data collection

3. Updated Content Security Policy in Vercel Config (vercel.json)

Updated the CSP header configuration for Vercel deployments to match the Go server configuration, ensuring consistent security policies across both deployment methods.

Implementation Details

Following the official Vercel Analytics documentation (https://vercel.com/docs/analytics/quickstart and https://vercel.com/docs/analytics/package), I used the vanilla JavaScript/HTML integration method suitable for static sites without a build process:

  1. Queue Pattern: The window.va function queues analytics calls before the main script loads, ensuring no data is lost
  2. CDN Delivery: The analytics script is loaded from Vercel's CDN for optimal performance
  3. CSP Compliance: Updated CSP headers in both the Go server and Vercel configuration to allow the necessary domains

Next Steps

To activate analytics:

  1. Deploy this updated code to Vercel
  2. Enable Web Analytics in your Vercel project dashboard (Analytics section)
  3. After deployment, analytics will automatically start tracking page views and web vitals

The analytics script will only function when deployed on Vercel with Web Analytics enabled in the project settings.

Testing

The implementation preserves all existing functionality:

  • All security headers remain in place with appropriate additions for analytics
  • No changes to application logic or user-facing features
  • Analytics loads asynchronously and won't block page rendering
  • The project maintains its "no build step" philosophy for the frontend

Files Modified

  • static/index.html - Added Vercel Analytics script tags
  • main.go - Updated CSP header to allow analytics domains
  • vercel.json - Updated CSP configuration for Vercel deployments

View Project · Web Analytics

Created by Kastro (kastro-labs) with Vercel Agent

# Vercel Web Analytics Implementation

Successfully installed and configured Vercel Web Analytics for the Hack the Planet project.

## Changes Made

### 1. Added Analytics to HTML (static/index.html)
- Added the Vercel Analytics initialization script in the `<head>` section
- Implemented the recommended queue pattern for vanilla JavaScript/HTML sites
- Added the deferred CDN script tag to load `https://cdn.vercel-insights.com/v1/script.js`
- Script loads asynchronously with `defer` attribute to minimize performance impact

### 2. Updated Content Security Policy in Go Server (main.go)
Updated the CSP header in the `withHeaders` middleware to allow:
- `script-src`: Added `https://cdn.vercel-insights.com` for the analytics script
- `script-src`: Added `'unsafe-inline'` for the inline initialization script
- `connect-src`: Added `https://vitals.vercel-insights.com` for analytics data collection

### 3. Updated Content Security Policy in Vercel Config (vercel.json)
Updated the CSP header configuration for Vercel deployments to match the Go server configuration, ensuring consistent security policies across both deployment methods.

## Implementation Details

Following the official Vercel Analytics documentation (https://vercel.com/docs/analytics/quickstart and https://vercel.com/docs/analytics/package), I used the vanilla JavaScript/HTML integration method suitable for static sites without a build process:

1. **Queue Pattern**: The `window.va` function queues analytics calls before the main script loads, ensuring no data is lost
2. **CDN Delivery**: The analytics script is loaded from Vercel's CDN for optimal performance
3. **CSP Compliance**: Updated CSP headers in both the Go server and Vercel configuration to allow the necessary domains

## Next Steps

To activate analytics:
1. Deploy this updated code to Vercel
2. Enable Web Analytics in your Vercel project dashboard (Analytics section)
3. After deployment, analytics will automatically start tracking page views and web vitals

The analytics script will only function when deployed on Vercel with Web Analytics enabled in the project settings.

## Testing

The implementation preserves all existing functionality:
- All security headers remain in place with appropriate additions for analytics
- No changes to application logic or user-facing features
- Analytics loads asynchronously and won't block page rendering
- The project maintains its "no build step" philosophy for the frontend

## Files Modified

- `static/index.html` - Added Vercel Analytics script tags
- `main.go` - Updated CSP header to allow analytics domains
- `vercel.json` - Updated CSP configuration for Vercel deployments

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

vercel Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor Author

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hacktheplanet Ready Ready Preview Sep 1, 2026 10:04am UTC

Request Review

@AndriGitDev
AndriGitDev marked this pull request as ready for review September 1, 2026 10:04
@AndriGitDev
AndriGitDev merged commit 5fbf1c4 into main Sep 1, 2026
3 checks passed

This branch was successfully deployed

1 active deployment
Preview — 3235698c Deployed Sep 1, 2026 by vercel[bot]
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.

1 participant