# JavaScript API

> Every command and method of the tracking script, including payment, reset, decorate and scroll goals with a threshold and delay.

The tracking script adds `window.reveata`. Call it after the script has loaded, or add the [queue snippet](https://reveata.com/docs/install-the-script#calling-the-script-before-it-loads) so calls made earlier are kept. No call ever throws into your page.

## Commands

| Command | What it does |
|---|---|
| `reveata('goal', name, props?)` | Records a [goal](https://reveata.com/docs/custom-goals). Names are lowercase letters, digits, `_`, `-` and `:` (for namespaces such as `checkout:started`), up to 64 characters. Up to 20 properties. |
| `reveata('identify', { userId, email?, name?, ...props })` | Links the visitor to your user. See [identify visitors](https://reveata.com/docs/identify-visitors). |
| `reveata('pageview', { url?, props? })` | Sends a pageview yourself (with `data-auto="false"`, or for virtual pages). |
| `reveata('payment', { email?, transactionId?, amount?, currency? })` | On a thank-you page: links the payment that just happened to this visitor. See [thank-you page payments](https://reveata.com/docs/thank-you-page-payments). |
| `reveata('reset')` | On log out: forgets this browser's visitor. The next pageview starts a new visitor and a first visit, so a shared computer doesn't merge two people. Nothing happens in cookieless mode, which stores nothing. |
| `reveata('optout')` / `reveata('optin')` | Stops (or resumes) counting this browser. |

## Methods

| Method | Returns |
|---|---|
| `reveata.visitorId()` | The visitor id, or `null` (cookieless mode, before the first pageview). |
| `reveata.sessionId()` | The current visit id, or `null`. |
| `reveata.metadata()` | `{ reveata_visitor_id, reveata_session_id }` for checkout metadata, or `{}`. |
| `reveata.decorate(url)` | `url` with Reveata's ids added, for links you open from code. |

### `decorate(url)`

The script adds ids to links people click: the cross-domain id `_dt` on links to your [other domains](https://reveata.com/docs/install-the-script) (`data-domains`). Stripe Payment Links get a `client_reference_id`. Links you open from JavaScript are never clicked, so pass them through `decorate` first:

```js
window.location.href = reveata.decorate('https://shop.example-checkout.com/cart');
window.open(reveata.decorate('https://buy.stripe.com/test_123'));
```

Any other address comes back unchanged, as it does in cookieless mode. The `_dt` id is only accepted on arrival when the visitor comes from one of your domains within 2 minutes, so a decorated link that gets copied and shared later doesn't carry the visitor along.

## Scroll goals

`data-reveata-scroll` records a goal the first time an element is visible enough:

```html
<section data-reveata-scroll="scroll:pricing">…</section>
```

With the opt-in [extras script](https://reveata.com/docs/custom-goals#automatic-goals-opt-in) (`script.extras.js`) on the page, scroll goals take options:

```html
<section
  data-reveata-scroll="scroll:pricing"
  data-reveata-scroll-threshold="0.8"
  data-reveata-scroll-delay="1500"
  data-reveata-scroll-plan="pro">
  …
</section>
```

- `data-reveata-scroll-threshold`: how much of the element must be visible, from `0` to `1`. The default is `0.5`. An element taller than the screen can never be fully visible, so keep the threshold below the share of it that fits on screen.
- `data-reveata-scroll-delay`: how many milliseconds it must stay visible. If the visitor scrolls away earlier, nothing is sent, and the wait starts again when it comes back.
- Any other `data-reveata-scroll-<name>` attribute becomes a goal property, with dashes turned into underscores (`plan_name`).

Each element fires once per page. Without the extras script, the main script fires at half visible, without a delay or properties, so a page never loses its scroll goals. Load the extras script with `defer` next to the main one (before or after it).

## npm and Next.js

The [npm packages](https://reveata.com/docs/npm-packages) have the same calls:

```js
import { goal, identify, payment, reset, decorate } from '@reveata/tracker';
```

```tsx
import { trackGoal, identify, trackPayment, resetVisitor, decorateUrl } from '@reveata/next';
```

Calls made before the script has loaded are queued. `decorate` and `decorateUrl` return the address unchanged until then.
