# Add CentPath to Ruby on Rails

> Paste the snippet into the application layout your Rails views render in, so every page counts its visits.

Add CentPath to your Rails app's application layout, so every page rendered in it 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:

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

## 2. Paste it into application.html.erb

1. Open `app/views/layouts/application.html.erb`. Rails renders your views in this layout unless a controller has a layout of its own, so every page loads what its head holds.
2. Paste the snippet inside `<head>`, before `</head>`. ERB leaves it as it is, since it holds no `<% %>` tag, unless you add a nonce.
3. 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](https://centpath.app/docs/goals) shows how to send one.

If a controller has a layout of its own, add the snippet to that layout's `<head>` too.

## 3. Deploy your app

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.
4. Open your dashboard, or reload it: the visit is in **Visitors** and in the **Visits** feed, usually within a minute.

With Turbo Drive, a link changes the page without loading a new one, and Turbo pushes the new address onto the browser's history. CentPath counts each new path as a page view, as it does the visitor's Back and Forward. 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 renders in another layout: add the snippet to that layout too.
3. If your app sends a Content-Security-Policy, from `config/initializers/content_security_policy.rb`, allow `https://centpath.app` in its `script_src` and `connect_src`, and let the snippet's first line run: give its tag `nonce="<%= content_security_policy_nonce %>"`, or add its hash to `script_src`.

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