Skip to content
New Bring your history: import it from Google Analytics, Plausible, Simple Analytics, Umami, Matomo or GoatCounter, on every plan

Ruby

The mettr gem sends a Rails or Rack app's events, sign-ins, crawler visits, 404s, errors and response times, with no dependencies.

Ruby 3.2 or later, no dependencies. Rails is set up on its own; any other Rack app (Sinatra, Roda, Hanami) adds the middleware. The source is on GitHub, MIT licensed.

Install

gem "mettr"

The client reads the environment; Mettr.configure wins.

Variable What
METTR_SECRET_KEY the site's secret key (the site's settings, Tracking); keep it on the server
METTR_PUBLIC_KEY the site's public key, for the script tag and the proxy
METTR_ENABLED true or false; by default on once a key is set (Rails: only in production)
METTR_PROXY_PATH the path on your own domain, _live by default; empty loads the script from mettr
METTR_ENDPOINT where mettr runs, https://mettr.app

Rails

Add the gem and the two keys: the middleware goes to the top of the stack by itself. In the layout:

<head>
  <%= mettr_script_tag %>
</head>

Sign-ins through Devise (Warden) are sent as login, unhandled exceptions from Rails' error reporter, and each request's route with its response time. Who the user is, and who is staff, goes in config/initializers/mettr.rb:

Mettr.configure do |config|
  config.user = ->(env) { env["warden"]&.user&.id } # the default
  config.staff = ->(env) { env["warden"]&.user&.admin? }
end

Rack

require "mettr"

use Mettr::Rack, user: ->(env) { env["rack.session"]&.dig("user_id") }

Behind a proxy of your own whose address is REMOTE_ADDR, pass trust_forwarded: true. In a template: Mettr.client.script_tag.

The script and the proxy

The script tag loads the browser script from /_live/script.js and sends its events to /_live/events, which the middleware serves: ad blockers mostly leave first-party requests alone, and the events carry the visitor's address and user as your server saw them. Only what a browser may send passes.

What it sends on its own

  • Crawler visits: a crawler's request, with its page and status. mettr names the crawler and checks it against its owner's network.
  • 404s of people, as errors.
  • Exceptions, by their class only, never the message. A client's error (a status under 500) is not one.
  • Response times of one request in twenty, by route.
  • Nothing for /up (ignore), for staff, or while it is off.

Sending never raises into your app: a failed batch answers false and is passed to on_error, without its events.

Your own events

Mettr.event("signup", {plan: "pro"}, user_id: user.id)
Mettr.revenue("order", 49.5, "EUR", {items: 2})
Mettr.client.error("PaymentFailed", {provider: "stripe"})

During a request they carry its page, address and browser. Props are flat: up to 30, each a string, number, boolean or nil. Events wait a second to go together, sent from a background thread; a script or a job that ends calls Mettr.client.flush.

Subscriptions

When your billing is your own (anything but Stripe, Paddle or Polar, which mettr reads itself), send each subscription whenever it changes, and every one once for the history:

Mettr.subscription(
  id: subscription.id.to_s,
  status: "active", # active, past_due, trialing, paused, canceled
  amount: 19, # each cycle, before tax, in the main unit
  currency: "USD",
  interval: "month", # day, week, month, year
  user_id: user.id.to_s,
  plan: "Pro"
)

See the Subscriptions API for what each field means.

Your MCP server

If your app runs an MCP server, put Mettr::Mcp before its own auth, so a 401 that sends a client to sign in counts too:

# Rails: config/application.rb
config.middleware.use Mettr::Mcp, path: "/mcp"   # errors: true keeps failed calls' messages

# Rack
use Mettr::Mcp, path: "/mcp", user: ->(env) { env["warden"]&.user&.id }

path: takes a string, a regexp, or nil for every request that reaches it. The user is read once the server has answered (Mettr.configure's user, else Warden's). Each handshake, tool call, resource read and prompt becomes an mcp.* event with the client, how it ended and how long it took; never the arguments or the answer. Mettr::Rack sends no crawl, 404 or timing for the path. More in MCP analytics.

History and deploys

Mettr.client.import(rows) # each with ts and source_id: a second run adds nothing
Mettr.client.annotation("Deployed v2.4", url: "https://github.com/acme/app/releases/v2.4")

Rows are those of the Import API; notes are those of the Annotations API.

Updated · This page as Markdown · Something unclear? [email protected]

Search the docs, features, comparisons, blog and changelog