• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
nbbaier

nbbaier

counterTown

Public
Like
counterTown
Home
Code
12
.git
10
backend
4
frontend
5
shared
2
.vtignore
AGENTS.md
ARCHITECTURE.md
CLAUDE.md
README.md
REFACTORING_PLAN.md
biome.json
deno.json
Environment variables
Branches
1
Pull requests
Remixes
1
History
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
ARCHITECTURE.md
Code
/
ARCHITECTURE.md
Search
1/26/2026
Viewing readonly version of main branch: v126
View latest version
ARCHITECTURE.md

Counter Town Architecture

A real-time web analytics tracker and dashboard hosted on Val Town.

Overview

┌─────────────────────────────────────────────────────────────────┐
│                         Server Side                             │
├─────────────────────────────────────────────────────────────────┤
│  counterTown.tsx        Main HTTP server/router + tracking      │
│  counterTownDb.tsx      SQLite database layer (sites, views)    │
└─────────────────────────────────────────────────────────────────┘
                              ↓ SSE stream
┌─────────────────────────────────────────────────────────────────┐
│                         Client Side                             │
├─────────────────────────────────────────────────────────────────┤
│  counterTownClient.tsx  React app entry point + routing         │
│  AnalyticsContext.tsx   State management via SSE connection     │
│  Dashboard.tsx          Main dashboard orchestrator             │
└─────────────────────────────────────────────────────────────────┘

Server Side

counterTown.tsx

Main entry point that handles:

  • HTTP routing for the dashboard, tracking endpoints, and SSE stream
  • CounterTown class for tracking logic and origin validation
  • Generates the tracking script that sites embed
  • Serves the dashboard HTML with Tailwind CSS configuration

counterTownDb.tsx

SQLite database layer with three tables:

  • sites - Tracked sites with total view counts
  • path_analytics - Per-path stats (views, referrers, browsers, OS, etc.)
  • view_timestamps - Individual view timestamps for time-series data

Exports: initAnalytics(), trackPageView(), getAnalytics(), cleanupOldData()

Client Side

counterTownClient.tsx

React application entry point:

  • React Router setup with routes for / and /:sitename
  • Theme provider for dark/light mode
  • Error boundary and loading states
  • Renders the main App component

AnalyticsContext.tsx

Central state management:

  • SSE connection to /stream endpoint for real-time updates
  • Data processing and aggregation (browsers, OS, screen sizes, countries)
  • Calculates day/week/month view percentages
  • Provides useAnalytics() hook for components

Dashboard.tsx

Main dashboard orchestrator that composes all section components based on the current site selection.

Dashboard Components

ComponentPurpose
OverallStatsSection.tsxTotal views, visitors, paths summary
statCard.tsxReusable stat display card
SitesSection.tsxList of all tracked sites
TopPathsSection.tsxMost visited paths table
TopReferrersSection.tsxTop referrer sources
VisitorOriginsSection.tsxCountry-based visitor breakdown
visitorGlobe.tsxGlobe visualization for visitor origins
ChartsSection.tsxBrowser/OS/device charts container
chartCard.tsxReusable chart card component
Navigation.tsxSite navigation/breadcrumbs
ConnectionStatus.tsxSSE connection status indicator

Utilities

FilePurpose
formatSiteName.tsxURL formatting (strips protocol, www)
personalData.tsxPersonal configuration data
valTownAnalytics.tsxVal Town specific analytics helpers

Data Flow

  1. Tracking: Sites embed a script from /trackingScript that sends page views to /trackPageView
  2. Storage: counterTownDb.tsx stores views in SQLite with JSON aggregations
  3. Streaming: Dashboard connects to /stream SSE endpoint
  4. Display: AnalyticsContext processes data and components render via React
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2026 Val Town, Inc.