> ## 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.

# Show a live visitor count on your site

> Publish one number, the visitors on your site right now, and show it on your own pages with a small HTML and JavaScript badge from CrispStats.

> [!NOTE]
>
> For anything new, use [widgets](https://docs.crispstats.com/share/widgets.md) instead. The [live visitors widget](https://docs.crispstats.com/share/widgets/live.md) shows the same count with no code of your own, hides small numbers, and works on your site, in a README and in Notion. This badge keeps working for pages that already use it.

The CrispStats live visitor badge shows how many people are on your site right now, on your own pages. You switch on **Make the live count public** in **Settings**, **Sharing and widgets**, copy the generated snippet, and paste it where the number should appear. The snippet calls a public endpoint on the collector that returns one number and nothing else about your traffic.

The live count is separate from the [public dashboard](https://docs.crispstats.com/share/public-dashboard.md). You can publish the count while the dashboard stays private.

## Add the badge

**1. Make the live count public**

Open your site, then **Settings** and the **Sharing and widgets** tab. Under **Live visitor badge**, switch on **Make the live count public**. Only owners and admins can change this switch.

**2. Copy the snippet**

The snippet appears below the switch with your site's id and public key filled in. It looks like this:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<span id="live-visitors"></span>
<script>
  fetch("https://in.crispstats.com/live/SITE_ID/count?k=cs_pk_xxxxxxxx")
    .then(function (response) { return response.ok ? response.json() : null; })
    .then(function (data) {
      if (data) document.getElementById("live-visitors").textContent = data.visitors + " online now";
    });
</script>
```

Copy it from the dashboard rather than from this page, so `SITE_ID` and `cs_pk_xxxxxxxx` hold your real values.

**3. Paste it into your page**

Put the snippet where the number should appear, for example in a footer or next to a signup button. Style the `span` as you like. If the request fails, the snippet leaves the `span` empty, so nothing broken shows on your page.

**4. Check it**

Reload your page. The `span` shows a text such as `3 online now`. If it stays empty, see [Troubleshooting](#troubleshooting).

## How the count works

The badge counts visitors seen on your site in the last 5 minutes. A visitor counts once, however many tabs or pages they have open.

The collector caches the answer for 10 seconds per site and sends `Cache-Control: public, max-age=10`. Requests within those 10 seconds get the same number, so polling more often than every 10 seconds does not make the badge more current.

The snippet fetches the count once, when the page loads. To keep the number current while the page stays open, fetch it again on a timer:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<span id="live-visitors"></span>
<script>
  (function () {
    var url = "https://in.crispstats.com/live/SITE_ID/count?k=cs_pk_xxxxxxxx";
    var badge = document.getElementById("live-visitors");
    function refresh() {
      fetch(url)
        .then(function (response) { return response.ok ? response.json() : null; })
        .then(function (data) {
          if (data) badge.textContent = data.visitors + " online now";
        })
        .catch(function () {});
    }
    refresh();
    setInterval(refresh, 30000);
  })();
</script>
```

## Endpoint reference

`GET https://in.crispstats.com/live/{siteId}/count?k={publicKey}`

The endpoint needs no secret: the site id and the public key both appear in the snippet and are safe to publish. It answers any origin (`Access-Control-Allow-Origin: *`), so you can call it from any page.

**siteId** `string` *required*

Your site's id, as filled in by the snippet in **Settings**, **Sharing and widgets**.

**k** `string` *required*

Your site's public key, which starts with `cs_pk_`. It must belong to the site in the path.

**visitors** `number`

Visitors on the site in the last 5 minutes.

```json 200 theme={"theme":{"light":"github-light","dark":"github-dark"}}
{ "visitors": 3 }
```

```json 404 theme={"theme":{"light":"github-light","dark":"github-dark"}}
{ "error": "not_found" }
```

The endpoint answers `404` with the same body when the site does not exist, when the key is wrong or belongs to another site, when **Make the live count public** is off, and when tracking for the site is paused or the account is suspended.

## Turn the badge off

Switch off **Make the live count public**. The endpoint then answers `404`, and the snippet leaves the `span` empty. The collector keeps site settings for up to a minute, so the change can take about a minute to apply. Remove the snippet from your pages as well, so they stop making the request.

## Troubleshooting

> [accordion] The badge stays empty
>
> Open your browser's network tab and find the request to `/live/.../count`. A `404` means the live count is not public, or the site id or key in the snippet is not this site's. Copy the snippet again from **Settings**, **Sharing**. If you just switched the live count on, wait a minute and reload.

> [accordion] The badge stopped working after I changed my public key
>
> The snippet contains the public key. After you issue a new key, copy the snippet again from **Settings**, **Sharing and widgets** and replace the old one on your pages.

> [accordion] The request is blocked by my Content Security Policy
>
> Allow `https://in.crispstats.com` in `connect-src`. See [Content Security Policy](https://docs.crispstats.com/help/content-security-policy.md).

> [accordion] The badge shows 0 while I am on the site
>
> Your own visits may be excluded, for example by an excluded IP address or `localStorage.crispstats_ignore`, or the page may run on `localhost`, which the script ignores by default. Check from another device or network. See [exclude traffic](https://docs.crispstats.com/tracking/exclude-traffic.md).

## Next steps

> [card] [Watch visitors live](https://docs.crispstats.com/dashboard/realtime.md)
>
> See who is on your site right now in the Realtime view.

> [card] [Share a public dashboard](https://docs.crispstats.com/share/public-dashboard.md)
>
> Give anyone with a link a read-only view of your traffic.

> [card] [Publish a status page](https://docs.crispstats.com/share/status-page.md)
>
> Show your uptime and recent incidents on a public page.


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