PlandalfDocsSign inStart free trial

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

EventSent whenData
startThe checkout opensNone
purchaseA payment goes through: the main purchase and each upsellSee The purchase event
completeThe buyer finishes the checkoutsession, mode, test
cancelThe buyer closes the checkout before finishingreason: 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.

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.

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.

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.

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.

EventSent when
plandalf:badge-clickThe visitor clicked the referral badge. Data: ReferralEventDetail (target is badge or cta).
plandalf:badge-dismissThe visitor dismissed the referral badge. Data: ReferralEventDetail.
plandalf:badge-holdoutThe visitor is in the badge holdout group, so no badge showed (attribution still counts). Data: ReferralEventDetail.
plandalf:badge-shownThe referral badge became visible. Data: ReferralEventDetail.
plandalf:changeElement only (plandalf:change, bubbles): the visitor picked another plan in a plan-selector widget. Detail: { planId }.
plandalf:closeAn offer opened with present closed. Data: { reason }.
plandalf:completeThe buyer finished an offer opened with present. Data: the FlowResult.
plandalf:errorAn offer opened with present failed. Data: { error }.
plandalf:expiredElement only (plandalf:expired, bubbles): a promo countdown reached zero, or the promo has no live config. Detail: { slug, ref }.
plandalf:loadedWindow only (plandalf:loaded): the SDK script has run and window.plandalf is ready to call.
plandalf:openAn offer is opening through plandalf('present') / plandalf('open'). Data: { slug }.
plandalf:purchaseA 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:readyThe SDK has started. Data: { instance }.
plandalf:referralEvery referral and badge event below. Data: { type, detail }.
plandalf:referral-changedThe visitor arrived through a different partner's link. Data: ReferralEventDetail (from is the previous code).
plandalf:referral-expiredThe stored referral passed the program's attribution window. Data: ReferralEventDetail.
plandalf:selectElement 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.

FieldWhat it is
invoiceThe invoice just paid
invoice_totalThat invoice's total in the smallest currency unit (cents), not the total of the whole checkout
currencyThe currency code
is_first_invoicetrue for the main purchase, false for upsells
customerThe buyer, with their email
sessionThe whole checkout session; session.completed_invoices lists every invoice paid so far
mode, testtest: 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:

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

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:

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

TrackerIn metadataOn purchase
Tolttolt_referral: () => window.tolt_referralwindow.tolt.signup(customer.email)
Rewardfulreferral: () => window.Rewardful?.referralwindow.rewardful('convert', { email: customer.email })
FirstPromoterfp_tid: () => window.$FPROM?.referralwindow.$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. For Plandalf's own partner program, see 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.