> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Add CrispStats to a Ruby on Rails app

> Install CrispStats in a Rails app by adding the script tag to app/views/layouts/application.html.erb. Turbo Drive page changes are tracked for you.

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

```erb app/views/layouts/application.html.erb theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for(:title) || "My app" %></title>
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag "application" %>
    <script
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <%= yield %>
  </body>
</html>
```

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

| Navigation | What the script sees |
| - | - |
| A full page load | One pageview. |
| A Turbo Drive visit | Turbo pushes the new URL with `history.pushState`; the script counts it as a pageview with the previous page as referrer. |
| A Turbo replace visit (`data-turbo-action="replace"`) | Turbo uses `history.replaceState`; a new URL is counted, the same URL is not. |
| A Turbo Frame update that does not change the URL | Not a pageview. |

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](https://docs.crispstats.com/tracking/single-page-apps.md).

## Send custom events

A `data-cs-event` attribute counts clicks with no JavaScript:

```erb app/views/pricing/show.html.erb theme={"theme":{"light":"github-light","dark":"github-dark"}}
<%= link_to "Start free", new_registration_path, data: { cs_event: "signup click", cs_prop_plan: "pro" } %>
```

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](https://docs.crispstats.com/track/declarative-events.md).

## 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:

```ruby app/services/crispstats.rb theme={"theme":{"light":"github-light","dark":"github-dark"}}
require "net/http"
require "json"

response = Net::HTTP.post(
  URI("https://in.crispstats.com/e"),
  {
    k: "cs_pk_xxxxxxxx",
    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",
)
```

A `202` means the event was accepted. See [server-side events](https://docs.crispstats.com/track/server-side-events.md) and, for payments from your own billing, the [payment API](https://docs.crispstats.com/revenue/payment-api.md).

## FAQ

> [accordion] Is there a gem?
>
> No. The script tag and, for server events, one HTTP request are the whole integration.

> [accordion] 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>`.

> [accordion] 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](https://docs.crispstats.com/tracking/proxy.md).

## Next steps

> [card] [Server-side events](https://docs.crispstats.com/track/server-side-events.md)
>
> Send renewals and other backend actions.

> [card] [Proxy with Caddy](https://docs.crispstats.com/tracking/proxy/caddy.md)
>
> Serve the script from your own domain.

> [card] [Identify signed-in users](https://docs.crispstats.com/track/identify-users.md)
>
> Tie visits to your own user id.

> [!NOTE]
>
> 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](https://guides.rubyonrails.org/layouts_and_rendering.html) and [https://turbo.hotwired.dev/handbook/drive](https://turbo.hotwired.dev/handbook/drive) on 2026-11-23.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.