Page view analytics plugin for EmDash CMS. Instant tracking, real-time dashboard, popular content — zero external services.
- Instant tracking — every page view is counted immediately, no cron jobs or batch processing
- Works with all Content Types — posts, pages, and any custom collections
- Dashboard — bar chart of views over time, stat cards, popular content table
- Real-time counter — "Last Hour" views with 30-second auto-refresh
- Dashboard widget — compact view count with 7-day sparkline on the admin home page
- Per-collection filtering — filter analytics by any content type
- Privacy-friendly — no cookies, no external services, all data stays in your database
- Auto-injected — tracking script is added to all pages automatically via the plugin system
npm install emdash-plugin-analytics// astro.config.mjs
import { analyticsPlugin } from "emdash-plugin-analytics";
import emdash from "emdash/astro";
export default defineConfig({
integrations: [
emdash({
// ... your emdash config
plugins: [analyticsPlugin()],
}),
],
});That's it. No additional configuration needed.
Go to Admin Panel > Plugins > Analytics to see:
- Today / Last 30 Days / Last Hour — stat cards at the top
- Views chart — daily page views as a bar chart (7d / 30d / 90d range selector)
- Popular Content — table of most viewed pages with collection badges
- Collection filter — dropdown to view analytics for a specific content type
The Page Views widget also appears on the admin dashboard home page.
The plugin injects a tiny beacon script on every page via the page:fragments hook. On page load, the script sends a navigator.sendBeacon() request to the plugin's public API endpoint with:
- Page path
- Referrer
- Screen width
- Locale
- Content type (collection) and content ID (auto-detected)
The tracking script:
- Skips admin pages (
/_emdash/*) - Uses
requestIdleCallbackto avoid blocking rendering - Sends no cookies — fully privacy-friendly
Each page view is stored as a document in EmDash's plugin storage system. Daily summaries and popular content lists are updated in KV (key-value store) on every view — no cron jobs needed.
The dashboard reads pre-aggregated data from KV for instant loading. The chart uses @cloudflare/kumo components. Real-time counter refreshes every 30 seconds.
The plugin exposes routes at /_emdash/api/plugins/analytics/:
| Method | Route | Auth | Description |
|---|---|---|---|
| POST | track |
Public | Record a page view (used by beacon script) |
| GET | stats/overview |
Admin | Last 30 days summary + popular content |
| GET | stats/content |
Admin | Per-collection breakdown |
| GET | stats/realtime |
Admin | Views in the last hour |
{
"path": "/posts/my-post",
"referrer": "https://google.com",
"screenWidth": 1920,
"locale": "en",
"collection": "posts",
"contentId": "01ABC..."
}Step-by-step instructions for AI coding agents to install and configure this plugin correctly.
npm install emdash-plugin-analyticsStep 1. Open astro.config.mjs and add the import at the top:
import { analyticsPlugin } from "emdash-plugin-analytics";Step 2. Add analyticsPlugin() to the plugins array inside the emdash({}) integration:
integrations: [
emdash({
// ... existing config
plugins: [
// ... any existing plugins
analyticsPlugin(),
],
}),
],Step 3. Restart the dev server. That's all — no additional configuration needed.
After installation, verify:
- Visit any page on the site (e.g.,
http://localhost:4321/) - Open browser DevTools > Network tab — you should see a beacon request to
/_emdash/api/plugins/analytics/track - Go to
http://localhost:4321/_emdash/admin/plugins/analytics/— the Analytics dashboard should show - The "Today" stat card should show at least 1 view
- The admin home page should show the "Page Views" widget
- The plugin uses EmDash's plugin storage for raw pageviews and KV store for pre-aggregated summaries
- Tracking is injected via the
page:fragmentshook — no theme changes needed - The
trackroute ispublic: true— no authentication required for the beacon - All aggregation happens inline in the track handler — no cron jobs
- Admin routes require authentication — only admins can view analytics
- The plugin automatically detects collection/contentId from EmDash's page context
- Do NOT add the tracking script manually — the plugin injects it automatically
- Do NOT set up cron jobs — aggregation is instant
- Do NOT worry about Content Types — the plugin works with all collections automatically
- EmDash CMS >= 0.1.0
- Astro >= 6.0.0
- Node.js >= 22
MIT