Stickybeak Docs Dashboard Get early access

Backends

Ruby on Rails

Rails analytics with Stickybeak starts by adding its script to your rendered pages; this guide shows you where it belongs and how to track page views, goals and purchase revenue. It is a lighter alternative to Google Analytics for Rails, with live visitors, goals and revenue attribution in one script.

Add the script

  1. Configure the site ID for production

    Copy the ID from Settings in the dashboard and set it as STICKYBEAK_SITE_ID where your app runs (Kamal secrets, Heroku config vars and so on).

    config.x.stickybeak_site_id = ENV["STICKYBEAK_SITE_ID"]
  2. Add it to your layout

    Put this before </head>:

    <script>window.stickybeak = window.stickybeak || function () { (window.stickybeak.q = window.stickybeak.q || []).push(arguments); };</script>
    <% if (stickybeak_site = Rails.configuration.x.stickybeak_site_id).present? %>
      <script
        defer
        data-site="<%= stickybeak_site %>"
        data-domain="example.com"
        src="https://app.usestickybeak.com/_cfw/s.js"
      ></script>
    <% end %>

Check it's working

Publish first, then follow Verify your install.

Track goals

Give any button or link a data-goal attribute. Extra data-goal-* attributes become properties. See Custom goals.

<%= link_to "See pricing", pricing_path, data: { goal: "view_pricing", goal_from: "hero" } %>

Fire a goal after a redirect

When a controller action saves something and redirects, put the goal in the session. A small helper takes it out again when the next page renders.

class ApplicationController < ActionController::Base
  helper_method :stickybeak_goal

  private

  def track_goal(name, props = {})
    session[:stickybeak_goal] = { "name" => name, "props" => props }
  end

  def stickybeak_goal
    @stickybeak_goal ||= session.delete(:stickybeak_goal)
  end
end
def create
  @user = User.new(user_params)
  if @user.save
    track_goal "signup", plan: @user.plan
    redirect_to dashboard_path
  else
    render :new, status: :unprocessable_entity
  end
end
<% if stickybeak_goal %>
  <script>window.stickybeak(<%= raw stickybeak_goal["name"].to_json %>, <%= raw (stickybeak_goal["props"] || {}).to_json %>);</script>
<% end %>

Track purchases

track_goal "purchase", revenue: order.total.to_s, currency: order.currency, order_id: order.number
redirect_to order_path(order)

See Revenue.

Page changes

Turbo counts different-path visits and Back or Forward. It excludes replace, query-only changes and ordinary Frames; data-turbo-action="advance" can promote a Frame visit. See Turbo, htmx and friends.

If it isn't working

  • Keep the head tag identical between pages. CSP nonces must cover inline calls, with Stickybeak allowed in script-src and connect-src.

See Troubleshooting.