---
title: "Roll Your Own Analytics With Astro, Vercel Edge Functions and Neon"
description: "A self-hosted page-view analytics pipeline replaces Google Analytics after GA4 dropped the latitude/longitude reporting dimension."
url: "https://swe.quest/articles/roll-your-own-analytics-with-astro-vercel-edge-functions-and-neon-0f9127dd2d84817b"
canonical: "https://neon.com/blog/roll-your-own-analytics-with-astro-vercel-edge-functions-and-neon"
source: "Neon"
published: "2023-10-03"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["analytics","serverless","postgres","javascript","frontend"]
---

# Roll Your Own Analytics With Astro, Vercel Edge Functions and Neon

A post on [Neon](https://swe.quest/sources/neon.md), published 2023-10-03, 8 min read, How-to. Original: [https://neon.com/blog/roll-your-own-analytics-with-astro-vercel-edge-functions-and-neon](https://neon.com/blog/roll-your-own-analytics-with-astro-vercel-edge-functions-and-neon)

## Summary

A self-hosted page-view analytics pipeline replaces Google Analytics after GA4 dropped the latitude/longitude reporting dimension. An Astro site deployed to Vercel sends the page slug from a small web component via navigator.sendBeacon to a Vercel Edge Function. The function reads flag, country, city and coordinates with the @vercel/edge geolocation helper and inserts a row into a Neon Postgres table through the serverless driver. Example SQL queries cover top countries and cities, a 30-day geolocation feed for a 3D globe, and daily visit counts. Two caveats: geolocation is null in local dev, and some ad blockers block sendBeacon.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Serverless](https://swe.quest/tags/serverless.md) (Backend & APIs)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [JavaScript](https://swe.quest/tags/javascript.md) (Languages)
- [Frontend](https://swe.quest/tags/frontend.md) (Web & mobile)

## Prerequisites

- Astro basics
- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Vercel deployment ([easy posts on Deployment](https://swe.quest/tags/deployment.md?difficulty=easy))

## Keywords

Astro, Vercel Edge Functions, Neon, sendBeacon, Google Analytics 4, @vercel/edge

## Related posts

- [How to use Postgres at the Edge](https://swe.quest/articles/how-to-use-postgres-at-the-edge-9d5045c1101b2176.md): Neon · 2023-09-19 · Medium · 4 min read · PostgreSQL, Serverless, CDN & edge, JavaScript
  Standard Postgres can't be reached from V8-isolate edge runtimes like Vercel Edge Functions because they don't support raw TCP.
- [Postgres Realtime location sharing with MapLibre](https://swe.quest/articles/postgres-realtime-location-sharing-with-maplibre-f6d03ffa8efe5716.md): Supabase · 2024-07-04 · Medium · 5 min read · Real-time systems, Maps & geospatial, PostgreSQL, React, Serverless
  Live location sharing is built by having a Telegram bot running in a Supabase Edge Function (grammY on Deno) capture edited-location updates and write them to Postgres through a PL/pgSQL RPC.
- [Sub-10ms Postgres queries for Vercel Edge Functions](https://swe.quest/articles/sub-10ms-postgres-queries-for-vercel-edge-functions-6bc9caef6fab65ca.md): Neon · 2023-07-10 · Medium · 3 min read · PostgreSQL, Serverless, CDN & edge, Performance engineering, TypeScript
  Neon's serverless Postgres driver now supports SQL-over-HTTP via fetch, and Neon's proxy adds opt-in experimental connection caching (\`fetchConnectionCache\`).
- [Geo Queries with PostGIS in Ionic Angular](https://swe.quest/articles/geo-queries-with-postgis-in-ionic-angular-9afa3d65d6fa3030.md): Supabase · 2023-03-01 · Medium · 20 min read · PostgreSQL, Maps & geospatial, Mobile, Frontend
  PostGIS on Supabase handles geo data for an Ionic Angular app with a Capacitor Google Map.
- [Edge Functions: Node and native npm compatibility](https://swe.quest/articles/edge-functions-node-and-native-npm-compatibility-4f29863e45494db0.md): Supabase · 2023-12-12 · Medium · 3 min read · Serverless, JavaScript, Observability
  Supabase Edge Functions now natively support npm modules (via \`npm:\` imports) and Node built-in APIs, so existing Node apps using Express, node-postgres and Drizzle can migrate with minimal changes.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
