Skip to content

Frameworks

Astro analytics with CrispStats

An Astro site gets analytics from one CrispStats script tag with the is:inline directive, placed inside the head of your shared layout, usually src/layouts/Layout.astro. Every page that uses the layout is tracked, and with view transitions each client-side navigation counts as a pageview too.

Updated

Set up CrispStats on Astro

  1. 01

    Step 1: Copy the tag with your key

    In the dashboard, open your site’s Settings and select Tracking code. The tag there has your public key (it starts with cs_pk_) and your domain filled in. The key is safe to publish.

  2. 02

    Step 2: Add the tag to your layout with is:inline

    Add the tag inside <head> and keep is:inline. That directive tells Astro to leave the tag as written instead of processing and bundling it. The script reads its options from its own data- attributes, so it has to stay a classic tag pointing at the hosted file. If your pages use more than one layout, add the tag to each, or to a shared head component they all include.

    Replace the highlighted values with your own.

    src/layouts/Layout.astro
    ---interface Props {title: string;}const { title } = Astro.props;---<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width" /><title>{title}</title><scriptdeferis:inlinedata-site="cs_pk_your_site_key"data-domains="example.com"src="https://in.crispstats.com/d.js"></script></head><body><slot /></body></html>
  3. 03

    Step 3: Turn on more tracking (optional)

    Add attributes to the same tag. The switches in Settings, Tracking code add data-outbound, data-downloads, data-vitals, data-errors and data-404. Leave Hash routing off: Astro routes with real paths.

    src/layouts/Layout.astro
    <scriptdeferis:inlinedata-site="cs_pk_your_site_key"data-domains="example.com"data-outbounddata-downloadsdata-vitalssrc="https://in.crispstats.com/d.js"></script>
  4. 04

    Step 4: Track clicks with attributes

    For clicks, HTML attributes are the simplest option in Astro. They need no script, and they keep working across view transitions because the script listens for clicks on the whole page. Each data-cs-prop-* attribute becomes a property, here from: "hero".

    src/components/SignupButton.astro
    <a href="/signup" data-cs-event="signup clicked" data-cs-prop-from="hero">Sign up</a>
  5. 05

    Step 5: Send other events from a script (optional)

    For anything else, call the global from a <script>. Astro runs a component’s processed script once per full page load, so with view transitions attach listeners in an astro:page-load handler. Without view transitions, attach the listener directly.

    src/components/PlanPicker.astro
    <select id="plan"><option value="starter">Starter</option><option value="pro">Pro</option></select><script>document.addEventListener("astro:page-load", () => {document.getElementById("plan")?.addEventListener("change", (event) => {const plan = (event.target as HTMLSelectElement).value;window.crispstats?.("plan selected", { plan });});});</script>
  6. 06

    Step 6: Build, deploy and open the live site

    Pages on localhost, 127.0.0.1 and file:// are ignored, so astro dev and astro preview are not counted. Deploy, then open the production site in a browser without an ad blocker.

Check that it works

In your browser’s developer tools, the Network tab shows a POST to https://in.crispstats.com/e on each page load, and the collector answers 202. In the dashboard, the site’s Tracking code tab shows Receiving events. with the time of the last event, and the visit appears on Realtime. To see events locally, add data-debug to the tag and localhost to data-domains while you develop: each event is logged in the console and nothing is sent. Remove both before you ship. See localhost and testing.

What the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • What you get

    • A pageview for every static page load, and for every view-transition navigation made through the client router, with the previous page as referrer.
    • Sources and channels, including search, newsletters tagged with UTM parameters and AI assistants such as ChatGPT and Perplexity.
    • A core script of about 2.2 KB gzipped, loaded with defer, so a fast static site stays fast.
    • Time on page and scroll depth for each page, useful on docs and blogs where reading is the goal.
    • Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute.
  • Notes and limits

    • Without is:inline, Astro processes the tag and the script cannot read its options. Keep the directive.
    • With view transitions (<ClientRouter />, called <ViewTransitions /> in older versions), the content is swapped and the URL updated with history.pushState, which the script watches. If the tag runs again after a navigation, the second copy finds the tracker running and does nothing, so no visit is counted twice.
    • Not-found pages can be reported from your 404 layout with data-404. See 404 pages.
    • There is no Astro integration or npm package to install. The tag in the layout is the whole install, for static output and for server output on any adapter.
    • Leave out private paths with data-exclude, and your own visits with the ignore flag in your browser. See exclude traffic.
    • To count visitors whose ad blocker stops the collector host, serve the script from your own domain through your host: see proxy by host.

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

  • Astro processes and bundles script tags by default. is:inline leaves the tag exactly as written, so it keeps its src and data attributes, which is where the script reads its options from.

  • Yes. The client router updates the URL with history.pushState, which the script watches, so each navigation is a pageview and the back and forward buttons are counted too.

  • Yes. The tag is in the built HTML of every page that uses the layout, and the script runs in the visitor's browser. No server code is needed.

  • CrispStats sets no cookies and writes nothing to the visitor's device by default, and the IP address is discarded once the daily visitor hash is computed. A static Astro build sets no cookies of its own, so what is left to check is anything else your pages embed. This is not legal advice.

  • No more than full page loads would. Each view-transition navigation is one pageview, the same count a site without the client router gets. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

Not answered here? Write to hello@crispstats.com

Go further

Related

  • Privacy-friendly analytics for bloggers

    See which posts get read to the end, where readers come from, including AI assistants, and keep your site free of analytics cookies.

  • Core Web Vitals in plain English

    What LCP, INP and CLS measure, the good and poor thresholds for each, why the 75th percentile matters, and the fixes that usually move them.

  • Free UTM builder

    Build UTM-tagged links one at a time or in bulk from pasted rows, with presets, checks for capitals and spaces, CSV export and a channel preview.

  • SvelteKit analytics

    Add CrispStats to a SvelteKit app with one script tag in src/app.html.

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.