Skip to content
Docs menuInstall CentPath › Install the script

Add CentPath to Astro

On this page

Add CentPath to the layout your Astro pages share, so every page built from it counts its visits.

1. Copy your snippet

Astro needs the snippet with is:inline on both tags, so copy this one. Put your site's own id, from Setup → Install, in place of YOUR_SITE_ID.

<script is:inline>window.analytics = window.analytics || Object.assign([], { q: [], goal: function () { this.q.push(arguments); } });</script>
<script is:inline defer src="https://centpath.app/script/p.js" data-site-id="YOUR_SITE_ID"></script>

2. Add it to your layout's head

  1. Open the layout your pages share, the component that holds <head>. Astro projects commonly keep layouts in src/layouts.
  2. Paste the snippet inside <head>, before </head>.
  3. Keep is:inline on both tags. Without it, Astro bundles a <script> that has no other attribute into a module of its own, and the first line would no longer run as the page is read. With it, Astro leaves each tag exactly as written.
  4. Keep the two lines together, the first above the second. The first line keeps any goal your page sends before CentPath's script has loaded, and the script sends it once it runs. Set up goals shows how to send one.

If some of your pages don't use this layout, add the snippet to their own <head> too.

3. Build and deploy your site

Build and deploy your site the way you always do, so your live pages carry the snippet.

Verify the setup

  1. Open your live site in a new tab and load any page.
  2. Back in CentPath, look under the steps in Setup → Install. Waiting for your first pageview… turns into First pageview received. once the visit arrives.
  3. If it said First pageview received. before you started, your site has had visits already: look for this one in your dashboard instead.
  4. Open your dashboard, or reload it: the visit is in Visitors and in the Visits feed, usually within a minute.

If your site uses Astro's <ClientRouter /> to change pages without loading a new one, CentPath counts each new path as a page view too, since the router changes the address with the browser's history. Visits from localhost count too, whenever your development server's pages load the snippet.

Troubleshooting

Setup still waits for the first pageview

  1. Load a page of your live site, not your development server.
  2. Open that page's source in your browser and search for your site's id. If it isn't there, that page doesn't use the layout that holds the snippet: add it to the layout the page uses.
  3. If your site sends a Content-Security-Policy, allow https://centpath.app in its script-src and connect-src, and let the snippet's first line run (a nonce or a hash in script-src).

A goal from your page throws an error

The error means window.analytics doesn't exist yet: the first line didn't run before your page's code. Check that it still has is:inline, and sits above your page's own scripts in <head>.

For ad blockers, a wrong site id and other causes, see Troubleshooting.