<head> in app/views/layouts/application.html.erb, the layout Rails uses when a controller has no layout of its own. With Turbo Drive, page changes update the URL with history.pushState, which the script watches, so every visit is counted.
You need your site’s public key, which starts with cs_pk_. Find it in your site’s settings under Tracking code, on the HTML tab. The key is safe to publish. There is no gem or npm package to install.
Add the script
1
Paste the tag into the application layout
app/views/layouts/application.html.erb
javascript_include_tag, so its data- attributes stay exactly as written. If some controllers use their own layout, add the tag there too.2
Deploy and open your app
bin/rails server runs on localhost, which the script ignores. Deploy, then open your live app in a normal browser window.Check that it works
- In your browser’s developer tools, the Network tab shows
d.jsloading fromin.crispstats.comand aPOSTtohttps://in.crispstats.com/eon the first load and on each Turbo visit. The collector answers202. - In CrispStats, Settings, Tracking code shows Receiving events., and the visit appears in Realtime within seconds.
How page changes are counted
Turbo merges
<head> on each visit and leaves an unchanged script tag in place, so the script starts once and keeps watching. Do not add data-turbo-track="reload" to the CrispStats tag: the script URL does not change with your deploys. See single-page apps.
Send custom events
Adata-cs-event attribute counts clicks with no JavaScript:
app/views/pricing/show.html.erb
cs_event into data-cs-event and cs_prop_plan into data-cs-prop-plan. From a Stimulus controller, call window.crispstats?.("signup", { plan: "pro" }). See declarative events.
Send events from your server
Server-side events need a secret API key (cs_sk_…) with the Events scope, kept in your credentials or environment, never in a view:
app/services/crispstats.rb
202 means the event was accepted. See server-side events and, for payments from your own billing, the payment API.
FAQ
Is there a gem?
Is there a gem?
No. The script tag and, for server events, one HTTP request are the whole integration.
Are Turbo visits counted twice?
Are Turbo visits counted twice?
No. Each new URL is one pageview, and the same URL twice in a row is not counted again. The script itself runs once, because Turbo keeps the unchanged tag in
<head>.Can I serve the script from my own domain?
Can I serve the script from my own domain?
Yes. Add one forwarding rule on nginx, Caddy, Cloudflare or another server in front of your app. See proxy by host.
Next steps
Server-side events
Send renewals and other backend actions.
Proxy with Caddy
Serve the script from your own domain.
Identify signed-in users
Tie visits to your own user id.
Rails and Turbo details (the default
application.html.erb layout, Turbo Drive’s pushState and replaceState visits and its <head> merging) checked against https://guides.rubyonrails.org/layouts_and_rendering.html and https://turbo.hotwired.dev/handbook/drive on 2026-11-23.