Skip to main content
To add CrispStats to a Rails app, paste the script tag inside <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
Replace the key and the domain with yours. Keep the tag as plain HTML rather than 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.js loading from in.crispstats.com and a POST to https://in.crispstats.com/e on the first load and on each Turbo visit. The collector answers 202.
  • 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

A data-cs-event attribute counts clicks with no JavaScript:
app/views/pricing/show.html.erb
Rails turns 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
A 202 means the event was accepted. See server-side events and, for payments from your own billing, the payment API.

FAQ

No. The script tag and, for server events, one HTTP request are the whole integration.
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>.
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.
Last modified on November 23, 2026