> The events a checkout sends while it's open, on the handle, on your page's elements and on window, and how to pass purchases to analytics and affiliate tools.

Source: https://plandalf.com/docs/developers/events

Developers

# Events and results

While a checkout is open, the SDK reports what happens: it opened, a payment went through, the buyer finished or closed it. You can hear about it in three ways. Pick the one that suits your code.

## The four checkout events

Event

Sent when

Data

`start`

The checkout opens

None

`purchase`

A payment goes through: the main purchase and each upsell

See [The purchase event](#purchase)

`complete`

The buyer finishes the checkout

`session`, `mode`, `test`

`cancel`

The buyer closes the checkout before finishing

`reason`: `cancel` (close button or backdrop) or `escape`

## From a handle

`present()` and `mount()` return a handle whose `.events` you can loop over. The loop ends after `complete` or `cancel`.

```js
const flow = plandalf.present('YOUR-CHECKOUT');

for await (const event of flow.events) {
  if (event.type === 'start') console.log('opened');
  if (event.type === 'purchase') console.log('paid', event.invoice_total, event.currency);
  if (event.type === 'cancel') console.log('closed', event.reason);
}
```

To wait only for the end, `await` the handle instead. See [Open a checkout](https://plandalf.com/docs/developers/open-a-checkout#flowresult).

## From your buttons

An element the SDK bound (a `data-plandalf-present` button or a `data-plandalf-mount` container) sends the same four events as DOM events named `plandalf:start`, `plandalf:purchase`, `plandalf:complete` and `plandalf:cancel`. They bubble, and `event.detail` holds the data.

```js
document.addEventListener('plandalf:complete', (e) => {
  const slug = e.target.dataset.plandalfPresent;
  location.href = '/thanks?checkout=' + slug;
});

document.addEventListener('plandalf:cancel', (e) => {
  console.log('closed with', e.detail.reason);
});
```

Deadline timer elements send `plandalf:tier-change` and `plandalf:expired` the same way. See [Deadline timers from code](https://plandalf.com/docs/developers/promos-from-code).

`plandalf.from(element)` returns the live handle behind a bound element, so you can call `.close()` on it. It returns `null` for a button that hasn't been clicked yet.

## From the SDK

`plandalf('on', name, callback)` listens for an SDK event, and `plandalf('off', name, callback)` stops. Each of these is also sent on `window` as `plandalf:<name>`, so `window.addEventListener` works too. Queue `on` before the script loads to catch `ready`.

`open`, `complete`, `close` and `error` come from checkouts opened with `plandalf.present()` or `plandalf('present', …)`; for buttons, listen on the element as above.

Event

Sent when

`plandalf:badge-click`

The visitor clicked the referral badge. Data: ReferralEventDetail (`target` is `badge` or `cta`).

`plandalf:badge-dismiss`

The visitor dismissed the referral badge. Data: ReferralEventDetail.

`plandalf:badge-holdout`

The visitor is in the badge holdout group, so no badge showed (attribution still counts). Data: ReferralEventDetail.

`plandalf:badge-shown`

The referral badge became visible. Data: ReferralEventDetail.

`plandalf:change`

Element only (`plandalf:change`, bubbles): the visitor picked another plan in a plan-selector widget. Detail: `{ planId }`.

`plandalf:close`

An offer opened with `present` closed. Data: `{ reason }`.

`plandalf:complete`

The buyer finished an offer opened with `present`. Data: the FlowResult.

`plandalf:error`

An offer opened with `present` failed. Data: `{ error }`.

`plandalf:expired`

Element only (`plandalf:expired`, bubbles): a promo countdown reached zero, or the promo has no live config. Detail: `{ slug, ref }`.

`plandalf:loaded`

Window only (`plandalf:loaded`): the SDK script has run and `window.plandalf` is ready to call.

`plandalf:open`

An offer is opening through `plandalf('present')` / `plandalf('open')`. Data: `{ slug }`.

`plandalf:purchase`

A purchase went through in any SDK checkout. Data: the purchase details, plus `referral` (`{ code, partner: { name } }`) when the visitor came through a partner link.

`plandalf:ready`

The SDK has started. Data: `{ instance }`.

`plandalf:referral`

Every referral and badge event below. Data: `{ type, detail }`.

`plandalf:referral-changed`

The visitor arrived through a different partner's link. Data: ReferralEventDetail (`from` is the previous code).

`plandalf:referral-expired`

The stored referral passed the program's attribution window. Data: ReferralEventDetail.

`plandalf:select`

Element only (`plandalf:select`, bubbles): the visitor chose a plan in a pricing-table widget. Detail: `{ planId }`.

## The purchase event

The SDK sends `purchase` once for every paid invoice, before the checkout moves to its next page. A checkout with an upsell sends it twice: once for the main purchase and once for the upsell.

Field

What it is

`invoice`

The invoice just paid

`invoice_total`

That invoice's total in the smallest currency unit (cents), not the total of the whole checkout

`currency`

The currency code

`is_first_invoice`

`true` for the main purchase, `false` for upsells

`customer`

The buyer, with their email

`session`

The whole checkout session; `session.completed_invoices` lists every invoice paid so far

`mode`, `test`

`test: true` for a purchase made with test cards

`referral`

`{ code, partner: { name } }` when the visitor came through a partner link (from `plandalf('on', 'purchase', …)` and the handle)

All three of these hear every purchase in any Plandalf checkout on the page:

```js
plandalf('on', 'purchase', (purchase) => { /* … */ });
window.addEventListener('plandalf:purchase', (e) => { const purchase = e.detail; });
for await (const ev of plandalf.present('YOUR-CHECKOUT').events) if (ev.type === 'purchase') { /* … */ }
```

**Count each invoice once**

Keep the ids of invoices you've already sent to analytics and skip repeats. A reload or a second listener shouldn't record the same sale twice.

### Send it to analytics

```js
plandalf('on', 'purchase', ({ invoice, invoice_total, currency, test }) => {
  if (test) return;
  gtag('event', 'purchase', { transaction_id: invoice?.ulid, value: invoice_total / 100, currency });
});
```

### Send it to an affiliate tracker

Trackers like Tolt, Rewardful and FirstPromoter attribute a sale two ways, and you can feed both. Pass the tracker's referral id into the Stripe customer's metadata with `configure`, and send the buyer's email to the tracker on purchase:

```html
<script>
  window.plandalf = window.plandalf || function () { (plandalf.q = plandalf.q || []).push(arguments); };
  plandalf('configure', { metadata: { tolt_referral: () => window.tolt_referral } });
  plandalf('on', 'purchase', ({ customer, test }) => { if (!test) window.tolt?.signup(customer.email); });
</script>
<script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
<!-- then the tracker's own script tag -->
```

The script tags can go in any order, as long as the stub line comes first. The metadata function runs when the checkout session is created, so the tracker has time to load. For other trackers, swap in their names:

Tracker

In `metadata`

On purchase

Tolt

`tolt_referral: () => window.tolt_referral`

`window.tolt.signup(customer.email)`

Rewardful

`referral: () => window.Rewardful?.referral`

`window.rewardful('convert', { email: customer.email })`

FirstPromoter

`fp_tid: () => window.$FPROM?.referral`

`window.$FPROM.trackSignup({ email: customer.email })`

The tracker matches the email first. That fails when the buyer pays with a different email from the one they signed up with, when the tracker's script is blocked, or when the same email came through two partners. The metadata on the Stripe customer still names the right partner in each case, so send both. The values also show on the customer's **Metadata** card in [Customers](https://plandalf.com/docs/customers/customers#metadata). For Plandalf's own partner program, see [Referrals from code](https://plandalf.com/docs/developers/referrals-from-code).

**Test purchases**

Every event about a test purchase carries `test: true`. Skip those in analytics, and never give anyone access because of one.

## On your server

Browser events can be blocked or missed if the buyer closes the tab. To record sales or grant access reliably, use [webhooks](https://plandalf.com/docs/api/webhooks).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
