Setup and data
Astro analytics: one script in the head, without double counts
Add analytics to an Astro site with one script in the layout head, and avoid counting each view transition twice when ClientRouter is on.
The CrispStats team · · 8 min read
In short
An Astro site is a pile of HTML until you opt into client-side routing. Analytics follows that split. A script in the head counts a normal Astro site with no extra code, because every link is a new document. The same script over-counts the day you add view transitions and also tell it, by hand, that the page changed. This guide is that distinction, using Astro’s view transition docs and its template directives, read on 29 November 2026.
There is no official Astro analytics integration to install, and this page does not invent one. The closest cousin is a Next.js app, where client-side navigations cause the same class of double count. That version is in the Next.js analytics guide.
Does Astro include analytics?
No. Astro builds pages and, if you ask it to, animates the move between them. It does not count visitors, sources or conversions. You add a script.
That script belongs in the head of the layout every page uses, usually src/layouts/Layout.astro, so a new page cannot ship without it. A tag that lives in one page component is missing from the rest of the site, which looks like a traffic drop concentrated on the templates you forgot.
How do you add a script to a static Astro site?
Put one classic script tag in the layout head and mark it is:inline, so Astro leaves the tag as written instead of bundling it.
Astro’s directives reference says a <script> is processed and bundled by default, and is:inline keeps it in the HTML where you wrote it. Any attribute other than src already implies that, and writing it out makes the intent obvious. The script has to stay a normal tag pointing at the hosted file, because it reads its site key from its own attributes. With is:inline on our tag:
<script is:inline defer src="https://in.crispstats.com/d.js" data-site="YOUR_SITE_KEY"></script>The default Astro site does a full browser navigation for every link. The document loads, the script runs, one pageview is sent. You do not listen for route changes. You do not import a package. A second copy of the tag, in the layout and again on a page, is the usual way a static site double-counts.
What changes when you turn on view transitions?
Two different features share the name, and only one of them changes what a script can see.
Astro’s view transitions guide splits them. A browser-native, cross-document transition is CSS animation on a normal page load. Astro says it does not change how scripts run and does not add a router. Your head script still runs once per document.
Astro’s own router is the <ClientRouter /> component from astro:transitions, added in the shared head. It intercepts links, the back button and the forward button, fetches the next page, and swaps it in. During that swap, scripts that already exist on the new page are left in place, so the analytics tag does not run again. The URL is updated through the browser history. At the end of the navigation, including the first load of a pre-rendered page, Astro fires astro:page-load on the document.
| Navigation | New document? | Script follows history, nothing else | Also send a pageview from astro:page-load |
|---|---|---|---|
| Default link | Yes | One pageview | Still one: the event does not fire without ClientRouter |
| CSS cross-document transition | Yes, with an animation | One pageview | Still one: there is no router event |
| ClientRouter | No, the page is swapped | One pageview per new URL | Two per URL, including the first load |
Why do view transitions double-count pageviews?
Because astro:page-load fires on the first load and on every later navigation, and a history listener has usually recorded that same URL already.
People add the listener for a good reason. A script that only runs on a full document load sees the first page and then goes quiet, since ClientRouter never loads a new document. The fix they copy sends a pageview inside astro:page-load. If the script also patches history.pushState, or listens to the Navigation API, both fire for the same click.
The order makes it worse, not better. Astro updates the browser history before astro:page-load. A tracker that counts history changes has already stored the new URL by the time the event runs. Calling the tracker with that URL sends a second pageview. Calling it with no URL is not automatically safe either: only a tracker that ignores a repeat of the last URL will drop it. Read the tracker’s own rule before you add the listener.
- One mechanism. If the script documents that it follows history changes, add the tag and stop. Do not also listen for
astro:page-load. - The other mechanism. If the script only counts full loads, send exactly one pageview from
astro:page-load, and disable its automatic pageview so the first load is not in both places. - Do not rerun the tag.
data-astro-rerunforces an inline script to execute again after every transition. On a snippet that sends a pageview whenever it runs, that is a third count.
How do you check that each click was counted once?
Click one internal link and expect one new pageview. Then use the back button and expect one more. Two requests for a single click means two mechanisms are on.
- Deploy, or use a preview host the tool is allowed to count. A local dev server is the wrong place to judge this: many scripts ignore localhost, and the production router is what you ship.
- Open the browser’s network panel and filter for the analytics request. Load the home page. Note how many requests left.
- Click a link to another path. Exactly one more request should leave, for the new path. Click back. Exactly one more.
- If each click sends two, remove the
astro:page-loadpageview or the history tracking, not the tag. Republish and repeat the same three clicks.
An anchor link that only changes the hash is not a new page unless you have decided fragments are pages. Counting #section as a pageview inflates a long document. Leave hash tracking off on a normal Astro site, which uses real paths.
What does a doubled visit look like in the report?
The site looks twice as busy, bounce rate falls, and pages per visit rise, with no change in the writing or the design.
A worked example: three docs pages
This example uses made-up numbers. A docs site turns on ClientRouter. A reader opens /docs, clicks /docs/install, then /docs/deploy. That is three pages.
The script counts history changes, and a component also calls the tracker with the current URL on every astro:page-load. The first page is counted by the load and again by the event. Each click is counted by the history change and again by the event. The visit is stored as six pageviews. Bounce rate for these readers falls toward zero, because almost nobody has a one-page session anymore. A week of this looks like a successful redesign.
Remove the event handler. The same reader is three pageviews, and a reader who opens one page and leaves is a bounce again. Compare the day before the handler and the day after you remove it before you conclude the content changed. The same check is how you would catch a double count on a Next.js app.
How does CrispStats work with Astro?
CrispStats is the single tag in the layout head, with is:inline, and nothing else. The core script is about 2.2 KB gzipped. It sets no cookies, does not store the IP address or the raw user agent, and does not identify the person. It counts a pageview on load and when the URL changes through pushState, replaceState, popstate or the Navigation API. The same URL twice in a row is one pageview. ClientRouter’s history update is that signal, so you do not add an astro:page-load listener.
If the tag is executed again after a transition, the second copy sees the script is already running and does nothing. Localhost is ignored, so astro dev will not show visits: check the deployed site. A button is a data-cs-event attribute or a crispstats("signup") call, not a second pageview. The Astro install guide has the layout file. There is still no Astro package to add.
Terms in this guide
Free tools
- Bounce rate calculatorCalculate bounce rate from sessions, under the classic single-page rule or the GA4 definition, and see how many bounces stand between you and a target.
- Analytics cost calculatorPick a monthly volume and compare list prices of privacy-friendly analytics tools at that exact volume. Dated, and never estimated between tiers.
- Engagement rate calculatorCalculate engagement rate the GA4 way for a website, or the social media way for a post by reach, impressions or followers, and see why the two differ.
More guides
All guides- Setup and data · 11 minNext.js analytics: your options and the App Router gotchasThe ways to add analytics to a Next.js app, from Vercel Web Analytics to GA4 and privacy-first scripts, and the App Router mistakes that skew your numbers.
- Traffic · 12 minHow to check website traffic: your own site and anyone else'sCheck your own traffic with analytics, Search Console or server logs; estimate other sites with Similarweb, Semrush or Ahrefs, and why the numbers disagree.
- Performance · 9 minCore Web Vitals in plain EnglishWhat LCP, INP and CLS measure, the good thresholds, and whether Core Web Vitals affect SEO, using Google's own wording.