# Plugins

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

> Every plugin exported from @spoar/sdk/plugins, with its signature and a usage example.

Import plugins from `@spoar/sdk/plugins` and pass them in `plugins`, or add one later with `analytics.use(plugin)`.

```ts
import { createAnalytics } from "@spoar/sdk";
import { errors, speedInsights } from "@spoar/sdk/plugins";

export const analytics = createAnalytics({
  project: "example.com",
  key: "pk_...",
  endpoint: "/_ra",
  plugins: [speedInsights(), errors()],
});
```

Each plugin has its own page with the events it sends and their props. The [overview](https://docs.analytics.remcostoeten.nl/docs/plugins.md) has one table of every plugin and explains how to write your own.

## Plugins

### `pageviews()`

```ts
pageviews(): Plugin
```

Sends a `pageview` on load and on client-side navigation. It is on by default, so add it yourself only after setting `pageviews: false`. See [Pageviews](https://docs.analytics.remcostoeten.nl/docs/plugins/pageviews.md).

```ts
createAnalytics({ pageviews: false, plugins: [pageviews()] });
```

### `speedInsights(options?)`

```ts
speedInsights(options?: { sampleRate?: number; random?: () => number; load?: () => Promise<Vitals> }): Plugin
```

Sends `web_vital` for LCP, INP, CLS, FCP and TTFB. `sampleRate` is the share of page loads that measure, from 0 to 1, and defaults to 1. `random` and `load` exist for tests. See [Speed insights](https://docs.analytics.remcostoeten.nl/docs/plugins/speed-insights.md).

```ts
createAnalytics({ plugins: [speedInsights({ sampleRate: 0.5 })] });
```

### `errors()`

```ts
errors(): Plugin
```

Sends `error` for uncaught errors and unhandled rejections, with the last 20 breadcrumbs. Messages, stacks and breadcrumbs are scrubbed of query strings, emails, tokens and long numbers. See [Errors](https://docs.analytics.remcostoeten.nl/docs/plugins/errors.md).

```ts
createAnalytics({ plugins: [errors()] });
```

### `scrub(text)`

```ts
scrub(text: string): string
```

The scrubbing function `errors()` uses, exported for your own error reporting.

```ts
scrub("GET /api/users?token=abc failed for ada@example.com"); // "GET /api/users[x] failed for [x]"
```

### `scrollDepth()`

```ts
scrollDepth(): Plugin
```

Sends `scroll_depth` with the deepest percentage reached on a page. See [Scroll depth](https://docs.analytics.remcostoeten.nl/docs/plugins/scroll-depth.md).

```ts
createAnalytics({ plugins: [scrollDepth()] });
```

### `engagement()`

```ts
engagement(): Plugin
```

Sends `engagement` with the milliseconds a page was visible. See [Engagement](https://docs.analytics.remcostoeten.nl/docs/plugins/engagement.md).

```ts
createAnalytics({ plugins: [engagement()] });
```

### `clicks()`

```ts
clicks(): Plugin
```

Sends `click` for elements with `data-spoar-click`. See [Clicks](https://docs.analytics.remcostoeten.nl/docs/plugins/clicks.md).

```ts
createAnalytics({ plugins: [clicks()] });
```

### `autocapture()`

```ts
autocapture(options?: { kinds?: ("click" | "submit" | "change" | "rage")[] }): Plugin
```

Sends `autocapture` for clicks and form submits, with `change` and `rage` as opt-in kinds. It never records input values. See [Autocapture](https://docs.analytics.remcostoeten.nl/docs/plugins/autocapture.md).

```ts
createAnalytics({ plugins: [autocapture({ kinds: ["click", "submit", "rage"] })] });
```

### `outboundLinks()`

```ts
outboundLinks(): Plugin
```

Sends `outbound_click` for links to other hosts and `file_download` for file links. See [Outbound links](https://docs.analytics.remcostoeten.nl/docs/plugins/outbound-links.md).

```ts
createAnalytics({ plugins: [outboundLinks()] });
```

### `forms()`

```ts
forms(): Plugin
```

Sends `form_submit` with the form id and action path. See [Forms](https://docs.analytics.remcostoeten.nl/docs/plugins/forms.md).

```ts
createAnalytics({ plugins: [forms()] });
```

### `ignoreSelf()`

```ts
ignoreSelf(): Plugin
```

Sends nothing. Opening any page with `?spoar=ignore` opts that browser out. See [Ignore self](https://docs.analytics.remcostoeten.nl/docs/plugins/ignore-self.md).

```ts
createAnalytics({ plugins: [ignoreSelf()] });
```

### `botSignals()`

```ts
botSignals(): Plugin
```

Sends nothing of its own. It adds bot hint bits to every event. See [Bot signals](https://docs.analytics.remcostoeten.nl/docs/plugins/bot-signals.md).

```ts
createAnalytics({ plugins: [botSignals()] });
```

### `experiments(assigned)`

```ts
experiments(assigned: { [experiment: string]: string }): Plugin
```

Sends `experiment_exposure` per experiment and adds an `experiment:<id>` prop to every event. See [Experiments](https://docs.analytics.remcostoeten.nl/docs/plugins/experiments.md).

```ts
createAnalytics({ plugins: [experiments({ hero: "b" })] });
```

### `notFound()`

```ts
notFound(): Plugin
```

Sends `not_found` with the referrer on pages marked as not found. See [Not found](https://docs.analytics.remcostoeten.nl/docs/plugins/not-found.md).

```ts
createAnalytics({ plugins: [notFound()] });
```

### `groups<Groups>()`

```ts
groups<Map extends GroupMap = GroupMap>(): Groups<Map>
```

Returns a plugin with `set(type, id, traits?)` and `leave(type)`. Joined groups are added to every event as the `group:<type>` dimension. See [Groups](https://docs.analytics.remcostoeten.nl/docs/plugins/groups.md).

```ts
const workspace = groups<{ company: { plan: "free" | "pro" } }>();

createAnalytics({ plugins: [workspace] });
workspace.set("company", "acme", { plan: "pro" });
```

### `speedProps(metric, sampleRate, connection)`

```ts
speedProps(metric: Measured, sampleRate: number, connection: string | null): Props
```

Turns a web-vitals metric into `web_vital` props. `speedInsights()` uses it, and it is exported for custom measuring.

## Types

* `SpeedOptions`: the options of `speedInsights`.
* `Groups<Map>` and `TraitsArgs<Map, Type>`: the plugin type `groups()` returns and the typed arguments of its `set`.
