Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 8 min read How-to mediumAdept

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

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.

Assumes: Astro basicsSQL basicsVercel deployment

Related posts

  • How to use Postgres at the Edge

    Neon mediumAdept

    Standard Postgres can't be reached from V8-isolate edge runtimes like Vercel Edge Functions because they don't support raw TCP. Neon's serverless driver, a drop-in replacement for node-postgres, gets around this by talking HTTP or WebSockets to a Neon proxy, which opens the TCP connection to Postgres. The walkthrough shows Next.js edge API routes using the `neon` SQL-over-HTTP function for single-shot queries and `Pool`/`Client` over WebSockets for multiple queries per connection. It then combines `@vercel/edge` geolocation with an INSERT into a locations table, as in the Ping Thing demo.

  • Postgres Realtime location sharing with MapLibre

    Supabase mediumAdept

    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. The RPC looks up the user's active session and stores the coordinates as a PostGIS point. A React/Next.js client subscribes to Supabase Realtime postgres_changes INSERT events, filtered by event_id, and renders markers with react-map-gl and MapLibre GL JS over a Protomaps basemap served from Supabase Storage.

  • Sub-10ms Postgres queries for Vercel Edge Functions

    Neon mediumAdept

    Neon's serverless Postgres driver now supports SQL-over-HTTP via fetch, and Neon's proxy adds opt-in experimental connection caching (`fetchConnectionCache`). Together they cut query latency from Vercel Serverless and Edge Functions by about 40%, bringing same-region one-shot queries to single-digit milliseconds. Use the `neon` HTTP function for a single query per invocation and `Pool` when a function runs multiple queries. Drizzle-ORM also supports the driver for type-safe queries.

  • Geo Queries with PostGIS in Ionic Angular

    Supabase mediumAdept

    PostGIS on Supabase handles geo data for an Ionic Angular app with a Capacitor Google Map. A stores table holds geography(POINT) locations behind a GIST spatial index. Two SQL functions do the geo queries: nearby_stores orders stores by the <-> distance operator and returns st_distance in meters, and stores_in_view filters with && against an ST_MakeBox2D bounding box. The app calls them via Supabase RPC, inserts locations as WKT POINT(long lat) strings, uploads store images to Supabase Storage, and covers the native location permissions and API keys needed for iOS and Android.

  • Edge Functions: Node and native npm compatibility

    Supabase mediumAdept

    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. Under the hood, the open-source Deno-based Supabase Edge Runtime serializes each function's module graph into a single eszip at deploy time and loads all modules from it in the hosted, local and self-hosted environments alike. This avoids fetch latency, dependency conflicts with locally installed packages, and bugs such as failures when a deno.lock file is present. The release also adds regional invocation via an `x-region` header, CPU/memory and status-code metrics in the dashboard, and error tracking through Sentry's Deno SDK.

The quest for software engineering knowledge.

Look:
Layout: