Keep your Astro site under 2 KB of client JS with privacy-first analytics.
Do not undo Astro zero-JS architecture with a 45 KB tag manager. Add one 1.96 KB is:inline defer script for automatic View Transitions tracking, active reading dwell, and EU cookie-less compliance.


How do you install lightweight privacy analytics in Astro without breaking View Transitions or Lighthouse scores?
Add TrimStats to your shared src/layouts/BaseLayout.astro head with <script is:inline defer src="https://collect.trimstats.com/t.js" data-site={siteKey}></script>. The is:inline directive stops Astro from bundling or stripping data-site, the 1.96 KB gzipped deferred payload preserves 100/100 Lighthouse scores, and built-in history.pushState hooks automatically track Astro ClientRouter View Transitions without duplicate pageviews.
Step-by-step setup for Astro
Copy-paste implementation verified against the live TrimStats tracker and collector APIs.
- 1
Add the is:inline defer tag to BaseLayout.astro
Place TrimStats inside your global layout <head> with is:inline so Astro leaves the external script URL and data-site attribute intact during static or SSR builds.
src/layouts/BaseLayout.astrohtml1---2const siteKey = import.meta.env.PUBLIC_TRIMSTATS_KEY;3---4<html lang="en">5 <head>6 <meta charset="utf-8" />7 <script is:inline defer src="https://collect.trimstats.com/t.js" data-site={siteKey}></script>8 </head>9 <body><slot /></body>10</html> - 2
Track Astro View Transitions (ClientRouter) automatically
Because Astro View Transitions update the URL via History API pushState and popstate, t.js automatically flushes departing active dwell time and records the incoming pageview with zero extra glue code.
- 3
Fire custom events from Astro islands or inline handlers
Call window.trimstats("event", ...) inside React, Svelte, Solid, or vanilla client scripts to track newsletter signups, CTA clicks, or docs feedback.
src/components/NewsletterForm.tstsdeclare global {interface Window { trimstats?: (...args: unknown[]) => void; }}window.trimstats?.('event', 'newsletter_subscribed', { source: 'blog_footer' }); - 4
Opt in to UI friction and 90% scroll reach on long content
Append data-friction="true" to measure 90% deep scroll reach on Starlight docs and Astro content collections alongside rage-click and dead-click selector hints.
High-signal custom events to track for Astro
Fire any event with window.trimstats?.('event', name, props) and attach it to conversion goals or multi-step ClickHouse funnels.
| Event name | When to fire | Sample properties |
|---|---|---|
| newsletter_subscribed | Reader submits an email signup form on an Astro blog post or home page. | {"placement":"article_end"} |
| docs_code_copied | Developer clicks a copy button inside an Astro Starlight code block. | {"language":"ts"} |
| cta_clicked | Visitor clicks a primary signup or demo button from a static landing page. | {"section":"hero"} |
| search_opened | Visitor triggers Pagefind or Command-K documentation search. | {"template":"starlight"} |


TrimStats preserves Astro near-zero JS payload compared to legacy trackers
Gzipped third-party analytics JavaScript downloaded on a zero-JS Astro landing page (KB)
Loading gtag.js or posthog-js adds 20x to 25x more client JavaScript than TrimStats on an otherwise zero-JS Astro page.
Methodology: TrimStats measured directly from packages/tracker/dist/t.js (1,964 B gzip). Competitor gzipped payload ranges verified from official docs and benchmarks.
View chart data table
| Label | Analytics script weight (gzipped) |
|---|---|
| TrimStats t.js (with SPA + dwell) | 1.96 KB |
| Simple Analytics latest.js | ~3.7 KB |
| Matomo matomo.js | ~22 KB |
| Google Analytics 4 (gtag.js) | ~40 KB |
| PostHog (posthog-js core) | ~50 KB |
Astro analytics FAQ
Implementation details, privacy trade-offs, and event-quota answers.
Why does Astro require the is:inline attribute on the TrimStats script tag?
Does TrimStats double-count pageviews when Astro View Transitions (ClientRouter) are enabled?
Does TrimStats work inside Astro Starlight documentation sites?
Will TrimStats hurt my Astro Core Web Vitals or Lighthouse Performance score?
Does TrimStats need a cookie consent banner on Astro blogs and docs sites?
How much does TrimStats cost for an Astro blog or documentation site?
Co-founder of DotCamp LLC in Brooklyn, NY and lead engineer of TrimStats. Ships privacy-first web analytics, ClickHouse pipelines, and WordPress plugins used on 115,000+ active sites.
- • Architect of the TrimStats 1.96 KB gzipped browser tracker, Fastify collector, and ClickHouse MergeTree pipeline
- • Co-founder of DotCamp LLC (est. 2019), creator of Ultimate Blocks (50,000+ installs), WP Table Builder (50,000+ installs), Tableberg, and OptimizeCamp
- • WordPress.org plugin & theme developer since 2016 with 1,200+ community support replies and 5M+ cumulative plugin downloads
Verification standard: Code snippets, custom event payloads, and webhook contracts for Astro are verified against the live TrimStats tracker (1.96 KB gzipped) and collector schema as of 2026-11-30.
Related documentation and guides
Instrument Astro in under 60 seconds
Start on Starter ($17/month for 100,000 monthly events), with cookie-less privacy defaults and full access to revenue webhooks, funnels, cohorts, and UI friction.