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

# Recognise returning visitors with persistent visitors

> Turn on persistent visitors to recognise the same browser across days: one switch in settings, then the updated snippet with data-persist, which stores a random id in localStorage.

Persistent visitors lets CrispStats recognise the same browser on later days. You turn it on with one switch, **Persistent visitors** in the site's settings, and then deploy the snippet the dashboard gives you, which now includes `data-persist`. The script creates a random id, keeps it in the browser's `localStorage` under `crispstats_vid`, and sends it with every event. It is off by default, because it stores an identifier on the visitor's device.

Without it, CrispStats recognises a visitor for one day only, from a [daily-rotating hash](https://docs.crispstats.com/privacy/cookieless-tracking.md), and stores nothing in the browser.

## Turn on persistent visitors

The setting and the attribute work together: the collector uses the id only while the setting is on, and the script creates an id only when the tag has `data-persist`. The dashboard keeps the two in step, so follow these steps in order.

**1. Turn on the site setting**

In the dashboard, open **Settings**, **General**, **Persistent visitors** and turn on **Recognise returning visitors across days**. Changing site settings needs the owner or admin role on the site.

**2. Deploy the updated snippet**

Open **Settings**, **Tracking code**. The snippet for your platform now includes `data-persist`, and the note under it says so. Copy it and replace the tag on your site:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-persist
  src="https://in.crispstats.com/d.js"
></script>
```

If you write the tag by hand, add `data-persist` (`data-persist=""` in JSX) only while the setting is on.

**3. Check that the id is stored**

Open your site, then run this in the browser console:

```js theme={"theme":{"light":"github-light","dark":"github-dark"}}
localStorage.getItem("crispstats_vid");
```

It returns a string of digits. Visit the site again on a later day from the same browser and the visitor is counted as returning.

| Setting | `data-persist` on the tag | Result |
| - | - | - |
| On | Yes | Persistent visitors. |
| On | No | Daily visitors: the script never creates an id. Deploy the updated snippet. |
| Off | Yes | Daily visitors, but the browser still stores an id the collector ignores. Remove `data-persist`. |
| Off | No | Daily visitors, nothing stored. The default. |

## How it works

**1. The script creates an id**

On the first page load with `data-persist`, the script generates an id from 128 random bits with `crypto.getRandomValues` and saves it in `localStorage.crispstats_vid`. Later page loads reuse it. The id is random: it is not derived from the device, the IP address or the User-Agent.

**2. Every event carries it**

The script sends the id as `vid` with each event. `crispstats.id()` also passes it to the collector, so the visitor id you send to your payment provider at checkout matches the stored one.

**3. The collector hashes it per site**

When the site setting is on, the collector stores the first 32 hex characters of `SHA-256("persist" | site id | id)` as the visitor id. It has the same shape as a daily id, and the same browser gets a different value on each of your sites, so stored ids cannot be matched across sites. The raw id from `localStorage` is not stored. No salt rotation applies, so the id stays the same across days.

If the site setting is off, or the browser blocks `localStorage`, the event falls back to the daily hash. The IP address and User-Agent are still read for the browser, device, location and bot filtering, and discarded as usual.

## What changes in your reports

* **Visitors over a period** count each id once. Without persistent visitors, a visitor on three different days counts as three. See [counting visitors](https://docs.crispstats.com/concepts/metrics.md#counting-visitors).
* **Retention cohorts** count returns on later days for persistent visitors, not only for visitors you [identify](https://docs.crispstats.com/track/identify-users.md).
* **Visitor profiles** gather a browser's visits from every day under one profile, with the source and landing page of its first visit.

The id is per browser. The same person on a phone and a laptop is still two visitors, and clearing site data starts a new id.

## Consent considerations

> [!WARNING]
>
> Persistent visitors stores an identifier on the visitor's device. Under the ePrivacy Directive, UK PECR and similar rules, that generally requires the visitor's consent first, and under GDPR the id is personal data. Turn it on only if you have a consent mechanism, or have established that you do not need one. This is a description of the feature, not legal advice.

The script reads `data-persist` once, when it starts, and a second copy of the script on the same page is ignored. To load it only after consent, inject the tag from your consent tool's callback:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script>
  function loadCrispStats(persist) {
    var script = document.createElement("script");
    script.defer = true;
    script.src = "https://in.crispstats.com/d.js";
    script.setAttribute("data-site", "cs_pk_xxxxxxxx");
    script.setAttribute("data-domains", "example.com");
    if (persist) script.setAttribute("data-persist", "");
    document.head.appendChild(script);
  }
</script>
```

Call `loadCrispStats(true)` once the visitor has given consent, and `loadCrispStats(false)` otherwise. Call it once per page load, after your consent tool knows the visitor's choice. If the script already started without `data-persist` on this page, the id is created from the next page load that includes the attribute.

### When a visitor withdraws consent

Stop adding `data-persist` for that visitor, and delete the stored id:

```js theme={"theme":{"light":"github-light","dark":"github-dark"}}
localStorage.removeItem("crispstats_vid");
```

The visitor is then counted with the daily hash again. Events already stored under the persistent id are kept until your plan's [retention](https://docs.crispstats.com/privacy/data-collected.md#retention) removes them.

### Turning the setting off

When you turn **Persistent visitors** off, the collector stops using `vid` within about a minute, once its cached copy of your site settings refreshes, and uses the daily hash for new events. Ids already in visitors' browsers stay there until they are cleared, and the script keeps sending them for as long as `data-persist` is on your tag. Remove the attribute as well if you no longer want them sent.

## Learn more on crispstats.com

* Glossary: [unique visitor](https://crispstats.com/glossary/unique-visitor), [visitor hash](https://crispstats.com/glossary/visitor-hash), [retention](https://crispstats.com/glossary/retention) and [cookie consent](https://crispstats.com/glossary/cookie-consent).

## Next steps

> [card] [Cookieless tracking](https://docs.crispstats.com/privacy/cookieless-tracking.md)
>
> How the default daily id is built and why it cannot be linked across days.

> [card] [Compliance facts](https://docs.crispstats.com/privacy/compliance.md)
>
> What each option changes, summarised for a privacy review.

> [card] [Retention cohorts](https://docs.crispstats.com/dashboard/retention.md)
>
> See how often visitors come back on later days.


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