Resources

Summary

# Claude Terminal
/analytics How many signups per month this year?
/analytics Paid revenue broken down by product.
/analytics Compare paid vs refunded order counts.
/analytics Which country has the most customers?

# .claude/commands/analytics.md
---
description: Answer a business question about the app's data by running read-only ActiveRecord queries via bin/rails runner, one at a time, until you can back the answer with real numbers.
argument-hint: a question about customers/orders, e.g. "which plan generates the most revenue?"
---

You are a data analyst with terminal access to this Rails app. Answer the following business question by **actually querying the data** — never guess or fabricate numbers.

## Question

$ARGUMENTS

## How to work

Treat this like a tool-calling loop, not a single guessed query. Run **one query, look at the real output, then decide the next query based on what you saw.**

1. **Inspect the schema first** (unless you already know it from earlier in this conversation). Don't assume column or association names. For example:

   ```bash
   bin/rails runner 'puts Customer.column_names.inspect; puts Order.column_names.inspect'
   ```

   Adjust to whichever models the question involves. If you're unsure which models exist, list them (e.g. `ApplicationRecord.descendants.map(&:name)` after `Rails.application.eager_load!`, or look at `app/models`).

2. **Query iteratively.** Run one read-only ActiveRecord query at a time with `bin/rails runner`, inspect the actual output, and let it inform the next query. Narrow down toward the answer instead of trying to get everything in one shot.

3. **Stop when you can answer** — or after **5–6 queries at most.** If you still can't fully answer by then, stop and report your **partial findings with the numbers you do have**, plus what remained unresolved. Do not keep looping.

## Rules

- **Read-only. No exceptions.** Do not call `save`, `create`, `update`, `update_all`, `destroy`, `delete`, `delete_all`, `insert`, `import`, or any other method that writes. Do not use raw `execute`, and never issue `UPDATE`/`INSERT`/`DELETE`/`DROP`/`ALTER`/`TRUNCATE`. If a query would mutate anything, don't run it.
- **Prefer ActiveRecord query methods** — `group`, `where`, `sum`, `count`, `average`, `order`, `limit`, `joins`, `pluck` — over hand-written SQL strings.
- **`amount_cents` is stored in cents.** Divide by 100.0 and format as currency (e.g. `$1,234.56`) whenever you present money to the user. Compute in cents, convert only for display.
- Keep each `bin/rails runner` invocation focused and print its result clearly (use `puts` / `.inspect` / `pp`) so you can read the real values.

## Final answer

End with a **direct answer to the question, backed by the actual numbers** you retrieved. When the question has multiple parts or comparisons (e.g. per-plan, per-status), include a short **Markdown table or bullet breakdown**. Show money in dollars, not cents.