# Add CentPath to Vue and Nuxt

> Paste the snippet into a Vue app's index.html, or add it to app.head in nuxt.config.ts, and count every page your router opens.

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

## Before you start

This guide covers both. If your app has a `nuxt.config.ts`, follow the Nuxt steps. If Vite builds it from an `index.html`, follow the Vue with Vite steps.

In CentPath, open your site, go to **Setup** → **Install** and pick Vue and Nuxt: the code there already holds your site's id.

## 1. Add the snippet to your app

The snippet's first line is a one-line queue: it keeps any goal your components send before CentPath's script has loaded, and the script sends it once it runs. Its second line loads CentPath's script with your site's id. Keep both, the queue first. [Set up goals](https://centpath.app/docs/goals) shows how to send a goal.

### Vue with Vite

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:

```html
<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>
```

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 you copied inside `<head>`, before `</head>`. Vite leaves both lines as they are: it bundles only scripts of `type="module"`.

### Nuxt

1. Open `nuxt.config.ts` at the root of your project.
2. Add the two entries of `script` under `app.head`, beside what your config already holds. Nuxt writes `app.head` into the head of every page.

Put your site's own id, from **Setup** → **Install**, in place of `YOUR_SITE_ID`:

```ts
export default defineNuxtConfig({
  app: {
    head: {
      script: [
        { innerHTML: "window.analytics = window.analytics || Object.assign([], { q: [], goal: function () { this.q.push(arguments); } });" },
        {
          src: "https://centpath.app/script/p.js",
          defer: true,
          "data-site-id": "YOUR_SITE_ID",
        },
      ],
    },
  },
});
```

The first entry is the queue, an inline script that runs as the page is read. The second loads CentPath's script with `defer`, so it runs once the page is read, after the queue.

## 2. 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: Vue Router does in its HTML5 mode (`createWebHistory`), the mode Vue's docs recommend, and Nuxt's router does unless you turn on its `hashMode`. 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 queue 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`: Vue Router's `createWebHashHistory`, or Nuxt's `hashMode`. CentPath reads the path alone, the part before any `#`, so every page counts as that one path. Use `createWebHistory`, or turn `hashMode` off, so each page has its own path.

For ad blockers, a wrong site id and other causes, see [Troubleshooting](https://centpath.app/docs/troubleshooting).
