Install Vercel Web Analytics - #7
Merged
Merged
Conversation
# 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>
Contributor
Author
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
AndriGitDev
marked this pull request as ready for review
September 1, 2026 10:04
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
<head>sectionhttps://cdn.vercel-insights.com/v1/script.jsdeferattribute to minimize performance impact2. Updated Content Security Policy in Go Server (main.go)
Updated the CSP header in the
withHeadersmiddleware to allow:script-src: Addedhttps://cdn.vercel-insights.comfor the analytics scriptscript-src: Added'unsafe-inline'for the inline initialization scriptconnect-src: Addedhttps://vitals.vercel-insights.comfor analytics data collection3. 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:
window.vafunction queues analytics calls before the main script loads, ensuring no data is lostNext Steps
To activate analytics:
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:
Files Modified
static/index.html- Added Vercel Analytics script tagsmain.go- Updated CSP header to allow analytics domainsvercel.json- Updated CSP configuration for Vercel deploymentsView Project · Web Analytics
Created by Kastro (kastro-labs) with Vercel Agent