Overview
Icon for Analytics Studio

Analytics Studio

Sales reports, live view and SQL in Admin



Medusa Analytics Studio

Sales reports, dashboards, a live view and a read-only SQL editor for the Medusa Admin.

Features · Install · Options · Reporting views · Query language · Safety


Copy to clipboard@306technologies/medusa-analytics-studio · Analytics for the Medusa Admin: sales reports, a customisable dashboard, a live view of today's orders and an optional read-only SQL editor. Reports read from pre-computed sales facts, not raw orders, on their own read-only connection pool, so they never slow down checkout.



Built by 306 Technologies
Medusa & headless commerce experts. Senior-led engineering, accelerated by AI.




Features



Dashboard

Reports

Live view

SQL mode

  • Dashboard: drag-and-drop tiles, each a saved report with its own chart.
  • Reports: built-in reports and your own, written in a small query language with a visual editor and 19 chart types.
  • Live view: today's orders, active carts and checkouts on a globe, refreshed every 10 seconds.
  • SQL mode (optional, off by default): admin-written SQL over curated reporting views, as a restricted read-only Postgres role.
  • Sales facts: per-order and per-line facts plus daily rollups, kept current from order and refund events, with currency conversion and a store-wide reporting timezone.
  • Read-only by design: see Safety.


