Skip to content

Frameworks

Ruby on Rails analytics with CrispStats

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.

Updated

Set up CrispStats on Ruby on Rails

  1. 01

    Step 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. 02

    Step 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). 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.

    Replace the highlighted values with your own.

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

    Step 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. 04

    Step 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 with no JavaScript. From a Stimulus controller, call window.crispstats?.("signup", { plan: "pro" }) instead.

    app/views/pricing/show.html.erb
    <%= link_to "Start free", new_registration_path,data: { cs_event: "signup click", cs_prop_plan: "pro" } %>
  5. 05

    Step 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.

    app/services/crispstats.rb
    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 the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • 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 and limits

    • How Turbo navigation is counted, from Turbo’s Drive handbook 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) in front of Rails: see the proxy guide.

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

  • 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.

  • 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.

  • 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.

  • 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.

Not answered here? Write to hello@crispstats.com

Go further

Related

  • Web analytics for SaaS products

    See which channels bring paying customers, where trials stall in the funnel, and how your app performs for real users, from one cookieless script.

  • Visitor journeys

    Follow a visit page by page, from first referrer to purchase.

  • Funnels and goals

    Find the step where people leave, and count what matters.

  • Cookieless tracking explained

    Cookieless tracking is web analytics that counts visitors without storing a cookie or any other identifier on their device.

  • Laravel analytics

    Add CrispStats to a Laravel app with one script tag in your Blade layout, then send server-side events with Laravel's HTTP client.

  • Django analytics

    Add CrispStats to a Django site with one script tag in base.html, the template your pages extend, then send server-side events from Python.

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.