Skip to content
Docs menuInstall CentPath › Install the script

Add CentPath to React and Vite

On this page

Add CentPath to a React app built with Vite, so every page your app shows counts its visits.

Before you start

This guide is for a React app that Vite builds from its index.html. If your React app runs on Next.js, follow Add CentPath to Next.js instead.

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 index.html

  1. Open index.html at the root of your project. Vite builds your app from this page, so every page of your app loads what its head holds.
  2. Paste the snippet inside <head>, before </head>. Vite leaves both lines as they are: it bundles only scripts of type="module".
  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. Your app 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.

CentPath counts a page each time your router changes the path of the address with the browser's history, as React Router's createBrowserRouter does. 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 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).

Every page counts as the same page

Your router keeps its pages after a # in the address, like /#/pricing, as React Router's createHashRouter does. CentPath reads the path alone, the part before any #, so every page counts as that one path. Use a router that gives each page its own path, like React Router's createBrowserRouter.

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