# Send goals from JavaScript

> Call goal on window.analytics when something happens, send a sale with its amount, currency and order id, and see why a call sent nothing.

Send a goal from your own code at the moment it happens: a sign-up that succeeded, a booking made, a sale paid. The snippet gives your page one function for it, `goal`, on `window.analytics`.

## Send a goal

Call this when the thing you count happens, with your goal's name in place of `signup`:

```js
window.analytics.goal("signup");
```

1. Call it once the action has succeeded, not when it starts: after your server has accepted the sign-up, say.
2. The name is the goal's name on your dashboard: up to 200 characters, spaces at its ends dropped. It is counted letter for letter, so `Signup` and `signup` are two goals.
3. Each call counts: three calls are the goal reached three times. A goal needs nothing in Setup first: it joins **Setup** → **Goals** and the **Goals** card when it first arrives. To see it listed before then, add it in Setup as [Set up goals](https://centpath.app/docs/goals) shows.

## Calls made before the script has loaded

The script loads with `defer`, after your page's own scripts. The snippet's first line is a queue: until the script runs, it keeps each goal your page sends, and the script sends them, in order, once it does. So a goal sent as the page opens, like on a thank-you page, is counted too. Keep that line above the script's, as the snippet has it. [The script](https://centpath.app/docs/script#the-queue) has the details.

With Google Tag Manager, the queue exists only once Tag Manager has run the tag. A goal your page sends earlier fails, with an error in your page's code, so mark the link, button or form with the data-goal attribute instead: see [Count clicks and form sends](https://centpath.app/docs/data-goal).

If another tool already uses `window.analytics`, the snippet can name ours `window.centpath`: see [Install CentPath](https://centpath.app/docs/install#another-tool-already-uses-window-analytics). Call `goal` on `window.centpath` then.

## Send a sale

A sale is the goal named `purchase`, with its amount, its currency and the order's id. On your thank-you page, call:

```js
window.analytics.goal("purchase", {
  revenueMinorUnits: 4900, // 49.00 USD, in its smallest unit
  currency: "USD",
  id: orderId, // counts each sale once
});
```

| Option | What it holds |
|---|---|
| `revenueMinorUnits` | The amount, as a whole number in the currency's smallest unit: `4900` for 49.00 USD, `49` for 49 JPY. |
| `currency` | The amount's three-letter currency code, like `USD` or `EUR`. An amount needs one. |
| `id` | The order's id. A sale sent again with the same id, when the buyer reloads the page, counts once. |

The amount you send is the one CentPath counts. If the site holds a read-only key from Stripe, Polar or Paddle, send as `id` the id the provider lists the sale under: the Checkout Session's (`cs_…`) for Stripe, the checkout's for Polar, the transaction's (`txn_…`) for Paddle. The provider's amount and currency then replace yours, and the sale counts once. Any other id is a second sale. [Count sales from any checkout](https://centpath.app/docs/other-checkouts) and [How revenue is counted](https://centpath.app/docs/revenue) say more.

## When a call sends nothing

The script checks each call before it sends it, and writes why in your browser's console when it doesn't:

| The console says | Why |
|---|---|
| `[analytics] goal: bad name` | The name is empty or longer than 200 characters. |
| `[analytics] goal: bad revenueMinorUnits` | The amount isn't a whole number of 0 or more, or has no currency. |

A site whose **Setup** → **Sales** only accepts amounts from its payment provider refuses every goal sent with an amount: see [How revenue is counted](https://centpath.app/docs/revenue#only-amounts-from-your-payment-provider).
