# Ruby on Rails analytics

> Live analytics for Rails and any Rack app: add the gem and see page views, Devise sign-ins, crawler visits, 404s, errors and your own events as they happen.

Source: https://status.mettr.app/integrations/rails

## Add the gem

```ruby
gem "mettr"
```

```erb
<head>
  <%= mettr_script_tag %>
</head>
```

Set `METTR_SECRET_KEY` and `METTR_PUBLIC_KEY`: in Rails the middleware puts itself at the top of the stack and sends from production only. Sinatra, Roda and Hanami add `Mettr::Rack`. Ruby 3.2 or later, no dependencies.

## What you get without writing code

- **Page views and who is online**, from the [browser script](/docs/browser-script), served from `/_live` on your own domain, so ad blockers mostly let it through.
- **Sign-ins** through Devise, as `login`.
- **Unhandled exceptions** from Rails' error reporter, by class only, never the message.
- **Crawler visits**, with their page and status, and **404s** of people.
- **Response times** of one request in twenty, by route.

## Your own events

```ruby
Mettr.event("signup", {plan: "pro"}, user_id: user.id)
```

During a request, an event carries its page, address and browser. Events go from a background thread, never in the request's way.

## Revenue from your own billing

[Stripe](/integrations/stripe), [Paddle](/integrations/paddle) and [Polar](/integrations/polar) are read by mettr itself. Billing of your own? `Mettr.subscription` sends each subscription as it changes, and MRR, churn and revenue by source follow.

## Open source

The `mettr` gem is MIT licensed, on [GitHub](https://github.com/mettrhq/mettr-ruby) and [RubyGems](https://rubygems.org/gems/mettr). [The full guide](/docs/ruby).
