# Ruby on Rails analytics with CrispStats

Updated 2026-11-23

Rails analytics comes down to one CrispStats script tag inside the head of app/views/layouts/application.html.erb, then a deploy. Turbo Drive changes pages with history.pushState, which the script watches, so every visit is counted without a gem or extra JavaScript.

## Steps

### 1. Copy the snippet with your key

In the dashboard, open your site's **Settings**, select **Tracking code** and choose the **HTML** tab. It shows the tag with your public key (it starts with `cs_pk_`) and your domain filled in. There is no gem or npm package to install.

### 2. Paste the tag into the application layout

Open `app/views/layouts/application.html.erb`, the layout Rails uses when a controller names no other one ([Rails guide on layouts](https://guides.rubyonrails.org/layouts_and_rendering.html)). Paste the tag inside `<head>` 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.

`app/views/layouts/application.html.erb`

```erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for(:title) || "My app" %></title>
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag "application" %>
    <script
      defer
      data-site="cs_pk_your_site_key"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <%= yield %>
  </body>
</html>
```

### 3. Deploy and open your live app

`bin/rails server` runs on `localhost`, which the script ignores on purpose. Deploy, then open your live app in a normal browser window.

### 4. Count clicks from a view (optional)

Rails turns `cs_event` into `data-cs-event` and `cs_prop_plan` into `data-cs-prop-plan`, so a link helper becomes a [custom event](/glossary/custom-event) with no JavaScript. From a Stimulus controller, call `window.crispstats?.("signup", { plan: "pro" })` instead.

`app/views/pricing/show.html.erb`

```erb
<%= link_to "Start free", new_registration_path,
      data: { cs_event: "signup click", cs_prop_plan: "pro" } %>
```

### 5. Send events from your server (optional)

Server-side events need a secret API key (`cs_sk_…`) with the **Events** scope, kept in your credentials or environment, never in a view. Pass the visitor id you saved from `crispstats.id()` so the event joins that visitor's history. Payments from your own billing go through the [payment API](/docs/revenue/payment-api).

`app/services/crispstats.rb`

```ruby
require "net/http"
require "json"

response = Net::HTTP.post(
  URI("https://in.crispstats.com/e"),
  {
    k: "cs_pk_your_site_key",
    t: "event",
    n: "subscription_renewed",
    u: "",
    p: { plan: "pro" },
    vid: subscription.crispstats_visitor_id,
  }.to_json,
  "Authorization" => "Bearer #{ENV.fetch("CRISPSTATS_API_KEY")}", # cs_sk_…
  "Content-Type" => "application/json",
)
# 202 means the event was accepted.
```

## Check that it works

Open your live app with the developer tools on the **Network** tab. You should see `d.js` load from `in.crispstats.com` and a `POST` to `/e` that answers `202`, on the first load and again on each Turbo visit. In the dashboard, **Settings**, **Tracking code** shows **Receiving events.** and the visit appears on **Realtime** within seconds.

## What you get

- One pageview per full page load and per Turbo Drive visit, with the previous page as the referrer, so entry pages, exit pages and visitor journeys stay correct.
- Sources and channels, including UTM campaigns and AI assistants such as ChatGPT and Perplexity, with countries, devices and browsers.
- Funnels and goals from the paths and events you already have, calculated over existing data.
- Server events such as renewals in the same reports as browser events, tied to the visitor through `crispstats_visitor_id`.
- A core script of about 2.2 KB gzipped, with optional web vitals and JavaScript errors switched on by `data-vitals` and `data-errors`.

## Notes for Ruby on Rails

- How Turbo navigation is counted, from [Turbo's Drive handbook](https://turbo.hotwired.dev/handbook/drive) and the script's History API listener: an advance visit is a pageview; a replace visit (`data-turbo-action="replace"`) counts only when the URL changes; a Turbo Frame update that leaves the URL alone is not a pageview.
- Turbo merges `<head>` on each visit and keeps an unchanged script tag in place, so the script starts once. Do not add `data-turbo-track="reload"` to the tag: the script URL does not change with your deploys.
- CrispStats tracks errors in the browser only. Exceptions raised in Ruby are not captured, and there are no source maps or breadcrumbs.
- To count visitors whose ad blocker stops the collector host, add one forwarding rule on nginx, Caddy or Cloudflare ([Worker guide](/integrations/cloudflare)) in front of Rails: see [the proxy guide](/docs/tracking/proxy).

Full reference: [Ruby on Rails in the docs](https://docs.crispstats.com/install/rails)

## Frequently asked questions

### Is there a gem for Rails analytics?

No, and you do not need one. The script tag in your layout and, for server events, one HTTP request with Net::HTTP are the whole integration.

### 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 the head.

### Do I need a cookie banner for a Rails app?

CrispStats sets no cookies and stores nothing on the device by default, and IP addresses are discarded after the daily hash. Rails sets its own session cookie when your app uses sessions, sign-in or CSRF protection, so the question is about that cookie and whatever else your app loads. This is not legal advice.

### What does it cost for a Rails app?

Turbo visits count like full page loads, one event each, and events you send from Ruby with Net::HTTP count too. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

## Related

- [Web analytics for SaaS products](/for/saas)
- [Visitor journeys](/features/visitor-journeys)
- [Funnels and goals](/features/funnels)
- [Cookieless tracking explained](/glossary/cookieless-tracking)
- [Laravel analytics](/integrations/laravel)
- [Django analytics](/integrations/django)
