# Astro analytics: one script in the head, without double counts

The CrispStats team · Published 2026-11-29 · Updated 2026-11-29

**TL;DR:** Astro does not ship visitor analytics. On a static site, one script tag in the shared layout head is the whole install, and each full page load is one pageview. The trap is ClientRouter: it swaps the page without a new document, and astro:page-load also fires on the first load, so a history listener plus a pageview from that event records every page twice.

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](/blog/nextjs-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](https://docs.astro.build/en/reference/directives-reference/#isinline) 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:

```html
<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](https://docs.astro.build/en/guides/view-transitions/) 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.

Router behavior from Astro's view transitions guide, read 29 November 2026.

| 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-rerun` forces an inline script to execute again after every transition. On a snippet that sends a pageview whenever it runs, that is a third count.

> **The first load counts too.** astro:page-load is not "the next page only". Astro's docs say the ClientRouter fires it on the initial page load of a pre-rendered page and on every navigation after that, forwards or back. A handler that does not skip work already done will double the landing page even before anyone clicks.

## 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.

1. 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.
2. Open the browser's network panel and filter for the analytics request. Load the home page. Note how many requests left.
3. Click a link to another path. Exactly one more request should leave, for the new path. Click back. Exactly one more.
4. If each click sends two, remove the `astro:page-load` pageview 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](/blog/nextjs-analytics-guide) 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](/integrations/astro) has the layout file. There is still no Astro package to add.

## Frequently asked questions

### Does Astro have built-in analytics?

No. Astro does not count visitors or pageviews. You add a script tag to the shared layout. There is no official analytics integration to install.

### Do I need extra code for a static Astro site?

No. One script in the layout head is enough, with is:inline so Astro does not bundle it. Each full page load is one pageview. Do not add a route-change listener on a site that does not use ClientRouter.

### Does ClientRouter count as a pageview by itself?

No. ClientRouter swaps the page and updates the browser history. Your analytics script counts a pageview only if it listens for that history change, or if you send a pageview yourself. Doing both counts each page twice.

### Does astro:page-load fire on the first page?

Yes. Astro's docs say ClientRouter fires astro:page-load on the initial load of a pre-rendered page and on every later navigation, including back and forward. A pageview inside that handler double-counts the landing page if the script already counted the load.

### Do CSS view transitions break analytics?

No. Astro's guide says a browser-native cross-document transition does not change how scripts run. The analytics script still loads with each new document. The double-count problem is the ClientRouter, not the CSS animation.