Requirements

  • Medusa Copy to clipboard2.19.0 or later (any 2.x from 2.19). Keep all Copy to clipboard@medusajs/* packages in your project on the same version.
  • PostgreSQL (tested with 16).
  • Redis recommended in production (Copy to clipboardREDIS_URL): report caching, queued events and one-instance-at-a-time jobs.


Install

1. Add the package

yarn add @306technologies/medusa-analytics-studio

2. Register the plugin

Register the plugin, then list its modules under Copy to clipboardmodules so each one gets its own options. A plugin's Copy to clipboardoptions would otherwise go to every module alike.

// medusa-config.ts
import { defineConfig } from "@medusajs/framework/utils"
const plugin = "@306technologies/medusa-analytics-studio"
module.exports = defineConfig({
// projectConfig: { ... },
plugins: [{ resolve: plugin, options: {} }],
modules: [
{
resolve: `${plugin}/modules/analytics-workspace`,
options: { readOnly: false },
},
{
resolve: `${plugin}/modules/sales-facts`,
options: { jobsEnabled: true },
},
{
resolve: `${plugin}/modules/analytics-reader`,
options: {

SQL mode stays off until you set Copy to clipboardANALYTICS_SQL_DISABLED=false and Copy to clipboardANALYTICS_SQL_PASSWORD. It runs on the replica in Copy to clipboardANALYTICS_DATABASE_URL, or on the primary only with Copy to clipboardANALYTICS_SQL_ALLOW_PRIMARY=true. TLS is on unless you set Copy to clipboardANALYTICS_SQL_SSL=false (for example, a local database without TLS). Add the names to your Copy to clipboard.env.template:

ANALYTICS_DATABASE_URL=
ANALYTICS_SQL_DISABLED=true
ANALYTICS_SQL_PASSWORD=
ANALYTICS_SQL_ALLOW_PRIMARY=false
ANALYTICS_SQL_SSL=true

3. Migrate and start

Then run the migrations and start Medusa:

npx medusa db:migrate
npx medusa develop
Large store? On a large store (hundreds of thousands of orders or more), build the analytics indexes on Copy to clipboardorder and Copy to clipboardcart without blocking checkout before the first Copy to clipboarddb:migrate; the migration then finds them and does nothing:

psql "$DATABASE_URL" -f node_modules/@306technologies/medusa-analytics-studio/sql/create-indexes-concurrently.sql
First run: the first backfill of sales facts runs within five minutes of boot. Until it finishes, reports show that they are still filling.

4. Set your reporting timezone

Reports decide which day (and hour) an order belongs to using one store-wide reporting timezone. It starts as Copy to clipboardUTC, so an order placed at 11pm in New York lands on the next day.

Set it once in Settings → Store → Reporting timezone to the timezone you run the business in. It applies to every admin user and every report.

Changing it later is safe:

  • Facts rebuild in the background in the new timezone, within a few minutes (longer on a large store).
  • Until the rebuild finishes, reports keep the old timezone and show a "Switching to …" notice.
  • Past days are re-split, so daily numbers shift. Totals over a period don't change.


Options

Copy to clipboardanalytics-reader

Option Default Description Copy to clipboarddatabaseUrl the main database Where reports read from, for example a read replica. Copy to clipboardseparateDatabase Copy to clipboardfalse Copy to clipboardtrue when Copy to clipboarddatabaseUrl is not the main database. Requires Copy to clipboarddatabaseUrl. Copy to clipboardpoolMax Copy to clipboard3 Most connections the report pool may hold. Copy to clipboardstatementTimeoutMs Copy to clipboard15000 Postgres cancels any report query running longer than this. Copy to clipboardssl Copy to clipboardfalse TLS for the report pool. Encrypts, but does not verify the server certificate.

Copy to clipboardsales-facts

Option Default Description Copy to clipboardjobsEnabled Copy to clipboardtrue Run the backfill, drain, reconcile and exchange-rate jobs. Set Copy to clipboardfalse on instances that should leave them to another. Copy to clipboardfxUrl Frankfurter's public API A Frankfurter-compatible Copy to clipboardhttps URL for exchange rates, or Copy to clipboard"off". Copy to clipboarddatabase the shared connection Medusa module database options, to give the facts their own small pool.

Copy to clipboardanalytics-workspace

Option Default Description Copy to clipboardreadOnly Copy to clipboardfalse The host refuses writes (for example a public demo): the admin keeps saved reports and the dashboard in the browser. Copy to clipboardhideCredit Copy to clipboardfalse Hides the small "Built by 306 Technologies" line under the dashboard.

Copy to clipboardanalytics-sql

SQL mode stays off unless Copy to clipboarddisabled is explicitly Copy to clipboardfalse, a password is set, and the role and replica checks pass.

Option Default Description Copy to clipboarddisabled required Copy to clipboardtrue turns SQL mode off whatever else is set. Copy to clipboardpassword none Password for the Copy to clipboardreporting_reader role: at least 16 printable ASCII characters, no spaces. Copy to clipboardanalyticsDatabaseUrl none The read replica SQL mode runs on. Copy to clipboardallowPrimary Copy to clipboardfalse Run on the primary when there is no replica. Copy to clipboardmainDatabaseUrl none The primary, where the role and its grants are set up. Copy to clipboardreportingViews none Views the role may read: names, Copy to clipboard"prefix_*" or Copy to clipboard"*". Copy to clipboardpoolMax Copy to clipboard2 Connections per instance. Copy to clipboardconnectionLimit Copy to clipboard5 The role's connection limit. Copy to clipboardssl required TLS for SQL-mode connections. Encrypts, but does not verify the server certificate.

When the app's database user can create roles, the role is set up at boot. Otherwise add this one-line script to your app, so the command the admin suggests has a stable path:

// src/scripts/setup-sql-role.ts
export { default } from "@306technologies/medusa-analytics-studio/scripts/setup-sql-role"

Then print the statements for a database admin:

yarn medusa exec ./src/scripts/setup-sql-role.ts print

The output holds the password's SCRAM verifier: treat it as a secret. Use a random password of 32 or more characters.


Add your own views

SQL mode reads only views in the Copy to clipboardreporting schema, so data from your own modules (loyalty points, subscriptions, B2B accounts) needs a view there. The plugin ships Copy to clipboardsales_daily and Copy to clipboardanalytics_reports_daily. Build yours with the same helper, in a migration of one of your app's modules:

// src/modules/loyalty/migrations/Migration20260101000000.ts
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
import {
dropReportingView,
reportingView,
} from "@306technologies/medusa-analytics-studio/shared/analytics/reporting-view"
export class Migration20260101000000 extends Migration {
override async up(): Promise<void> {
this.addSql(
reportingView({
name: "loyalty_points_daily",
comment: "Loyalty points earned and spent per day",
sql: `
select created_at::date as day,
sum(points) filter (where points > 0) as earned,
-sum(points) filter (where points < 0) as spent
from loyalty_transaction
where deleted_at is null
group by 1`,
  • Copy to clipboardreportingView creates the Copy to clipboardreporting schema if needed, then Copy to clipboardcreate or replace view reporting.<name> with your Copy to clipboardselect, and the view and column comments. The SQL editor shows those comments in its views panel.
  • Copy to clipboardname and column names: lowercase letters, digits and Copy to clipboard_, starting with a letter, at most 63 characters.
  • Copy to clipboardsql is one Copy to clipboardselect (or Copy to clipboardwith ... select). A trailing Copy to clipboard; is dropped.
  • The view runs with its owner's rights, so it may join any table, and SQL mode sees only the columns you select. Leave out customer details (emails, names, addresses) unless admins should query them.
  • Copy to clipboardcreate or replace can only add columns at the end. To rename, remove or reorder columns, call Copy to clipboarddropReportingView(name) first in the same migration.

Then:

  1. Run Copy to clipboardnpx medusa db:migrate.
  2. Add the view to Copy to clipboardreportingViews (Copy to clipboard"loyalty_points_daily", or a prefix such as Copy to clipboard"loyalty_*"). A view that isn't listed shows as "Not enabled yet" in the views panel, and SQL mode can't read it.
  3. Restart the app. Grants are applied at boot, on the primary: the role is granted Copy to clipboardselect on each listed view and loses access to every other view in the schema. Running the Copy to clipboardsetup-sql-role script without Copy to clipboardprint applies them too.

On a streaming read replica, the views and grants arrive by replication. They don't need to be created there.


Query language

FROM sales
SHOW total_sales, orders
GROUP BY month
SINCE -12m UNTIL today
COMPARE TO previous_year
ORDER BY total_sales DESC
LIMIT 10
CURRENCY eur
  • Metrics: Copy to clipboardorders, Copy to clipboardgross_sales, Copy to clipboarddiscounts, Copy to clipboardreturns, Copy to clipboardrefunds, Copy to clipboardnet_sales, Copy to clipboardshipping, Copy to clipboardtaxes, Copy to clipboardtotal_sales, Copy to clipboardaverage_order_value, Copy to clipboardunits, Copy to clipboardreturned_units, Copy to clipboardnet_units, Copy to clipboardcustomers, Copy to clipboardnew_customers, Copy to clipboardreturning_customers.
  • Dimensions (up to two): Copy to clipboardday, Copy to clipboardweek, Copy to clipboardmonth, Copy to clipboardday_of_week, Copy to clipboardhour_of_day, Copy to clipboardcountry, Copy to clipboardcity, Copy to clipboardsales_channel, Copy to clipboardcustomer_type, Copy to clipboardproduct, Copy to clipboardvariant.
  • Dates: Copy to clipboardtoday, Copy to clipboardyesterday, Copy to clipboard-30d, Copy to clipboard-4w, Copy to clipboard-3m, Copy to clipboard-1y or Copy to clipboard2026-01-31.
  • Compare: Copy to clipboardprevious_period or Copy to clipboardprevious_year.

Reports can also be run without HTTP:

const reader = container.resolve("analyticsReader")
const result = await reader.runReport(
"FROM sales SHOW orders SINCE -6d UNTIL today",
{ currency: "eur", timezone: "UTC" }
)


Safety

Reports cannot write to your database. Each layer holds on its own:

  1. The query language accepts only its own clauses and listed metric and dimension names. The server builds the SQL; values are bound parameters.
  2. Every report query runs in its own transaction that starts Copy to clipboardSET TRANSACTION READ ONLY with a local statement timeout, so nothing a query does to the session can make a later one writable.
  3. The reader runs only a single Copy to clipboardSELECT or Copy to clipboardWITH statement with no write keywords, and refuses functions that could change the session, take locks or reach outside the database.
  4. Every report connection is opened with Copy to clipboarddefault_transaction_read_only = on, and one whose setting cannot be confirmed is discarded.

SQL mode runs as a role that can read only the listed views, inside a read-only transaction with a statement timeout, a 5,000-row cap and a 10 MB result cap. It turns itself off if the role can read anything else, or create objects on the primary.

The plugin does not refuse writes for a public demo by itself. Pair Copy to clipboardreadOnly with your own write guard.


Admin

  • Analytics Studio in the sidebar, at Copy to clipboard/app/analytics-studio: dashboard, reports, live view.
  • SQL at Copy to clipboard/app/analytics-studio/sql, only when SQL mode is on.
  • Reporting timezone widget on the store details page (see step 4).


Development

npm install
npm run build # compile to .medusa/server
npm run dev # rebuild on change, for a Medusa app linked to this plugin
npm run test:unit
npm run check-types
npm run lint
npm run format

Generate migrations for a changed model with Copy to clipboardnpx medusa plugin:db:generate.

Every change needs an entry in CHANGELOG.md, and releases follow Semantic Versioning.

Issues and pull requests are welcome at github.com/306-Technologies/medusa-analytics-studio. Accepted changes are applied upstream and released from there, so a merged pull request appears in the next release.



License

MIT © 306 Technologies

You may also like

Browse all integrations

Build your own

Develop your own custom integration

Build your own integration with our API to speed up your processes. Make your integration available via npm for it to be shared in our Library with the broader Medusa community.

gift card interface

Ready to build your custom commerce setup?