Skip to content
Docs menuInstall CentPath › Install the script

Add CentPath to SvelteKit

On this page

Add CentPath to src/app.html, the page template every SvelteKit page is rendered into, so every page counts its visits.

1. Copy your snippet

In CentPath, open your site, go to Setup → Install and click Copy. The snippet you copy holds your site's own id, so every visit counts for the right site:

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

2. Paste it into src/app.html

  1. Open src/app.html. SvelteKit renders each page into this template, so every page loads what its head holds.
  2. Paste the snippet inside <head>, before </head>. SvelteKit sends the template's own text as it is, apart from its %sveltekit.…% placeholders.
  3. Keep the two lines together, the first above the second. The first line keeps any goal your components send before CentPath's script has loaded, and the script sends it once it runs. Set up goals shows how to send one.

3. Build and deploy your app

Build and deploy your app 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. Reload it, and the visit shows in the Visits feed, usually within a minute.
  4. Open another page of your app through its links. SvelteKit changes the page without loading a new one, and CentPath counts each new path as a page view, as it does the visitor's Back and Forward.

By default, SvelteKit's router picks each page by the path of the address, so each page has a path of its own, and it changes the address with the browser's history. A change to the query string alone, like ?tab=2, isn't a new page view. 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, the build you deployed doesn't carry the snippet: build and deploy again.
  3. If your app sends a Content-Security-Policy, allow https://centpath.app in its script-src and connect-src. With SvelteKit's csp setting in nonce mode, add nonce="%sveltekit.nonce%" to the snippet's first tag, so the first line may run.

Every page counts as the same page

Your app sets router.type to hash in svelte.config.js, which keeps its pages after a # in the address, like /#/pricing. CentPath reads the path alone, the part before any #, so every page counts as that one path. Leave router.type at its default, pathname, so each page has its own path.

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