# Debug console

URL: https://docs.analytics.remcostoeten.nl/docs/sdk/debugtools

> A console for admins on their own site that shows the numbers and the events of the page in front of them. It ships as @spoar/debugtools and visitors never download it.

`@spoar/debugtools` opens a console over your live site. Only you see it. It answers questions about the page you are on and the browser you are using:

* **This page**: visitors, pageviews, bounce rate and time on page for the current path, its top referrers, its Web Vitals and the issues raised on it. **Show clicks** outlines every element marked with `data-spoar-click` and pins its click count to it.
* **My events**: each event this tab sent since the console loaded, with whether the API accepted it, rejected it and why, or the SDK dropped it and why. **Server rejects** lists the project's recent rejected events, rate limited requests and refused origins.
* **Count my visits**: a switch that opts this browser out of tracking, or back in.
* **Annotate**: adds a chart annotation, such as a release or a post, that shows on the dashboard's charts.

[SQL](https://docs.analytics.remcostoeten.nl/docs/api/sql.md) is an opt-in tab through the `tabs` option, and the dashboard shows it. It is a separate package from `@spoar/sdk`, so the SDK's size budgets do not change.

## Try it here

This site runs the console. Signed in as an admin of this site's project, you see live data. Anyone else sees sample data.



## With @spoar/sdk

On a Next.js site that already uses `@spoar/sdk`, set `debugConsole` on `<Analytics>`. `@spoar/sdk` depends on `@spoar/debugtools`, so there is nothing else to install.

```tsx
import { Analytics } from "@spoar/sdk/next";

<Analytics debugConsole />;
```

Then turn on **Debug console** in the project's settings in the dashboard. Visitors get a keyboard listener and send no request. Press Ctrl+Shift+K (Cmd+Shift+K on a Mac) on the site: a popup on the API checks your session and hands the site a widget token, then closes. After that, the console opens on its own in that browser until the session ends. The popup works on any domain, because the session cookie is sent to the API's own page.

The console talks to the API behind an absolute `endpoint`, or to the hosted API when events go through the `/_ra` proxy. Pass `debugConsole={{ api: "https://api.example.com" }}` for a self-hosted API.

The console opens with a status line under its title bar: the project, whether the widget token is live and until when, and when each key was last used and from which origin. The key lines need a token that may read the project's settings; without one they are left out. Escape closes the console and puts focus back on the launcher.

If you press the shortcut and the browser blocks the popup, a notice asks you to allow pop-ups. When this browser opened the console before and the API now refuses it, such as when the project's **Debug console** switch is off, a notice shows the API's reason. A visitor who never opened the console sees nothing.

## Install

```bash
npm install @spoar/debugtools
```

`react` and `react-dom` 19 or later are peer dependencies of the React and Next entries.

## Requirements

The entries below call [`GET /v2/widget/session`](https://docs.analytics.remcostoeten.nl/docs/api/dev-widget.md#signing-in) for an admin check on every page load. The check passes when all four hold:

* Your browser has an API session as an owner or an admin of the project.
* The site's origin is in the project's allowed origins.
* The project's `debugConsole` switch is on.
* The API shares the site's registrable domain.

Visitors without that session load nothing past the loader.

A passing check returns a widget token (`wt_`) for that one project. The console sends it with every query and fetches a new one a minute before it expires. Queries need the project's `sqlEnabled` switch on. Without it the console shows the 403.

## Entries

### mount

```ts
import { mount } from "@spoar/debugtools";

mount(options: DebugtoolsOptions): () => void
```

Starts the console on any page, with no React on the host. The loader runs the admin check, then downloads the console chunk, which brings its own React. It returns a function that removes the console.

```ts
const unmount = mount({ endpoint: "https://api.example.com", project: "example.com" });
```

### Debugtools in React

```tsx
import { Debugtools } from "@spoar/debugtools/react";
```

A component that takes the same options as `mount`. It uses your app's React.

### Debugtools in Next.js

```tsx
import { Debugtools } from "@spoar/debugtools/next";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Debugtools endpoint="https://api.example.com" project="example.com" />
      </body>
    </html>
  );
}
```

For the App Router. It loads the console with `next/dynamic` and `ssr: false`.

### Console

```tsx
import { Console } from "@spoar/debugtools/console";
```

The console alone, with no loader and no admin check. Use it to embed the console in your own page. It needs one of the stylesheet entries below.

### Stylesheets

* `@spoar/debugtools/tailwind.css`: the console's theme tokens and base styles, for an app that runs Tailwind v4.
* `@spoar/debugtools/styles.css`: the compiled stylesheet, for an app without Tailwind.

### Fixtures

```ts
import { consoleFixture } from "@spoar/debugtools/fixtures";
```

Sample data for `<Console />` and for the `data` option.

## Options

```ts
type DebugtoolsOptions = {
  endpoint: string;
  project: string;
  auth?: "widget" | "session";
  launcher?: "left" | "right";
  fetch?: Fetcher;
  data?: ConsoleData;
  sections?: ConsoleSections;
  theme?: "dark" | "light" | "system";
  demo?: boolean;
  open?: boolean;
  tabs?: ("page" | "events" | "sql")[];
  host?: ConsoleHost;
};
```

### endpoint and project

`endpoint` is the API origin. `project` is the project id.

### auth

Default `"widget"`. With `"widget"`, the console runs the admin check and sends the widget token. With `"session"`, it skips the check and sends the browser's session cookie. Use `"session"` on a page on the API's own domain, such as the dashboard.

### launcher

Default `"left"`. The corner the **Console** pill sits in, `"left"` or `"right"`.

### fetch

Replaces `fetch` for the admin check and the queries.

### data

Shows this fixture data instead of the project's.

### sections

Turns parts of the console on or off: `nav`, `toolbar`, `editor`, `results`, `library`, `recent` and `agent`. Every section is on except `agent`.

```ts
mount({
  endpoint: "https://api.example.com",
  project: "example.com",
  sections: { agent: true, library: false },
});
```

### theme

Default `"dark"`. Use `"light"`, or `"system"` to follow the visitor's `prefers-color-scheme`.

### demo

Default `false`. When the admin check fails, shows the console on fixture data instead of nothing. Use it on a page that shows the console to everyone, such as these docs.

### open

Default `false`. Opens the console as soon as it mounts, instead of showing the launcher.

### tabs

Default `["page", "events"]`. The tabs to show, always in the order **This page**, **My events**, **SQL**. Add `"sql"` for the SQL console; `sections` applies to it.

### host

The page's `@spoar/sdk` client, created with `createAnalytics`. **My events** and **Count my visits** read it, so without it they explain that the page gave the console no client. `<Analytics debugConsole />` passes its own client.

```ts
mount({ endpoint: "https://api.example.com", project: "example.com", host: analytics });
```

## Using the console

A **Console** pill sits at the bottom left of the page. Click it, or press Ctrl+Shift+K (Cmd+Shift+K on a Mac), to open the console. Escape or the close button closes it.

The console opens as a panel docked to the bottom of the page. Drag its top edge to resize it. The title bar has three buttons:

* Maximize the console to the viewport.
* Pop it out as a floating window. Drag it by the title bar and resize it from a corner.
* Dock it back.

The browser remembers the mode, height, position and size.

**This page** follows the page as you navigate. Type another path to look at it instead, and press **Follow page** to go back.

On the **SQL** tab, write SQL in the editor and press Run or Ctrl+Enter. `:from` and `:to` are bound to the time range in the toolbar, and `:project` to the project. The dataset library lists the views and columns you can query. A dataset's quick query fills the editor with its latest rows.

The console renders in a Shadow DOM root on a `<spoar-debugtools>` element, so your site's CSS and the console's never meet.

## Embedding the console

`<Console />` uses plain Tailwind utilities and injects no CSS. In an app on Tailwind v4, import the tokens into your stylesheet:

```css
@import "tailwindcss";
@import "@spoar/debugtools/tailwind.css";
```

Without Tailwind, import the compiled stylesheet once:

```tsx
import { Console } from "@spoar/debugtools/console";
import "@spoar/debugtools/styles.css";

<Console theme="system" />;
```
