Developers
Referrals and visitor refs
When someone arrives through one of your partners' links, the SDK records the referral with no code from you, and every Plandalf checkout on your site credits that partner. This page covers the code for everything around it: reading the referral, the badge, checkouts Plandalf doesn't render, and visitor refs. Setting up the partner program is in Affiliates.
Referral capture
The visitor clicks a partner link
Partner links carry the partner's code, by default as
?via=CODE.The SDK swaps the code for a token
It asks Plandalf for a referral token (
rt_…) and keeps it in the browser and in apf_rtcookie until the program's attribution window ends.Checkouts carry it
present(),mount()and your buttons send the token with the checkout, so the sale is credited to the partner.
Read the referral
plandalf.referral() returns the page's referral handle. Await it for the referral, or null if there isn't one.
const referral = await plandalf.referral();
if (referral) {
console.log(referral.partner.name, referral.code, 'until', referral.expires_at);
}
Loop over .events to hear about the referral and the badge. The handle replays what already happened on this page, so a late listener doesn't miss anything.
for await (const event of plandalf.referral().events) {
console.log(event.type); // captured, changed, expired, shown, held-out, click, dismiss
}
The same events are DOM events on window (plandalf:referral, plandalf:referral-changed, plandalf:badge-click and so on) and work with plandalf('on', …). See Events. The purchase event includes referral with the partner's code and name.
Show the referral badge
The badge names the partner who sent the visitor. Put an element where you want it:
<div data-plandalf-referral data-plandalf-variant="note" data-plandalf-theme="auto"></div>
Or from code, or as its own tag:
plandalf.referral({ target: '#badge', variant: 'pill', template: 'Recommended by {partner}' });
<plandalf-referral variant="bar"></plandalf-referral>
The badge shows nothing when there's no referral, when the program's badge is off, after the visitor dismisses it, and for visitors in the badge holdout group (who still count for the partner). plandalf.from(badgeElement) returns the page's referral handle.
| Attribute | Values | What it does |
|---|---|---|
data-plandalf-decorate | Present on a link: when the visitor has a referral, the SDK appends pf_rt=<token> to the link's URL on click, whatever its host. Links to hosts in referral.decorate are decorated without it. | |
data-plandalf-referral | Marks an element as the place for the referral badge. The SDK appends a <plandalf-referral> badge inside it when the visitor arrived through a partner link. | |
data-plandalf-referral-template | On a data-plandalf-referral element: badge wording with a {partner} token. | |
data-plandalf-theme | light dark auto | On a data-plandalf-referral element: the badge colours. |
data-plandalf-variant | pill note bar float | On a data-plandalf-referral element: the badge style. Default pill. |
Stripe links and other checkouts
For a Stripe Payment Link or another site's checkout, carry the token yourself.
decorate(url)addspf_rt=<token>to a URL, and Stripe'sclient_reference_idonbuy.stripe.comlinks. Without a referral it returns the URL unchanged.data-plandalf-decorateon a link does the same when it's clicked.metadata()returns{ plandalf_referral: 'rt_…' }(or{}) to put on a Stripe Checkout Session your server creates.
const referral = plandalf.referral();
buyLink.href = referral.decorate('https://buy.stripe.com/YOUR-LINK');
const res = await fetch('/create-checkout-session', {
method: 'POST',
body: JSON.stringify({ metadata: referral.metadata() }),
});
To decorate every link to a host automatically, list it on the script tag with data-config-referral-decorate="shop.example.com". If your server records the sale itself, send the token as referral_token to POST /api/v1/affiliate/sales.
Change how capture works
The program's settings decide the link parameter and attribution window. These script tag attributes override them on one site (the same fields work as referral: {…} in plandalf('init', …) or plandalf('configure', …)):
| Attribute | What it does |
|---|---|
data-config-referral-params | Link parameters that carry a partner code, comma-separated. Default via. |
data-config-referral-decorate | Hosts whose links get pf_rt added on click. |
data-config-referral-cookie | Another cookie name to write the token to, as well as pf_rt. |
data-config-referral-global | A window global to set to the token, for code that read an older tracker's global. |
data-config-referral-metadata-key | The key metadata() uses. Default plandalf_referral. |
data-config-referral-consent | required waits for consent before reading links or storage. |
With consent required, call this when the visitor accepts (or false when they decline):
plandalf('consent', true);
To turn capture off on a page, start the SDK with plandalf('init', { apiBase: 'https://YOUR-ORG.plandalf.dev', referral: false }).
Visitor refs (pf_ref)
A visitor ref is your own id for a person, such as their email address or CRM id. It isn't a partner referral. Deadline timers use it so one person keeps one deadline timer on every device. See Deadline timers from code.
- Links set it with
?pf_ref=, for example?pf_ref={{subscriber.email}}in an email. - Code sets it with
plandalf('setRef', 'ada@example.com'), which works before the SDK loads.plandalf('setRef', null)clears it. plandalf('getRef')reads it, andplandalf('getIdentity')returns the whole record:anonymous_id,ref,first_seen_atandlast_seen_at.
The SDK keeps it in the browser across visits. If your form tool can only put the email in another parameter, such as ?email=, add data-config-ref="email" to the script tag. Without that setting, the SDK also reads the older ?ref= parameter.
plandalf.getAnonId() returns the browser's anonymous id, which the SDK makes on the first visit and keeps.