Astro IntegrationZero-JS Static & Island Architecture (Astro 4 & 5)

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.

Imtiaz Rayhan
Imtiaz RayhanCo-Founder & Lead Engineer, DotCamp & TrimStats
Tested against Astro & live TrimStats APIs
Seeded demo workspace
TrimStats Overview dashboard displaying cookie-less visitor counts, visibility-aware reading time, and top content paths for an Astro siteTrimStats Overview dashboard displaying cookie-less visitor counts, visibility-aware reading time, and top content paths for an Astro site
Measure real foreground reading duration and <10s bounce rate on Astro blogs and Starlight docs without cookie banners.
Direct answer

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. 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.astrohtml
    1---
    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. 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. 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.tsts
    declare global {
    interface Window { trimstats?: (...args: unknown[]) => void; }
    }
    window.trimstats?.('event', 'newsletter_subscribed', { source: 'blog_footer' });
  4. 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.

Recommended taxonomy

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 nameWhen to fireSample properties
newsletter_subscribedReader submits an email signup form on an Astro blog post or home page.{"placement":"article_end"}
docs_code_copiedDeveloper clicks a copy button inside an Astro Starlight code block.{"language":"ts"}
cta_clickedVisitor clicks a primary signup or demo button from a static landing page.{"section":"hero"}
search_openedVisitor triggers Pagefind or Command-K documentation search.{"template":"starlight"}
Seeded demo workspace
TrimStats Behavior Multi-step Funnels dashboard tracking Astro content readers from /blog/* through /pricing to signup_completedTrimStats Behavior Multi-step Funnels dashboard tracking Astro content readers from /blog/* through /pricing to signup_completed
Use trailing-* prefix steps like /blog/* in ClickHouse funnels to see which Astro content clusters actually drive signups.

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)

KB
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

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
LabelAnalytics 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?
By default, Astro processes and bundles <script> tags without is:inline as ES modules and can strip custom data-* attributes. Adding is:inline keeps the deferred <script> tag verbatim in the HTML <head> so t.js reads data-site reliably.
Does TrimStats double-count pageviews when Astro View Transitions (ClientRouter) are enabled?
No. TrimStats patches history.pushState and listens to popstate while deduplicating against the previous pathname, so Astro View Transitions emit one clean leave beacon for the old route and one pageview for the new route.
Does TrimStats work inside Astro Starlight documentation sites?
Yes. Add the deferred script entry to the Starlight head array in astro.config.mjs with attrs: { defer: true, src, "data-site": key } to track every docs page, outbound link, and file download automatically.
Will TrimStats hurt my Astro Core Web Vitals or Lighthouse Performance score?
No. At 1.96 KB gzipped (1.73 KB Brotli) loaded with defer, zero third-party dependencies, and passive event listeners, TrimStats adds negligible parse time and zero layout shift.
Does TrimStats need a cookie consent banner on Astro blogs and docs sites?
Not in default cookie-less mode. TrimStats writes zero cookies and zero localStorage, discards raw IP addresses in volatile memory on German EU servers using a 24-hour rotating salt, and requires no consent wall for standard traffic analytics.
How much does TrimStats cost for an Astro blog or documentation site?
TrimStats starts on Starter at $17/month (100k events, 5 sites, 365-day retention) and scales to Growth at $44/month (1M events, 25 sites, 730-day retention) and Scale at $134/month (5M events, 100 sites) with every feature included.
Imtiaz Rayhan
Integration guide verified by
Imtiaz Rayhan (Co-Founder & Lead Engineer, DotCamp & TrimStats)

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.

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.

Verified against live TrimStats APIs and tracker bundle.