Docs menuInstall CentPath › Install the script
Add CentPath to Ruby on Rails
On this page
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:
<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
- 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. - Paste the snippet inside
<head>, before</head>. ERB leaves it as it is, since it holds no<% %>tag, unless you add a nonce. - 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 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
- Open your live site in a new tab and load any page.
- Back in CentPath, look under the steps in Setup → Install. Waiting for your first pageview… turns into First pageview received. once the visit arrives.
- If it said First pageview received. before you started, your site has had visits already: look for this one in your dashboard instead.
- 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
- Load a page of your live site, not your development server.
- 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.
- If your app sends a Content-Security-Policy, from
config/initializers/content_security_policy.rb, allowhttps://centpath.appin itsscript_srcandconnect_src, and let the snippet's first line run: give its tagnonce="<%= content_security_policy_nonce %>", or add its hash toscript_src.
For ad blockers, a wrong site id and other causes, see Troubleshooting.