Skip to content
Docs menuTrack goals › Set up goals

Send goals from JavaScript

On this page

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:

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

If another tool already uses window.analytics, the snippet can name ours window.centpath: see Install CentPath. 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:

window.analytics.goal("purchase", {
  revenueMinorUnits: 4900, // 49.00 USD, in its smallest unit
  currency: "USD",
  id: orderId, // counts each sale once
});
OptionWhat it holds
revenueMinorUnitsThe amount, as a whole number in the currency's smallest unit: 4900 for 49.00 USD, 49 for 49 JPY.
currencyThe amount's three-letter currency code, like USD or EUR. An amount needs one.
idThe 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 and How revenue is counted 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 saysWhy
[analytics] goal: bad nameThe name is empty or longer than 200 characters.
[analytics] goal: bad revenueMinorUnitsThe 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.