> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Send custom events with crispstats()

> Track signups, checkouts and other actions with crispstats(name, props) in the browser or one POST from your server. Covers prop rules, limits, the queue stub and where events show.

A custom event records an action that a pageview cannot see, like a signup, a checkout or a click on a pricing toggle. With the CrispStats script on the page, call `crispstats("signup", { plan: "pro" })` where the action happens. The event appears under **Events** in the dashboard within seconds, with its count, unique visitors and a breakdown of every property you sent.

## Send your first event

Once the [script is installed](https://docs.crispstats.com/install/overview.md), the global `crispstats` function is available on every page. Call it with an event name and, optionally, an object of properties:

```js theme={"theme":{"light":"github-light","dark":"github-dark"}}
crispstats("signup", { plan: "pro", seats: 3 });
```

A real handler looks like this:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<form id="signup-form">
  <input name="email" type="email" required />
  <select name="plan">
    <option value="free">Free</option>
    <option value="pro">Pro</option>
  </select>
  <button type="submit">Create account</button>
</form>

<script>
  document.getElementById("signup-form").addEventListener("submit", function (event) {
    var plan = new FormData(event.target).get("plan");
    crispstats("signup", { plan: plan });
  });
</script>
```

The event is sent with `navigator.sendBeacon` (or `fetch` with `keepalive` when the beacon is refused), so it is delivered even when the click or submit navigates to another page.

To track a click without writing JavaScript, use [`data-cs-event` attributes](https://docs.crispstats.com/track/declarative-events) instead. To send an event from your backend, such as a renewal from a webhook, see [send events from your server](#send-events-from-your-server).

## Call crispstats() before the script loads

The script loads with `defer`, so inline code at the top of a page can run before it exists. Put this stub above the script tag. Calls made before the script loads are queued, then replayed in order once it starts:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!-- Above the script tag. Calls made before it loads wait here and run in order. -->
<script>
  if (typeof window.crispstats != "function") {
    var queue = [];
    var stub = function () { queue.push([].slice.call(arguments)); };
    stub.q = queue;
    ["identify", "revenue", "pageview"].forEach(function (method) {
      stub[method] = function () { queue.push(Object.assign([].slice.call(arguments), { m: method })); };
    });
    window.crispstats = stub;
  }
</script>
```

It queues `crispstats()`, `identify`, `revenue` and `pageview`, and is safe to leave in place: the script replaces it with the real function. In a TypeScript app, use the module version and the global types from [TypeScript](https://docs.crispstats.com/install/typescript.md) instead.

## Send events from your server

Actions that happen without a browser, such as a subscription renewal in a webhook or a job finishing in a queue, can be sent from your server with one `POST` to `https://in.crispstats.com/e` and a secret API key. Pass the visitor id you saved from `crispstats.id()` and the event joins that visitor's history:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://in.crispstats.com/e \
  -H "Authorization: Bearer $CRISPSTATS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"k":"cs_pk_xxxxxxxx","t":"event","n":"subscription_renewed","u":"","p":{"plan":"pro"},"vid":"0123456789abcdef0123456789abcdef"}'
```

[Server-side events](https://docs.crispstats.com/track/server-side-events.md) covers the key, the payload and the status codes.

## Event and property rules

Properties are a flat object. The script converts values before it sends them:

| You pass | CrispStats sends |
| - | - |
| A number, `true`, `false` or `null` | The value as it is. |
| A string | The string, cut to its first 500 characters. |
| An array or an object | Its string form, cut to 500 characters. `["a", "b"]` becomes `"a,b"`, and a plain object becomes `"[object Object]"`, so flatten objects into separate keys yourself. |
| `undefined` | Nothing: the key is left out. |

The collector also checks each event against these limits:

| Limit | Value | When it is exceeded |
| - | - | - |
| Event name length | 120 characters | The whole event is dropped. |
| Property key length | 60 characters | The whole event is dropped. |
| Properties per event | 40 | The first 40 are kept, the rest are ignored. |
| String property value | 500 characters | The script cuts it before sending. |
| Request body | 16 KB | The event is rejected. |

> [!WARNING]
>
> The script does not shorten event names or property keys. A browser event with a name over 120 characters or a key over 60 characters is dropped without an error, because the collector answers a browser event that fails these checks with the same empty `202` as an accepted one. Keep names short and fixed.

An empty name sends nothing. Surrounding whitespace in a name is trimmed when the event is stored.

### Naming events

* Use the same name every time an action happens, like `signup`, so counts add up in one row.
* Put the variable part in a property, not the name: `crispstats("plan selected", { plan: "pro" })` rather than `crispstats("pro plan selected")`.
* Do not put personal data such as email addresses in names or property values. Use [`crispstats.identify()`](https://docs.crispstats.com/track/identify-users) when you need to tie events to a signed-in user.

The tracker sends three event names itself when the matching modules are on: `Outbound link`, `File download` and `404`. Avoid using those names for your own events.

## When events are not sent

`crispstats()` is always defined once the script runs, but it sends nothing when the script is not tracking the page: on `localhost` without `data-allow-localhost`, on a hostname outside `data-domains`, on a path matched by `data-exclude`, in a browser that sends Do Not Track when the tag has `data-dnt`, in an automated browser, or in a browser where `localStorage.crispstats_ignore` is `"1"`. Under `data-manual`, events before the first `crispstats.pageview()` are dropped too. Calls in those cases are harmless. To see each event and why it was not sent, add [`data-debug`](https://docs.crispstats.com/tracking/localhost-and-testing#debug-mode).

## Where events show in the dashboard

Open **Events** in the site's sidebar. The **Events** table lists the custom events sent in the selected range, up to 200 names, with their **Count**, unique **Visitors** and **Last seen** time. It adds **Change** when you compare periods and **Revenue** when an event carries revenue.

Select an event to open **Properties of** that event: each property key with its values, how often each value was sent and its share. Select the event name there to filter the whole dashboard to visitors who sent it.

To count an event as a conversion and see its conversion rate, add it as a [goal](https://docs.crispstats.com/dashboard/goals.md). The [events screen](https://docs.crispstats.com/dashboard/events.md) describes the page in full.

## Next steps

> [card] [Track clicks with attributes](https://docs.crispstats.com/track/declarative-events.md)
>
> Send events from HTML with `data-cs-event`, no JavaScript needed.

> [card] [Identify signed-in users](https://docs.crispstats.com/track/identify-users.md)
>
> Attach your own user id to the events a visitor sends.

> [card] [Set up goals](https://docs.crispstats.com/dashboard/goals.md)
>
> Turn an event into a conversion with a rate you can track.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.